7779

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

Answer1:

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(); /*...*/ }

<strong>EDIT:</strong>

To get the Item, you can do like this:

(Item) arg0.getAdapter().getItem(arg2); <hr>

<strong>TL;DR</strong>

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.

Answer2:

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

Answer3:

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

Answer4:

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

Recommend

  • Commandlink action does not work after an ajax call JSF-2.0
  • Error inflating class Fragment when testing fragment using DataBinding
  • Problem in opening Sub Activity of ActivityGroup
  • How to get a txt file from a jar in tomcat7
  • GLSL textureCube and texture2D in same shader
  • How to get the #fragment identifier part of an URL
  • Limited access in 2nd fragment of FragmentPagerAdapter
  • To use (DataContext) or not to use
  • I got MISSING_PERMISSION issue Here maps Android
  • split with no argument in perl
  • View Pager - slide to next page programmatically with static variable
  • Fancybox 2.1.3 preventing iframe from haivng scrollbars
  • Update Fragment UI with data passed from Activity
  • Script-based sorting on Elasticsearch date field
  • Parse url hash fragment in express application
  • Correct way to start RSpec-puppet unit tests
  • Android Fragment Interaction
  • Using a ListFragment with a SimpleCursorAdapter with the Holder Pattern
  • Navigation drawer is always inflated
  • Why is this code not working? Hangman
  • How can I detect errors in an HTML document fragment with Ruby?
  • Running a Service from ASyncTask or Thread?
  • Back press fragments issue in android
  • UIScrollView setContentOffset: animated: not working
  • What does 'Language neutral' mean with regard to MAKELANGID?
  • Excel's Macro-Recorder usage
  • How to use carriage return with multiple line?
  • Row Count Is Returning the incorrect number using RaptureXML
  • OpenGL 3.3 on Mac OSX El Capitan with LWJGL
  • MongoDB in PHP using aggregate to group by _id is null not working
  • QLineEdit password safety
  • Display issues when we change from one jquery mobile page to another in firefox
  • Illegal mix of collations for operation for date/time comparison
  • Release, debug version and Authorization Google?
  • Weird JavaScript statement, what does it mean?
  • To display the title for the current loaction in map in iphone
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • trying to dynamically update Highchart column chart but series undefined
  • Hits per day in Google Big Query
  • need help with bizarre java.net.HttpURLConnection behavior