Hi I currently have a Grid View, Which I Inserted an Array through an Adapter onto the Grid View.
I am using this method currently:
gridview.setOnItemClickListener(new OnItemClickListener() {
}
As demonstrated within the Android Documentation, Specifically I need to find the value set within the field of the grid that the user touch/clicked. Is this done through using the position variable within on item click e.g?
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// DO something
}
Or is it done manually ? Kind of at logger head's with this. Any help would be really appreciated.
You can try something like:
String value = (String)adapter.getItemAtPosition(position);
inside your onItemClick where adapter is the AdadpterView
The v argument in your onItemClick() method is a reference to the View that was clicked.
Simply cast that into a TextView and get the text:
String text = ((TextView)v).getText().toString();
Related
I have a custom listView with 3 spinners. I want to execute a code in onItemSelcted but I cannot do this in adapter because some part of the code that I need to execute won't work in a adapter.
I had this code pinned to a button before but sometimes there are over 20-30 elements in the list and I need do this "automaticly" right after user picks a value from spinner to save some time.
For a button I had this code:
((ListView) parent).performItemClick(v, position, 0);
in setOnClickListener that I created inside adapter. After using this code in adapter I could exetue any code outside adapter by writing something like this:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
long viewId = view.getId();
if(viewId == R.id.button){
myCode(); }
I found no way to do something similar because there was no "link" between inside adapter onItemSelcted and outside adapter onItemSelcted. Of course writing same line of code inside adapter's onItemSelcted won't work because spinner "doesn't click".
Any idea how to solve this?
In a fewer words: I need to execute onItemSelected for a spinner that's inside custom listView element but need to do this outside the adapter in onCreate method.
You can create an CallbackInterface and create an instance of it "outside" and pass it down to the "inside", where you call the callback function with the needed arguments.
public interface Callback {
void onAction (int a, int b, String c);
}
Currently I am making an app in Android which is a todo list, it has a edit text, button and a listview, when button is clicked the text is added to listview, everything works fine but when i add too many items to list view and try to select a list item my app stops working and I get a error saying
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
Does anyone know a fix?
this is my view data method whcih displays items in listview
public void viewdata(String tablename)
{
db = new mydbhandler(this);
lvitemslist = findViewById(R.id.lvitemlist);
ArrayList mylist = new ArrayList();
Cursor c = db.getdata(tablename);
while(c.moveToNext())
{
mylist.add(c.getString(1));
}
ListAdapter mylistadapter = new ArrayAdapter(this,R.layout.custom_item_row,R.id.ctv,mylist);
lvitemslist.setAdapter(mylistadapter);
}
and this is the onitemclicklistener
lvitemslist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
View v = lvitemslist.getChildAt(i);
CheckedTextView ctv = v.findViewById(R.id.ctv);
ctv.setCheckMarkDrawable(android.R.drawable.checkbox_on_background);
db.deletedata(tablename,ctv.getText().toString());
viewdata(tablename);
Take a look at this line from your OnItemClickListener:
View v = lvitemslist.getChildAt(i);
I guess you want to assign the ListView row which was clicked to the variable v. But the ViewGroup method getChildAt(int) (ListView is a kind of ViewGroup) will give you
the view at the specified position or null if the position does not exist within the group
(quoted from the ViewGroup documentation)
Like you said, this works for the first few items but for higher positions the app crashes. This is because the ListView has a fixed number of child Views: about as many as fit on the screen simultaneously plus some more to enable smooth scrolling. For every clicked row beyond that fixed number, lvitemslist.getChildAt(i) will return null.
What can you do to fix your code?
Luckily you do not have to do much work to get the clicked View, it is handed to you as one of the parameters of onItemClick(): the second parameter is the clicked ListView row.
So you just need to write
lvitemslist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long rowId) {
CheckedTextView ctv = view.findViewById(R.id.ctv);
ctv.setCheckMarkDrawable(android.R.drawable.checkbox_on_background);
db.deletedata(tablename,ctv.getText().toString());
viewdata(tablename);
}
}
This will help you to avoid the NullPointerException.
Another thing: you change the appearance of a ListView row from outside of the Adapter by calling
ctv.setCheckMarkDrawable(android.R.drawable.checkbox_on_background);
This is dangerous (there are lots of questions on this site where people had weird behaviour when scrolling as a consequence). It may not be a problem in your case because you refresh the whole ListView soon afterwards. But generally one should provide the Adapter with all the data necessary to decide what each row should look like. Then one can change the data list and after that of course call notifyDatasetChanged() on the Adapter.
I have successfully populated a ListView with an onItemClickListener that takes the user to a new Activity when clicked.
But I'd like to pass on the name of the ListView item that was clicked as a variable (so I can use it in an SQLite query).
I understand that I need to pass this variable as an extra in an Intent, but how can I capture that variable in the first place?
Here's my current code:
authorsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// String author = captured ListView item
}
});
authorsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String author = authorsListView.getItemAtPosition(position).toString();
}
});
The list you are passing in the adapter, get the reference of that and get the object of the list using get method/ and use 'i' variable as an argument for get method. It will return the object of click position. And done. Use this object get the require property and pass that with intent to new activity.
I have created a custom ArrayAdapter that fills a ListView. The data in the adapter is a list of POJOs which I get from a server. Each POJO has a unique id given by the server, I have no influence on that id. The id is only for internal use, means it isn't displayed to the user on the ListView.
My intention: if the user clicks an item in the list, I want to pass the id back to the server. The first step would be to create an onItemClickListener:
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void OnItemClick(AdapterView<?> parent, View view, int position, long id) {
// call the server in a new thread
// BUT: how do I get the POJO id from this position?
}
});
My problem is: I don't know how to obtain the id of the POJO. Since it is not displayed to the user, is is not in the TextView I get passed in the listener. The other parameters - position and id - do not seem to be what I need also. Description:
position: The position of the view in the adapter.
id: The row id of the item that was clicked.
I hope you got what I mean, otherwise tell me. Thank you!
the parent.getItemIdAtPosition(int position) will give you for pojo object at that index.
That would depend on how your adapter work. The id parameter will have a correspondence to one of the items the adapter is adapting. Based on that id, and how your adapter works, you can retrieve the original logical item that was represented by the view that was clicked.
#Override
public void OnItemClick(AdapterView<?> parent, View view, int position, long id) {
Object item = parent.getAdapter().getItem(position);
}
In the application I am writing, I have a main class which extends the ListActivity - it contains a list of elements. I want these elements to link to a different page (an xml one, or perhaps a View object). However, I realised one cannot use the method setContentView(int) on a ListActivity object.
What's to be done?
Thanks!
Looks like you are trying to launch a new activity.
You have to override the onListItemClick method of ListActivity.
Here is the code.
// ListView l points to list view whose item user clicked
// View v points to the item in the list view on which the user clicked
// int position is the position index of the item in the list
// long id is the id assigned to the item. This id is assigned using the ListAdapter, CursorAdapter etc.
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// I am using getApplicationContext() as it is more safe then just passing `this`
Intent i = new Intent(this.getApplicationContext(), ActivityToRun.class);
this.startActivity(i);
}
NOTE: You have to improve on this skeleton depending upon your needs.
Have you tried setting the XML page as a ListView and give it the ID #android:id/list?