1

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.

4
  • Set the onClickListener on the convertView instead Commented Jul 15, 2015 at 13:31
  • Can you expand please. convertView.setOnItemClickListener ? Commented Jul 15, 2015 at 13:35
  • convertView.setOnClickListener doesnt give any errors except I cant seem to run the line startActivity(intent). Any ideas? Commented Jul 15, 2015 at 13:39
  • Try using the context initialized in the adapter constructor Commented Jul 15, 2015 at 13:42

5 Answers 5

2

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

Sign up to request clarification or add additional context in comments.

10 Comments

I have methods running inside my adapter class which returns values I need to pass through the intent. Hence the onClickListener in the adapter class
You can also get the selected content in OnItemClickListener itself by using adapterView.getItemAtPosition(position).
Yes, you can use getItem(position) or getItemAtPosition(position). Basically getItemAtPosition(position) calls the adapter with getItem(position).
What's the difference between Item item = (Item) listview.getAdapter().getItem(position); and Item item = listArray.get(position) where listArray is the array I pass into my adapter
Also if there is not relevant different, I always prefer the first one. If you mantain an external reference to the list inside the adapter you can cause problem. Or if your list is accidentally set to null, you lost your reference, but your list still exsists inside the adapter.
|
0

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

4 Comments

Cannot resolve startActivity. Other than this it seems to be giving no problems
Try with context.startActivity()
It gets rid of the error message, but the app crashes on start-up now
check for null on convertView
0

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

Comments

0

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.

Comments

0

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);

        }
    });

3 Comments

had to declare final... still app crashes on launch though
you can direct change it from your constructor in place of context it change to activity variable/instance and typecast this.
private Context context; is replace with private Activity context; and this.context = context; is replace with this.context = (Activity)context;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.