2

I have class

public class User
{
   UUID id;
   String name;
}

I want to pass into ListView this: List and display only "name". But when item in list view will be selected I want to know "id" of the selected item.

1
  • Please clarify how you want the id of the selected item to be displayed. Commented Sep 5, 2012 at 16:49

2 Answers 2

4

First I would make a class that contains both pieces of information.

public class MyItem {

public final String name;
public final String id;

public MyItem(String name, String id) {
    this.name = name;
    this.id = id;
 }

}

Then make a custom Adapter that can support this class.

public class CustomAdapter extends ArrayAdapter<MyItem> {

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

public EntryAdapter(Context context,ArrayList<MyItem> 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, View convertView, ViewGroup parent) {
    View v = convertView;

    final Item i = items.get(position);
    if (i != null) {
            v = vi.inflate(R.layout.list_item, null); //custom xml for desired view
            //do what ever you need to

    }
    return v;
     }

     }

Populate an Array of MyItem's, then crete your custom Adapter using the Array.

items = new ArrayList<MyItem>(); // then populate it
myAdapter = new CustomAdapter(this, items);

Now set up the ListView and use OnItemClickListener

myList.setAdapter(myAdapter);

    myList.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parentView, View view, int position,
                long id) 
        {
                ((MyItem)myAdapter.getItem(position)).id); //this line gets you the id of the clicked item

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

2 Comments

Why do we need: UserSearchItem method?
Whoops, meant to be a constructor, let me fix.
1

store the object into an arraylist say like

Arraylist<User> al = new ArrayList<User>();

then add the objects to the arraylist using

 User user = new User(); 
 user.id = somevalue;
 user.name = someNameValue;
 al.add(user);

Next in the onitemclick() get the item clicked and next display it using the arraylist item

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

Toast.makeText(this, "item clicked is " + arg2+al.get(arg2).id+"  "al.get(arg2).name,     
 Toast.LENGTH_SHORT)
 .show();
}
}

4 Comments

How to pass this ArrayList into ListView?
you can use listView.setAdapter(adapter); Where adapter can be adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, al); Have a look at codinglookseasy.blogspot.in/2012/07/… or codinglookseasy.blogspot.in/2012/07/custom-list-view.html they may be helpful
al - is the array with our objects?
Yes al is the array with my objects

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.