To get the name of the item which was clicked - android

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

Related

android on click list item i want to select 2 item(tick mark)

I a newbie,i would like to ask on click list view i want to select (tick) mark on particular item,i know how to get position on click item.Here is my code below
public class MainActivity extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get ListView object from xml
listView = (ListView) findViewById(R.id.list);
// Defined Array values to show in ListView
String[] values = new String[]{"FirstName",
"FullName",
"Simple List View In Android"};
ArrayAdapter < String > adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setClickable(true);
// Assign adapter to ListView
listView.setAdapter(adapter);
// ListView Item Click Listener
listView.setOnItemClickListener(new 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();
listView.getAdapter().getView(position, null, null).performClick();
}
});
}
}
Thank you..Please
Check this sample project. It may help.
Use this if you need multiple selections
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
If you need a brief description visit
http://theopentutorials.com/tutorials/android/listview/android-multiple-selection-listview/

how to add action to arraylist item in listview and how to get its position?

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

Android list view the single value of a row

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

Get a field from a ListView

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

ListView items with two rows

This is my ListView structure and element :
// Get a cursor with all phones
Cursor c = getContentResolver().query(Phones.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
startManagingCursor(c);
/** ----Display the Contacts on the device----- */
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, c,
new String[] { Phones.NAME, Phones.NUMBER },
new int[] { android.R.id.text1, android.R.id.text2 });
setListAdapter(adapter);
and my implementation of the setOnItemClickListener is as follows:
/** ----Defining the ItemOnClickListener for the displayed List---- */
final ListView contactlistview = getListView();
contactlistview.setTextFilterEnabled(true);
contactlistview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.d(TAG, String.valueOf(position));
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
contactlistview.getItemAtPosition(position).toString(),
Toast.LENGTH_SHORT).show();
Log.d(TAG, contactlistview.getItemAtPosition(position).toString());
}
});
and finally got the ListView as :
and when i clicked on any of the item i get the Toast Displaying the Following:
but i want my toast to display the contact name of the selected item
can u pls help me with the code :)
sorry for the inconvenience :)
THANKS :) :)
You are printing the value of the object here.
Now try like this:
Cursor c = ((Cursor) parent.getAdapter().getItem(position));
Toast.makeText(getApplicationContext(),
c.getString(Phone.NAME),Toast.LENGTH_SHORT).show();

Categories

Resources