My ListView looks like this:
[CheckBox] [TextView]
My question is, how can I change the item position when CheckBox is checked? I mean to say, if the user checked any item from ListView, then the checked item goes to the end, so its position changes from current to last.
Thanks in Advance.
Use Custom adapter for listView. here is an example. Now from your ListActivity class
final ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, Object> map = (HashMap<String, Object>)
lv.getItemAtPosition(position);
// get the item Position from here and make the nacessary changes
// and update the listview again.
}
Without posting any code, this will have to be a brief overview of the pseudo steps you need to take
You simply need to update the ordering of your data set being used by your adapter (usually an arraylist or array of objects), and then called
notifyDataSetChanged()
on your adapter.
So in your case, you want to take the element at the position that was clicked, and put it at the end of your arrayList of objects.
If you post some code, you may get more detailed answers however
Here is the step you can follow. I have not tested it but you can try this.
// your value array you are binding with listview
private final String[] values;
// put default value to "n". size will be the same as for values array.
private final String[] valuesChecked;
onClick of Checkbox Listview
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//get checkbox is checked or not
if it is checked then{
valuesChecked[position]="y";
}else{
valuesChecked[position]="n";
}
// short your values array by first "n" and after "y".
// call your adapter.notifyDataSetChanged();
}
Good Luck :)
Related
I have CustomAdapter which I am using for populating ListView with some data.
Each element in ListView has two variables. For each listview (in onItemClick method) I must check this variables and If they are the same - do some code and If they are different - do another code, for example Toast.makeText(EPG.this, "Variables are different", Toast.LENGTH_SHORT).show();
So I have tried this:
private List<SomeItem> items = new ArrayList();
//items were created
SomeAdapter adapter = new SomeAdapter(this, R.layout.list_item, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
for(int i=0; i<=items.size(); i++) {
SomeItem item = items.get(position);
String tmpCI = item.getFirstVariable();
String tmpPCI = item.getecondVariable();
if (!tmpCI.equals(tmpPCI)) {
//some code
} else {
Toast.makeText(EPG.this, "Variables are different", Toast.LENGTH_SHORT).show();
}
}
}
});
But all of my listview elements have values of the first element in those two variables.
So how can I do something like item.next(); for validating all of items in listview?
UPD:
Sorry, I will provide more information about what I am doing after checking variables of listview items for understanding my issue.
I have one more adapter:
SomeAnotherAdapter adapterPr = new SomeAnotherAdapter(this, R.layout.list_tem_another, itemsAnother);
and one more listview:
listViewAnother.setAdapter(adapterPr);
First of all I understood, that first variable should be from first listview and the second variable from another listview.
In this listViewAnother I have many items, which has some "id". For example 1st, 5th and 20th elements have id 90 and other elements have id 100.
We can say, that items from the first listview also have "id".
So I must check if(first variable = second variable) and then show in listViewAnother only items that have id which equals ID from clicked item in listView.
I tried: adapterPr.remove(item2); but then I understood, that I need all of items because I can go back to listView and press another item which will need those removed elements.
Now, hope I provided full information and you will be able to help me improve my code.
Do you need to perform the check on every element of the adapter when you click on one element of the adapter? If not, you don't need a loop. If you do, your loop should be iterating over the original list, and does not need adapter position at all.
In general when using adapters and lists, you should use the adapter's position and the adapter's data set to perform any tasks. It's not good practice to use the adapter position to get an item from the original list.
Simply set one onItemClickListener which gets the corresponding item from the adapter, and do what you need to from there:
private List<SomeItem> items = new ArrayList();
//items were created
SomeAdapter adapter = new SomeAdapter(this, R.layout.list_item, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SomeItem item = adapter.getItem(position);
String tmpCI = item.getFirstVariable();
String tmpPCI = item.getecondVariable();
if (!tmpCI.equals(tmpPCI)) {
//some code
} else {
Toast.makeText(EPG.this, "Variables are different", Toast.LENGTH_SHORT).show();
}
}
});
I have a list view and I've implemente filtering.
Lets say I have items A, B and C. If I type B in the filter box, only item B will be displayed and it is the position 0 of the list (before it was in position 1). So when I call the onClick item, I get the the id/position 0, which leads to displaying details about A instead of B.
This is the onclick code:
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Poi poi = pois.get((int)id);
goPOIDETAIL(poi);
}
});
id and position have the same value.
is there a way to get the original position, or get some other value indicating the real item that I clicked?
Thanks
flashsearchList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Integer temp=flashSearchNameMap.get(adapter.getItem(position));
navigateSearch(temp);
}
});
(adapter.getItem(position) will return you the exact list name and in flashSearchNameMap i have stored names and position at beginning from oncreate before applying filtering.So you can get exact position by this
I think the problem is in the way you manage your filter. You should get the object with selected id not from the original List (or array) but from the filtered one.
I used something like it in this post from my blog. Hope this help you
ID and Index are not the same. Of course, you can return item index in getItemId() method of your adapter, but don't expect your items to be identified correctly by this method if you do.
Try providing unique ID for each of your items. The idea is somewhat similar to ID of each record in the database, which never changes (and lets you reliably identify each record), and it is easily implemented when you get your data from database.
But if your items don't have unique IDs, and you don't want to bother providing them, there's another approach (see this example code for Adapter below):
public MyAdapter extends BaseAdapter {
private List<Item> items;
private List<Item> displayedItems;
public MyAdapter(List<Item> items) {
this.items=items;
this.displayedItems=items;
}
public filter(String query) {
if(query.isEmpty()) {
displayedItems=items;
} else {
displayedItems=new ArrayList<Item>();
for (Item item : items) {
displayedItems.add(...) //add items matching your query
}
}
notifyDataSetChanged();
}
//...
//NOTE: we use displayedItems in getSize(), getView() and other callbacks
}
You can try:
#Override
public boolean hasStableIds() {
return false;
}
in your adapter
if you are using datbase you have the _id key that you can load in a filtered list as invisible field. Once you click on the item you can query data with _id key.
If you aren't using a database you could add a hidden id element in your row element as well.
Up front: This is my first attempt at an Android app. I'm in that strange place of not knowing what to search for to find the answer to my question.
What I have accomplished is:
Created a custom class myCustomClass with properties of 'title' and 'youTubeUrl'
Created an ArrayList<myCustomClass>
Added multiple elements to ArrayList<myCustomClass>
Created a custom ArrayAdapter and attached it to the the arraylist.
Added an onItemClickListener to the custom ArrayAdapter.
All of that works good. I would like to show the title in the ListView and then when the user clicks the list view item, I'd like to get a reference to the youtubeUrl property.
Here's what I have for the adapter code:
MyListAdapter myListAdapter = new MyListAdapter(this, R.layout.my_list, elements);
myList.setAdapter(myListAdapter);
myList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
myListAdapter.notifyDataSetChanged();
Thanks for your help.
You can use the position property in onItemClick to go back to your data source and find the relevant item. From there you should be able to retrieve the Url.
As another poster implied, it depends on what you are using in your adapter. Assuming it's MyCustomClass. You can do something like this in your onItemClick method:
MyCustomClass selection = (MyCustomClass) getListView().getItemAtPosition(position);
I am trying to use an AutoCompleteTextView to choose an item from a potentially long list of candidate items. I can display the list fine, and I can get the call onItemClick when the user chooses one of the autocomplete suggestions. The problem is, the "position" reported in onItemClick is the position in the dropdown list of filtered items, NOT the position in the original list of candidates.
I need to know the index of the selected item in the original list I gave to the ArrayAdapter, NOT the position in the dropdown list after it's been filtered.
I tried subclassing AutoCompleteTextView and overriding onCommitCompletion, which is supposed to give the original list index, but it is not being called when an item is selected.
I also tried subclassing BaseAdapter so I could generate the views for the autocomplete list myself and setTag with the application object for each item, but AutoCompleteTextView won't accept a BaseAdapter subclass for setAdapter.
I can't believe there isn't a way to do this without completely re-writing AutoCompleteTextView.
Hoping someone has an answer for this!
I'm not entirely sure how you'd go about it, but ensure your adapter is assigning an id properly, and then use that value in the callback.
STATE.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
String selection = (String) parent.getItemAtPosition(position);
int pos = -1;
for (int i = 0; i < yourarray.length; i++) {
if (yourarray[i].equals(selection)) {
pos = i;
break;
}
}
System.out.println("Position " + pos); //check it now in Logcat
}
});
I have a activity displaying call logs in a ListView. The adapter used here to populate listview extends CursorAdapter. Listview is set to onItemClickListener(..). My Question is whenever an item is clicked how does cursor get the data? how does cursor know which position is clicked and need to get data from clicked position only? I have provided a code snippnet.
public class CallLog extends Activity
{
ListView mListView;
Cursor cursor;
//other variables
public void OnCreate()
{
setContentView(R.layout.calllog);
//SQLiteDatabaseInstance db
cursor = db.query(...parameters to get all call logs...);
mListView.setOnItemClickListener(this);
}
public void OnItemClick( // all arguments... )
{
//there is a column name 'NAME' in call log table in database
String name = cursor.getString(cursor.getColumnIndex(Database.NAME))
//here name is obtained of the clicked item.
}
Cursor is a result set. how does the cursor know which item is clicked? What can be the methods implicitly called by cursor that gives it position of clicked item?
If there are any links of similar question then pls provide.
I hope I am able to make you understand the question. Thank you
Try this:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//move cursor to clicked row
cursor.moveToPosition(position);
}
Specifically it is NOT the Cursor that knows who clicked on what. This is actually handled by the Adapter. The adapter is used to group elements together and allow abstraction as such that they can be handled in a uniform way.
Any form of list, always has an adapter, and this is exactly why the adapter works so well. If you look at a Custom Listview with a Custom Adapter, you'll see exactly how this is done.
Example:
http://android.vexedlogic.com/2011/04/02/android-lists-listactivity-and-listview-ii-%E2%80%93-custom-adapter-and-list-item-view/
You should use cursor.moveToposition(position) inside function to get to the position of that clicked item. After that you apply this and when you will click on any item, the cursor will be set on that item and then you can use that particular item for your operation.
mListView..setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0,
View view, int position, long id) {
// here position gives the which item is clicked..
}
});
Additionally check this link for ListView ListView and ListActivity
It may help you..