On Item Click Listener for List View extending ArrayAdapter

纵饮孤独 提交于 2021-02-10 20:39:42

问题


What I have tried:

public class EntryAdapter extends ArrayAdapter<Item> {

private Context context;
private ArrayList<Item> items;
private LayoutInflater vi;

public EntryAdapter(Context context,ArrayList<Item> items) {
    super(context,0, items);
    this.context = context;
    this.items = items;
    vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(int position, final View convertView, ViewGroup parent) {

    // // // //  NON-FUNCTIONING CODE BELOW 

    AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent intent = new Intent(getContext(), NewsItemActivity.class);
            convertView.getContext().startActivity(intent);

        }
    };
}

The AdapterView.onItemClickListener doesnt yield any errors but doesnt seem to function whatsoever.

What is the proper way of setting this onClick Listener?

Note: I have to set it in this adapter class, not the main class for my own reasons.


回答1:


You shuldn't implement a listener inside the getView() unless you what set a listener on a particular view inside your row layout.

You should instead use setOnItemClickListener() method on your ListView:

ListView lv = (ListView) findViewById(R.id.your_listview);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
         Intent intent = new Intent(context, NewsItemActivity.class);
         context.startActivity(intent);
     } 
});

EDIT:

If, for each action inside the onclick, you need information that resides in your Objects (Item) then you can get it in this way:

Item item = (Item)listview.getAdapter().getItem(position);

from inside of onItemClick() method




回答2:


You can use:

convertView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            //Make what you want
        }
    });

Or depending on your item view, you can make the OnCLickListener on your global layout of the item or a specific item's layout




回答3:


Try by this code :-

Spinner cb_tv;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> dataAdapter;
list.add(phone1);
list.add(name1);
dataAdapter = new ArrayAdapter<String>(Display_Tank.this,android.R.layout.simple_spinner_item, list);
cb_tv.setAdapter(dataAdapter);

cb_tv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub  
            Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_SHORT).show();
        }
    });

at the place of cb_tv Spinner You can use ListView




回答4:


First of all, the best way to achieve this is to use your own ViewHolders in ListView.

This is an example of a custom ViewHolder

class MyViewHolder extends ViewHolder implements View.OnItemClickListener {
    public MyViewHolder(View view) {
        /* Implement your views here
         * like this: mTextView = (TextView) view.findViewById(R.id.myId);
         */
         view.setOnItemClickListener(this);
    }

    @Override
        public void onClick(View view) {
            Intent intent = new Intent(getContext(), NewsItemActivity.class);
            convertView.getContext().startActivity(intent);
        }
    }

And on your getView method you create this ViewHolder and populate your Views with your data.




回答5:


You can try this way... it is working in my case Change your context to
Activity activity = (Activity) context;

       convertView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
             Intent intent = new Intent(activity, NewsItemActivity.class);
             activity.startActivity(intent);

        }
    });


来源:https://stackoverflow.com/questions/31431802/on-item-click-listener-for-list-view-extending-arrayadapter

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