Android Listview Get Selected Item

android listview get selected item

final ListView lv = (ListView) findViewById(R.id.ListView01);

lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
String selectedFromList =(String) (lv.getItemAtPosition(myItemInt));

}
});

I hope this fixes your problem.

Getting the values of ListView selected item

Change this code to setters.

upick.Subject_Name = jsonObject.getString("id_servicio");
upick.Subject_Full_Form = jsonObject.getString("cliente_servicio");

eg:

upick.setSubjectname(jsonObject.getString("id_servicio"));

Then inside onclick you can take values using getters

String item = upicks.get(position).getSubjectname();

parent.getItemAtPosition(position) will return an Object (the model used in your adapter).To get some field from your Object don't call Object.toString(); it will return the object reference not the value that you're looking for. Use Object.yourField; instead.

How to get the position of a selected item in ListView, onItemClickListener

I guess you have a ListView which you want to add listener to. In that case, in onItemClick(AdapterView adapterView, View view, int i, long l) method, third parameter int i holds the position.

Edit:
If you want to use int i in another activity, the easiest way is to declare a static class variable in the same activity where the setOnItemclick is. For example:

class MainActivity extends Activity{
// variable accessible from anywhere in your package
static int global_int;

// other usual code...

// your existing code
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String selected_item= String.valueOf(adapterView.getItemAtPosition(i));
position=(Integer)List.getTag(); //without this line it doesn't crash
Toast.makeText(Main2Activity.this,"Please Log-In"+selected_item+"Thesi :"+String.valueOf(position),Toast.LENGTH_SHORT).show();
Intent toy = new Intent(Main2Activity.this,Main3Activity.class);
startActivity(toy);

// saving "i"
global_int=i;
}
});

}

Then from any other place in your code, other activity, etc. you access it like this:

int get_global_int=MainActivity.global_int;

There are other ways like saving it in SharedPreferences, but this is the easiest one.

How to get value(s) of selected Item in listview - Android

You could do something like this below:-

ContactsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

ThreeStrings threeStrings = (ThreeStrings)ContactsListView.getItemAtPosition(position);

Log.i("Item", "Selected: " + threeStrings.getCentre());
}
});

Get selected item from customadapter listview Android

Try this..

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

TextView textview1 = (TextView) v.findViewById(R.id.text1);
TextView textview2 = (TextView) v.findViewById(R.id.text2);

Log.v("textview1",textview1.getText().toString().trim());
Log.v("textview2",textview2.getText().toString().trim());

}

Get element from the selected item in the listView

Understanding your case, getItemAtPosition(position) is returning you an Object.

The answer posted by @Charuka Silva might not be working for you as you may have used hashmap object something like HashMap<String,String> obj = new HashMap<String,String>();

So, the solution is to use HashMap again for getting the returned object and using get("key") function to retrieve the value associated with it. I think this is what you want:

listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Use HashMap here
HashMap<String, Object> member = (HashMap<String, Object>) parent.getItemAtPosition(position);

//Use obj.get("key") here to retrieve the value associated with the "key"
Toast.makeText(getApplicationContext(),"member.get("uname"), Toast.LENGTH_LONG)
.show();
}
});

I had a similar problem in my Minor Project and an answer by @user1068963 helped me. Adding Link.

Hope this helps.

Android ListView - get the selected item

I figured out that the OnItemSelected method is never/rarely called on touch devices.
It fires up if you use the emulator cross-navigation and on special devices with hybrid or non-touch control. It is only called if you scroll through the list but not if you click on it.

Thats why you should use the OnItemClickListener on a general tablet/smartphone with touch control.

Get Selected Items from ListView

Extend ArrayAdapter and override getView method where you need to inflate your own layout. So it'll contain as much switches as you need. Then each switch can implement its own OnCheckedChangeListener so you'll exactly know which switch was changed.

EDIT

Create a new class CustomArrayAdapter that extends ArrayAdapter and override getView.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Inflate view
LayoutInflater inflator = mContext.getLayoutInflater();
View view = inflator.inflate(R.layout.list_item_topic, null);

switch = (Switch)view.findViewById(R.id.any_switch);
switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub

}
});

// TODO bind other views or anything else you need
return view;
}


Related Topics



Leave a reply



Submit