onListItemClick回调未激活

时间:2012-01-07 09:59:01

标签: android list listactivity

public class List_Items extends ListActivity{

private ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

   public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);


       setContentView(R.layout.list_item);

       List <ImageAndText> total_list=new ArrayList<ImageAndText>();

           ListView lv = (ListView) this.findViewById(android.R.id.list);

       lv.setAdapter((ListAdapter) new ImageAndTextListAdapter(this, total_list));

       getListView().setTextFilterEnabled(true);

              //////////////////////////////////////////////////////
      Button btn=(Button) findViewById(R.id.button_sync);
      btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
     // TODO Auto-generated method stub
Toast message=Toast.makeText(getApplicationContext(), "click the list_item", Toast.LENGTH_LONG);
            message.show();}
      });
       }

  public void onListItemClick(ListView parent, View v, int position, long id) 
  {         
  Toast.makeText(getApplicationContext(), "You have selected " +(position+1)+"th item",
          Toast.LENGTH_SHORT).show();}

}

我有这个list_item,它扩展了listactivity。但是,当我单击其中一行时,回调onListItemClick没有被激活。这是为什么?在这个问题上,我不需要使用适配器吗?

}

3 个答案:

答案 0 :(得分:1)

你还没有注册你的项目点击列表视图。

listView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
            Log.i("List Clicked ....", "List Clicked...");          
        }
    });

listView.setOnItemClickListener(listener);
public OnItemClickListener listener = new OnItemClickListener() {

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        Log.i("List Clicked ....", "List Clicked...");      

    }
};

答案 1 :(得分:1)

您必须在自定义ListView的 xml的所有小部件中使用以下内容

android:focusable="false"

android:focusableInTouchMode="false"

希望有所帮助

答案 2 :(得分:0)

你能试试吗

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
  super.onListItemClick(l, v, position, id);
  Log.i("You clicked the list");
}