setOnItemClickListener not responding for Custom ListView

独自空忆成欢 提交于 2019-12-07 00:41:01

问题


I wrote a custom Adapter for a listview ,but when i tried implement click event for list item ,i found that it was not responding ,I will be glad if someone suggest me a solution.

public class TourList extends ListActivity {
....
setContentView(R.layout.tourlist);
.....





getListView().setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                     //i couldn't reach here
             Log.v(TAG,"did u get me");
        }
      }); 
adap = new MyAdapter(TourList.this,mylist);
getListView().setAdapter(adap);

and my custom adapter is

private class MyAdapter extends BaseAdapter {

            ArrayList<HashMap<String,String>> elements;
            Context ctx;

            public MyAdapter(Context context, ArrayList<HashMap<String,String>> mylist) {
                  this.elements=mylist;
                  this.ctx=context;
             }

            public boolean isEnabled(int position){
               return true;
                      }

            @Override
            public int getCount() {
                return elements.size();
                }
            @Override
            public Object getItem(int position) {
                return elements.get(position);
                }
            @Override
            public long getItemId(int position) {
                return position;
                }
                @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (convertView == null) {
                      LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                      v = vi.inflate(R.layout.rowfor_tourlist, null);
                 } 

                    TextView in = (TextView)v.findViewById(R.id.intro);
                    TextView du = (TextView)v.findViewById(R.id.duration);
                    TextView pf = (TextView)v.findViewById(R.id.price);
                    TextView pn = (TextView)v.findViewById(R.id.product);
                    WebView wv=(WebView)v.findViewById(R.id.photo);  
                    in.setText(Html.fromHtml(mylist.get(position).get("Intro")));
                    du.setText(mylist.get(position).get("Duration"));
                    pf.setText(mylist.get(position).get("Price"));
                    pn.setText(mylist.get(position).get("Product"));
                    wv.getSettings().setJavaScriptEnabled(true);
                    wv.loadUrl(mylist.get(position).get("ImageURL"));

                return v;
            }           
        }//class

and my tourlist.xml file looks like

 <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
            ....
        >

 <ListView android:id="@id/android:list"
           android:layout_width="fill_parent" 
           android:layout_height="fill_parent"
           android:background="@drawable/white"
            android:cacheColorHint="#00000000"
           android:layout_weight="1"

           />
</...>

回答1:


See if this discussion helps...




回答2:


The question is really simple.

You cannot call setOnItemClickListener before setAdapter call because ListView relies on its children and its children are unknown until you set the adapter.

So simply do like this:

public class StatusListView extends ListView implements OnItemClickListener {

    private StatusListAdapter adapter;

    public StatusListView(Context context) {
        super(context);
        init();
    }

    private void init() {
        // do other things
            ...

        adapter = new StatusListAdapter(getContext(), R.layout.status_list_item, statuses);
        // pay attention here
        setAdapter(adapter);
        setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        StatusListAdapter adapter = (StatusListAdapter) ((ListView) adapterView).getAdapter();
        Status status = (Status) adapter.getItem(position);
        Intent intent = new Intent(getContext(), CustomActivity.class);
        intent.putExtra(C.extra_keys.status, status);
        getContext().startActivity(intent);
     }

}  

In this way you will have your event up and running ! Sorry but I didn't time to go in detail in Android source code, if I will I will post again.

Bye guys, Alex



来源:https://stackoverflow.com/questions/2734892/setonitemclicklistener-not-responding-for-custom-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!