I have an Activity with ListView took in my Database. I would like to, when I select one, get the id field.
lv = (ListView) findViewById(R.id.listMessageConversationView);
Cursor c = selectInfoInDB();
int[] to = new int[] {R.id.idMessageClavier, R.id.nomMessageClavier, R.id.valeurMessageClavier, R.id.groupeMessageClavier, R.id.occurrenceMessageClavier};
SimpleCursorAdapter sCA = new SimpleCursorAdapter(this, R.layout.conversation_clavier_display_data, c, SmartAccess_v1Activity.nomColonnesMessage, to);
lv.setAdapter(sCA);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView textView = (TextView)findViewById(R.id.idMessageClavier);
}
});
The fields in the ListViews are correct, but the toast give me the id of the first item of ListView, wherever I touch on list.
I worked on that and i I can't figure where is the mistake -_-
Thanks for your help, korax
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int postion,
long id) {
Toast.makeText(this, "id is :: " + id +"position :: " + position,
Toast.LENGTH_SHORT).show();
}
});
Related
I have a ListView in my Android app, which it has some item with exactly same
name. How can distinguish which one is clicked? Is there any solution?
For example, I have retrieved these rows from database:
How do I send their ID and receive back their ID by selecting the name in ListView?
(I just want to display item's name)
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// use the position to distinguish the view clicked
});
}
With the position is:
plansList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int intPosition = position;
String clickedValue = plansList.getItemAtPosition(intPosition).toString();
}
});
String[] locations = {"russia", "ukraine", "brazil"};
locationList = new ArrayList<String>(Arrays.asList(locations));
search_listview_result = (ListView)findViewById(R.id.search_listview_result);
searchResultListViewAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_2, android.R.id.text1, locationList);
search_listview_result.setAdapter(searchResultListViewAdapter);
search_listview_result.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SharedPreferences.Editor editor = getSharedPreferences("SelectedLocation", MODE_PRIVATE).edit();
editor.putString("Name", locationList.get(position));
editor.putString("Lat", String.valueOf(locationLatList.get(position)));
editor.putString("Long", String.valueOf(locationLongList.get(position)));
editor.commit();
finish();
}
});
Hi I am working with android.I had created a listview using array list. Now how can I add Action to each list item and also how to get the list position of that item. Please help me, thanks
here is my code
public class MainActivity extends Activity {
private ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv=(ListView)findViewById(R.id.listView1);
ArrayList<String> list=new ArrayList<String>();
list.add("aaaa");
list.add("bbbb");
list.add("cccc");
list.add("dddd");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
list);
lv.setAdapter(arrayAdapter);
}
}
You need to implement ListView setOnItemClickListener
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
}
});
For more information go to Documentation.
you can also get the selected item of ListView in setOnItemClickListener using following code:
listView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
int itemPosition = position;
String itemname = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), "Item: " + itemname + " is at position: " + itemPosition, Toast.LENGTH_LONG).show();
}
});
As the listview cells position and the index of arraylist are same. So we can easily get the item from arraylist by the position of cell of listview.
listView1.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id)
{
int position=position;
String item=marraylist.get(position).toString();
}
}
My current problem is I have a ListView I'm using to display my contacts now I'd like to set the onItemClick to grab the name of the contact and set that to a TextView called p2 the code I currently have is:
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.setp2);
cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
String[] from = new String[]{ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME};
int[] to = new int[]{R.id.id_text,R.id.name_text};
final SimpleCursorAdapter adapter = new SimpleCursorAdapter(setp2.this,R.layout.setp2_layout,cursor,from,to);
ListView list = (ListView)findViewById(R.id.contact_list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
}
});
}
So basically I'm unsure of what I need to do in the onItemClick, any help appreciated!
In your onItemClick, you will get the instance of the view associated with that list item.From that view you can get the text associated with it and set it to the p2 TextView. If my understanding of your requirement is correct, the code will be something like this:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
CharSequence name = ((TextView)view.findViewById(R.id.name_text)).getText();
//Set this to your textview in P2
tvP2.setText(name);
}
});
Set up a SimpleCursorAdapter.CursorToStringConverter and set it on the adapter, then use convertToString(Cursor cursor) to get the text in on item click :
(Cursor) simple.getItem(position)
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
cursor.moveToPosition(position);
String name = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
p2.setText(name);
}
});
I have written the code to get the data from database and to bind it to listview now I want to click on perticular item from the listview and to get the name of that item which was clicked.
Cursor cursor = dbHelper.fetchAllRecords();
String[] columns = new String[] {
RecordsDbAdapter.KEY_NAME,
RecordsDbAdapter.KEY_BIRTHDAY,
};
int[] to = new int[] {
R.id.name,
R.id.birthdate,
};
dataAdapter = new SimpleCursorAdapter(
this, R.layout.row,
cursor,
columns,
to);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),"cominggggg", Toast.LENGTH_SHORT).show();
}
});
}
Toast.makeText(getApplicationContext(), "Click ListItem Text "
+ ((TextView) view.findViewById(R.id.Txt))
.getText().toString(), Toast.LENGTH_LONG).show();
You can display the toast with selected item name
edit ur code like this:--
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
TextView textView = (TextView) view;
Toast.makeText(getApplicationContext(),textView.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
is possible get the value of a single row.
In partycular i wont get in a string the value of id in my row of mt list with adapter.
In each row there are value id and label
Thaks a lot and sorry for my english
ListView list;
list = (ListView) findViewById(R.id.listView1);
mSchedule = new SimpleAdapter(getApplicationContext(), result ,R.layout.row_meteo,
new String[]{"id", "label"}, new int[] {R.id.textView1,R.id.textView2});
list.setAdapter(mSchedule);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String idPlace = mSchedule.getItem(position).toString();
Toast.makeText(getApplicationContext()," Cliccato sul luogo :"+idPlace,Toast.LENGTH_LONG).show();
}
}
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView tv = (TextView)view.findViewById(R.id.textView1); //<---------
String str = tv.getText();
}
Replace
String idPlace = mSchedule.getItem(position).toString();
with
String idPlace =((TextView)view.findViewById(R.id.textView1)).getText().toString();