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();
Related
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);
I have listview that binds from sqlite and groups by KEY_TITLE(field) so i need to get the name of the item that is clicked
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
Intent i=new Intent(this,detail.class);
i.putExtra(DatabaseIN.KEY_TITLE,SOMETHING THAT I NEED!);
//startActivity(i);
startActivityForResult(i,ACTIVITY_EDIT);
}
// maybe something like this string selectedIteme = l.getSelectedItem().getSOMETNIG?
check this tutorial might help you
http://www.vogella.de/articles/AndroidListView/article.html
you can pick the selected item value like this
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Cursor c = (Cursor) arg0.getAdapter().getItem(arg2);
Intent i=new Intent(getApplicationContext(),Xyz.class);
i.putExtra("abc", c.getString(1));
startActivity(i);
}
Try this in your onClick method..
Map<String, String> selection = (Map<String, String>) listView.getItemAtPosition(position);
String count = selection.get("count");
String title = selection.get("title");
I have responceid (String responceid = Activity2.getData()) for every row ...I want when I click on any position of list item it show its corresponding responceid ...so how to bind responseid for every row ....responceid takes from database
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
String responceid = Activity2.getData();
Object o = (String) (Notepadv3.this).getListAdapter().getItem(position);
Toast.makeText(this, "this row responce id is= " + " " + , Toast.LENGTH_LONG).show();
}
If you have used an adapter to fill the listView then use getItemId() to retrieve the database id.
Salil
Android:
Hello, i am trying to get index of the item that is clicked but i don't know how i can do it.
I want if i click on any item in listview it will open SmsManager with number of this item(Phone Number).
Can somebody tell me how i can do it?
There is source code:
list1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
SmsManager m = SmsManager.getDefault();
Uri uriSMSURIs = Uri.parse("content://sms/inbox");
Cursor cc = getContentResolver().query(uriSMSURIs, null, null, null, null);
String phoneNumber = cc.getString(cc.getColumnIndex("address"));
m.sendTextMessage(phoneNumber , null, phoneNumber , null, null);
}
});
The "index of the item witch [sic] is clicked" is the position parameter supplied to onItemClick().
Implement
onListItemClick
#Override
protected void onListItemClick(ListView l, View v, final int position, long id) {
super.onListItemClick(l, v, position, id);
Log.i("the Item clicked is :: ", position);
}
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.