How to Get the Selected Item from Listview

Get single listView SelectedItem

Usually SelectedItems returns either a collection, an array or an IQueryable.

Either way you can access items via the index as with an array:

String text = listView1.SelectedItems[0].Text; 

By the way, you can save an item you want to look at into a variable, and check its structure in the locals after setting a breakpoint.

How to get the item selected from ListVIew

thks to Inder Kumar Rathore and DJ Burb to guide me to find the answer.
so, here is

Segment seg= ListView.SelectedItem as Segment;
if (seg!= null)
{
tbckChoiceSelected.Text = compa.CompanyName;//TextBlock called bckChoiceSelected
}

it was very easy. again, thnks.

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.

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.

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.

Get selected item inside listview

You can disable the list view selection by setting isEnabled property to false .This will make listview unclickable while GestureRecognizers on ViewCell will respond as expected

For Navigation this can be of help

How can i get all selected item(s) from ListView in ViewModel class?

To get selected items into the ViewModel, first create a property of bool type in your model that will be bound with IsSelected property of ListViewItem.

Property in Model class:

 public bool IsSelected
{
get { return isSelected; }
set
{
isSelected = value;
RaiseChange("IsSelected");
}
}

XAML Style:

 <ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
</Style>
</ListView.ItemContainerStyle>
</ListView>

Final Property in ViewModel:

 public List<DataGridItem> SelectedItem
{
get
{
return list.Where(item=>item.IsSelected).ToList();
}
}

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.



Related Topics



Leave a reply



Submit