I'm having some trouble getting the correct id value when clicking on the listview.
I made a custom adapter since I'm drawing from SQLite data, I get the right text back but when I click on a row, I get a 0 index id back, which is not what I want.
I'm also using Sugar ORM, so I'm not sure if that requires some special work from me to get the id field.
I'm not sure what I should show you in terms of code, so whatever you think will help you, let me know and I'll post it.
Thanks
BoardArrayAdapter adapter = new BoardArrayAdapter(this, boards);
setListAdapter(adapter);
ListView lv = getListView();
registerForContextMenu(lv);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "Board ID " + id, Toast.LENGTH_SHORT).show();
}
});
You need to override the getItemId() method in your adapter.
According to the SugarORM docs you can call getid()to return the db id.
ArrayList<Object> data;
#Override
public long getItemId(int position) {
return data.get(position).getid();
}
This is how I am using on my project (with SQLite and CustomListView).
getData method
public void getData(View view, long id)
{
//storing the ID into a public static variable and converting to int
CLIENTE_ID = (int)id;
//When the user touches on the field in the listview, I get the touched field's name and email
TextView textViewnome = (TextView) view.findViewById(R.id.tv_cliente_nome);
TextView textViewemail = (TextView) view.findViewById(R.id.tv_cliente_email);
//Stores both name and email of the touched field
String nome = textViewnome.getText().toString();
String email = textViewemail.getText().toString();
//store into static variable
CLIENTE_NOME = nome;
CLIENTE_EMAIL = email;
}
within onCreate:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
getData(view, id);
Log.d("myClass", "ID: " + view.getId());
}
});
I hope it helps! :)
Related
I have a ListView that I populate using the FirebaseListAdapter. Now I want the OnItemClickListener to show the value of the item clicked (the text displayed on the item) in a Toast.
ListView listView;
ListAdapter listAdapter;
mFirebaseRef = new Firebase("https://<firebase root ref>/posts");
mFirebaseQuery = mFirebaseRef.orderByChild("author_id").equalTo(id); //`id` is a variable defined earlier
listAdapter = new FirebaseListAdapter<Post>(this, Post.class, android.R.layout.simple_list_item_, mFirebaseQuery) {
#Override
protected void populateView(View view, Post post) {
String postTopic = post.getTopic();
((TextView) view.findViewById(android.R.id.text1)).setText(postTopic);
}
};
listView = (ListView) findViewById(R.id.postsList);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String topic = String.valueOf(parent.getItemAtPosition(position));
Toast.makeText(getApplicationContext(), "List Item Value: "+topic, Toast.LENGTH_LONG).show();
}
});
Instead of returning the text displayed on the item (as is in the case an ArrayAdapter is used), String.valueOf returns some reference to the model class I use - Post.class.
The output is something like this: com.example.android.model.Post#a1e1874
Can anybody tell me how to get the value of the list item in this case?
AdapterView.getItemAtPosition() returns a Post object, so:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Post post = (Post)parent.getItemAtPosition(position);
String topic = post.getTopic();
Toast.makeText(getApplicationContext(), "List Item Value: "+topic, Toast.LENGTH_LONG).show();
}
Hi i am new to android programming, i have make an Http post request to get json data from an external sql database and displayed my result in a lisView. i want to be able to retrieve the string value from a clicked item in the listView. Please any help with this will be much appreciated
I would try something like this, which has worked for me in the past:
String itemValue = (String) listView.getItemAtPosition(position);
This is from within the listView.setOnItemClickListener(new OnItemClickListener() portion of your code.
If your strings are aggregated into one string, then try this:
//Let itemValue = "item1 item2 item3" for example:
String[] parts = itemValue.split(" ");
String part1 = parts[0]; // item1
String part2 = parts[1]; // item2
Set OnItemClickListener on listview. See on below.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
String itemString=listView.getSelectedItem().toString();
}
});
Enjoy!!!...
try this:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String itemString=listView.getSelectedItem().toString();
}
});
I have a listview populated with items retrieved from database (mysql). all is working fine and data is showing as required. Now is the time for the last step: when an item on the table is clicked, it should load more details on that item in next page. This is simple enough I believe. If I can get the id of the element based on database table, then I can query on next screen and load data easily.
The problem is how can I get the id of the item as it is on the mysql table? below is my adapter and list code:
this is the adapterview method that does most of the work:
#Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
if(arg1==null)
arg1 = layoutInflator.inflate(R.layout.simplerow ,arg2, false);
TextView name = (TextView)arg1.findViewById(R.id.nameTxtView);
TextView rate = (TextView)arg1.findViewById(R.id.rateTxtView);
//name.setText(discountObjectArray[arg0].name + "---> " + discountObjectArray[arg0].location + "---> " +discountObjectArray[arg0].rate);
String tempName = discountObjectArray[arg0].name;
tempName = ellipsize(tempName, 6);
name.setText(tempName);
rate.setText(discountObjectArray[arg0].rate);
return arg1;
}
public static String ellipsize(String input, int maxLength) {
if (input == null || input.length() <= maxLength) {
return input;
}
return input.substring(0, maxLength-1) + "...";
}
}
and this is the list code displaying a simple toast:
discountListingListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
RelativeLayout tempParent = (RelativeLayout) view;
TextView t = (TextView) tempParent.findViewById(R.id.nameTxtView);
Toast.makeText(getBaseContext(), t.getText(), Toast.LENGTH_LONG).show();
}
});
I am pretty new to android and appreciate your support
Keep the database id as an attribute of discountObjectArray element. Then in your adapter override this method:
#Override
public long getItemId(int position) {
return discountObjectArray[position].id;
}
Now the database id will be available as an "id" argument of onItemClick method.
You should use to ArrayList or List on your ArrayAdapter.. This;
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String Name = discountObjectArray[position].name;
Toast.makeText(getBaseContext(), Name, Toast.LENGTH_LONG).show();
}
});
i have a listview. i have to get the text of the list item which i clicked. i tried using onitemclick. but i am not able to get. can some 1 please explain me how to get it.
here is my onitemclick method
lstcities.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
// String str =(String) lstcities.getItemAtPosition(position);
String S =view.getContext().toString();
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + S, Toast.LENGTH_LONG)
.show();
thanks
ramu
try this,
setListAdapter(new ArrayAdapter<String>(this, R.layout.yourlayout,your list));
ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
String selectedValue = (String) getListAdapter().getItem(position);
Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();
}
});
Assuming you are using a TextView, you should be able to do
String text = parent.getItemAtPosition(position).getText().toString()
You can get string from ArrayList used in adapter.
like : String str = arrayList[position]; //Assume arrayList here is String[];
If your using customize view(own xml) for list item. You will get view of whole xml as 2nd parameter(View view) using that you can find out your textView like view.findViewById(your text view id);
If your using android define item android.R.layout.list_item_1 then typecast your view object with textview;
Using this textView you can get text of textView or like same manner any view will be accessible from list item.
Another way to do it is:-
lstcities.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick (AdapterView<?> parent, View view, int position, long id)
{
String text = (String) (((ListAdapter)parent.getAdapter).getItem(position));
// Do what you want to with this text
}
}
All you need to simply add below statement :
String selectedFromList = (String) (lv.getItemAtPosition(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.