user3691657
user3691657

Reputation:

How to get item value in a listview in android?

I have referenced to a tutorial and I have created a custom list view in fragment, but I am unable to get the position or value of any item on item click of list view. I want to show a toast containing the position of the item. I don't know how to implement that.

CustomListAdapter.java

public class CustomListAdapter extends BaseAdapter{

    private ArrayList<MyActivityAdapterItem> listData;
    private Context context;

    //private LayoutInflater layoutInflater;

    public CustomListAdapter(Context context, ArrayList<MyActivityAdapterItem> listData) {
        this.listData = listData;
        this.context = context;

    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        //ViewHolder holder;
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater)
                    context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.list_row_layout, null);
           // holder = new ViewHolder();

            //convertView.setTag(holder);
        } 
        TextView headingView = (TextView) convertView.findViewById(R.id.title);
        TextView placeView = (TextView) convertView.findViewById(R.id.place);
         TextView reportedDateView = (TextView) convertView.findViewById(R.id.date);

        headingView.setText(listData.get(position).getHeading());
        placeView.setText("At, " + listData.get(position).getPlace());
        reportedDateView.setText(listData.get(position).getDate());

        return convertView;
    }

}

FindPeopleFragment.java

public class FindPeopleFragment extends Fragment implements OnClickListener {
    AlertDialog.Builder builder ;
    ListView lv1;
    ImageButton delall;
    CharSequence options[] = new CharSequence[] {"Delete"};
    ArrayList details;
    CustomListAdapter ad1;
    String msg="abc";
    public FindPeopleFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
        delall=(ImageButton) rootView.findViewById(R.id.deleteall);
        details = getListData();
        lv1 = (ListView) rootView.findViewById(R.id.activitylist);
        ad1=new CustomListAdapter(getActivity(), details);
        lv1.setAdapter(ad1);
        builder= new AlertDialog.Builder(getActivity());
        delall.setOnClickListener(this);
       lv1.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

            //Toast.makeText(getActivity(), , Toast.LENGTH_LONG).show();
            /*Bundle args = new Bundle();
            args.putString(MyActivitiesFragment.MSG, msg);
            MyActivitiesFragment f1=new MyActivitiesFragment();
            f1.setArguments(args);
            MainActivity.fm.beginTransaction().replace(R.id.frame_container,f1).commit();*/
        }
    });
return rootView;
}

Upvotes: 4

Views: 32550

Answers (4)

yassino
yassino

Reputation: 11

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
         //   Intent i1 = new Intent(getApplicationContext(), Plaeeng.class);
         //   List<ScanResult> wifiList;
         //   i1.putExtra("cuor", );
         //   startActivity(i1);
           Toast.makeText(ListCours.this, "=>"+parent.getAdapter().getItem(position), 5000).show();
        }
    });

Upvotes: 1

G.T.
G.T.

Reputation: 1557

The position of the clicked item in a ListView can be retrived easily on the onItemClick method as you can see in the documentation:

public abstract void onItemClick (AdapterView parent, View view, int position, long id)

Callback method to be invoked when an item in this AdapterView has been clicked.

Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.

Parameters parent The AdapterView where the click happened. view The view within the AdapterView that was clicked (this will be a view provided by the adapter) position The position of the view in the adapter. id The row id of the item that was clicked.

So in your case, the position of the clicked item is the arg2 value. Your code should be like this:

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    Toast.makeText(getActivity(), arg2+"", Toast.LENGTH_LONG).show();
    /*...*/
}

EDIT:

To get the Item, you can do like this:

(Item) arg0.getAdapter().getItem(arg2);

TL;DR

You can see in this case that it is important to use good names for parameters. With the default names given by Eclipse - or by others IDEs - this line:

(Item) arg0.getAdapter().getItem(arg2);

looks pretty strange. But if you are using the Android names - which gives you onItemClick(AdapterView<Item> parent, View view, int position, long id) - the line becomes:

(Item) parent.getAdapter().getItem(position);

Which is clearer and easier to use.

Upvotes: 2

Guy S
Guy S

Reputation: 1424

this should work:

lv1.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        Toast.makeText(getApplicationContext(), "Selected item at position: " + position, Toast.LENGTH_LONG).show();
    }
}

Upvotes: 1

Javier
Javier

Reputation: 1685

You are doing well in setOnItemClickListener the problem its the autocomplete its not working well in parameters you should have something like this: lv1.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub

        }
    });

the position is in your arg2, and as a tip you can get the item you click using this snippet:

parent.getAdapter().getItem(position);

Upvotes: 0

Related Questions