protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
int selection = position;
switch (selection)
{
case 0:
{
String toast=" 1 clicked";
DisplayToast(toast);
}
break;
case 1:
{
String toast=" 2 clicked";
DisplayToast(toast);
}
break;
}
}
This is the code I am using for my OnListItemClick() in List Activity.
My Problem is my List item is populated dynamically and hence I don't know how many items would be there in the list.
I cannot use switch statement in that case.So. How can I distinguish which item was clicked in a dynamically changing list.
Set an onItemClickListener like this:
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
Toast.makeText(MainActivity.this, adapter.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
}
});
So you can get the text of your currently selected item by getItemAtPosition(position).toString().
Or if you make your own ArrayAdapter, you can implement the getItem(position) method, which can return with anything about your adapter item.
This will get the Text from the selected textview and display it in a Toast.
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
String toast= ((TextView)v).getText();
DisplayToast(toast);
}
Following is the code
String toast = " " + ++position + " clicked";
DisplayToast(toast);
Related
Is there any way I can capture a name of an item clicked in a list view when using "onItemLongClickListiner" ? I know I can capture position etc but I need a name
I suppose that you ListView is filled up with String object:
public boolean onItemLongClick (AdapterView<?> parent, View view, int position, long id) {
String name = (String) parent.getItemAtPosition(position);
}
AdapterView.getItemAtPosition(position) gets the data associated with the specified position in the list.
You can use lv.getItemAtPosition(position) to get the text based on the index position
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position,
long arg3)
{
String name = arg0.getItemAtPosition(position).toString()
}
});
For more info
getItemAtPosition
Try this..
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//inflated row layout textview
TextView tagText = (TextView) view.findViewById(R.id.txt_text);
String tag = tagText.getText().toString();
Toast.makeText(getApplicationContext(),
"Element Name " + tag + " Clicked", Toast.LENGTH_SHORT).show();
}
});
It has worked for me.
listtview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l){
String selected = (String) adapterView.getItemAtPosition(i);
Toast.makeText(getApplicationContext(),"This is"+selected,Toast.LENGTH_LONG).show();
}
});
Im having problems with deleting items from listview(and database). So far i have followed this example:
http://www.vogella.com/articles/AndroidSQLite/article.html but i dont like the delete there (button that always delets first one).
Here is my activity class:
public class FirstActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
final ShoppingSQL shoppingSQL = new ShoppingSQL(this);
List<ShoppingData> list = shoppingSQL.getAll();
ArrayAdapter<ShoppingData> adapter = new ArrayAdapter<ShoppingData>(
this, android.R.layout.simple_list_item_1, list);
setListAdapter(adapter);
this.getListView().setLongClickable(true);
this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
Log.w("DELETED", " DELETED");
shoppingSQL.delete((int)id);
return true;
}
});
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Log.w("CLICKED", "CLICKED");
}
}
As you can see i have the listner for long clicks set up and also the delete method that requires an ID. The probelm is with the ID, the one im giving it at the moment seems to be just the order number (0, 1, 2, 3) - not the actual id in db. So, my question is how do i get the real id?
You Can get your Id of Shoping data by
this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
Log.w("DELETED", " DELETED");
//here You can get your id by
list.get(position).getID();
//getID id the getter method of shoppingdata ,here you can declare your method for ID as whatever you have in shopping data
shoppingSQL.delete((int)list.get(position).getID());
return true;
}
});
For delete items
adapetr.remove(list.get(i));
then call notifydatasetchanged on listview
You can get the item that is selected from the ListView and then check its ID from the List as follows,
String selectedItem =(String) (MyListView.getItemAtPosition(position));
int ItemID = list.indexOf(selectedItem);
shoppingSQL.delete(ItemID);
How to disable list view on item click in particular row in android.if value is search i have to disable the onitem click event of particular row in list view in android can anybody tell how to do?
try this code
mListView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,long arg3)
{
if(your condition)
{
//your click event code
}
else
{
//nothing
}
}
});
you for comple row
onListItemClick(AdapterView<?> l, View v, int position, long id) {
if(Clickable condition ){
super.onListItemClick(l, v, position, id);
// your code .........
}else{
}
}
i create a ListView with content from a Database.
I want to get the Text from one Item with:
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String item = l.getItemAtPosition(position).toString();
}
But i get some text: "android.database.sqlite.SQLiteCurser#45765"
Can anyone please help me?
What exactly you retrieved from the database. If it's only set of Strings you can try the below code.
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG).show(); }
this should work...
On onItemClick :
String text = parent.getItemAtPosition(position).toString();
i have a listview from webservice and i want to get the index and textview of the selected item .my list view is
company symbol
android an
iphone ip
blackderry bb
. .
. .
. .
so tell me how to get the index and textview of symbol in the listview from webservice.i used two methods
firstmethod:
protected void onListItemClick(ListView l, View v, int position,long id)
{
super.onListItemClick( l, v, position, id);
ID = id;
int selectedPosition = l.getSelectedItemPosition();
index=String.valueOf(selectedPosition);
Log.e("index value","index"+index);
TextView txt=(TextView)findViewById(R.id.symboltext);
TextView temp=(TextView)v;
txt.setText(temp.getText());
Log.e("text",txt.toString());
}
in above code the logcat shows index is -1 and could not shows the txt.
Second method:
listview.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView parentView, View childView, int position, long id)
{
String text = ((TextView)childView.getText()).toString();
//The above text variable has the text value of selected item
// position will reflect the index of selected item
}
public void onNothingSelected(AdapterView parentView)
{
}
});
in above method the listview didnt get onclick action. so please tell me how to get the index and textview of the selected item in listview.
Best Regards
Thanks in advance
Try this code. And Check your logcat. It should get you the desired result.
protected void onListItemClick(ListView l, View v, int position,long id)
{
super.onListItemClick( l, v, position, id);
ID = id;
Log.e("index value","index"+position);
TextView txt = (TextView) v.findViewById(R.id.symboltext);
Log.e("text",txt.toString());
}
you can use a hashmap to put your string to a listview
this link will help you to understand how hashmap works and how the strings are saved and retrived from it
I was trying something like this, on a Custom ListView:
My scenario was:
To fetch three Objects from Server with JSONArrayRequest named as:
ID (tvLvDocID)
Title (tvLvDocTitle)
Sub Title (tvLvDocSub Title)
ID is actually the ID of the column of item in MySQL database.
What I did was:
listViewDoc.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int myPosition = position;
String id_doc_from_lv = listViewDoc.getItemAtPosition(myPosition).toString();
Toast.makeText(getApplicationContext(), "ID is : " + id_doc_from_lv, Toast.LENGTH_LONG).show();
}
});
} catch (Exception e) {
e.printStackTrace();
}
But My problem was that it was returning Title of the Custom ListView.
And I wanted only and only the ID, which I was also showing in ListView above Title. (Its another story I will later apply INVISIBLE property to that ID).
As I wanted only ID. So I did toke help from this answer:
When I add these lines in my code:
String tv_ID_Str= ((TextView)view.findViewById(R.id.tvLvDocID)).getText().toString();
And call this tv_ID_Str as:
Toast.makeText(getApplicationContext(), "ID is : " + tv_ID_Str, Toast.LENGTH_LONG).show();
Aha! Now I can successfully show tvLvDocID and I can retrieve all the information from database on this ID.
Final code looks like this:
listViewDoc.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int myPosition = position;
String id_doc_from_lv = listViewDoc.getItemAtPosition(myPosition).toString();
// selected item
String tv_ID_Str = ((TextView)view.findViewById(R.id.tvLvDocID)).getText().toString();
Toast.makeText(getApplicationContext(), "ID is : " + tv_ID_Str, Toast.LENGTH_LONG).show();
}
});
} catch (Exception e) {
e.printStackTrace();
}
Purpose of my answer is people like me, who spend lot of time to do this. I hope this will help someone.