开发者

Analize listview header click

I have 2 layout .xml forms. One is header form and the other is to display the list. Using the header view I can display the header. When I tryed to click the button on the header form I cannot get the response. Please help me Here is my code.

//To display the Taskname as listview
                ListView listView = getListView();
                TextView HeaderUnametxt=(TextView) header.findViewById(R.id.UsernameHead);
                TextView HeaderCnametxt=(TextView) header.findViewById(R.id.CompanynameHead);
                String UName= bundle.getString("UserName");
                String CName= bundle.getString("Company");
                HeaderUnametxt.setText(UName); //To display the Usename on the header
                HeaderCnametxt.setText(CName); //To display the Company Name on the header
                listView.addHeaderView(header);
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.tasklist,
            R.id.Tasklist, TaskNames));  

header.xml file is as.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
    <TableLayout android:id="@+id/TableLayout01" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content">
        <TableRow android:id="@+id/TableRow01" 
        android:layout_width="fill_parent" 
        android:background="@color/white"
        android:layout_height="wrap_content">
            <ImageView android:id="@+id/Imagecompany" 
            android:layout_width="wrap_content" 
            android:src="@drawable/company"
            android:layout_height="wrap_content">
            </ImageView>
            <TextView android:background="@color/white"
            android:textColor="#0000FF"
            android:text="Guest"            
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:id="@+id/CompanynameHead">
            </TextView>     
            <ImageView android:id="@+id/ImageView01" 
            android:layout_width="wrap_content"
            android:src="@drawable/user"
            android:layout_height="wrap_content">
            </ImageView>
            <TextView android:background="@color/white"
            android:textColor="#0000FF"
            android:text="Guest"            
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:id="@+id/UsernameHead">
            </TextView>     
            <Button android:text="Log Out" 
            android:layout_width="60px" 
            android:layout_height="20px"
            android:id="@+id/Logout">
            </Button>开发者_C百科
        </TableRow>
    </TableLayout>
</LinearLayout>


try this code ...it's work for ListView Header button click..

public class ListHeaderActivity extends ListActivity implements
        View.OnClickListener {

    View header;
    ArrayAdapter<String> aR;
    ListView lstVw;
    String[] str = new String[] { "item1", "item2", "item3", "item4",
            "item5", "item6" };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        lstVw = getListView();
        header = getLayoutInflater().inflate(R.layout.header, lstVw, false);
        lstVw.addHeaderView(header);
        aR = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, str);
        lstVw.setAdapter(aR);
        View logoutBtn = findViewById(R.id.Logout);
        logoutBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Toast.makeText(ListHeaderActivity.this, "it's works.!", 1000).show();
    }

}


Try to use this method instead. addHeaderView (View v, Object data, boolean isSelectable) And set isSelectable to true. Otherwise this view might be untouchable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜