I have list of items in gridview. On selecting item, position of the selected item should change to 0. Every time i choose item , it should move to first position.
How can i do this?
got it
temp = mFilteredBrandsList.get(position);
mFilteredBrandsList.remove(position);
mFilteredBrandsList.add(0, temp);
notifyDataSetChanged();
Try this inside your onItemClickListener.
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//assuming your grid is a list of string elements
String item= (String)parent.getItemAtPosition(position);
mList.remove(position);
mList.add(0,item);
notifyDataSetChanged();
}
Assuming mList is the main list consisting of elements inflated in grid's row.
If this isn't clear enough. SHare your code will modify the same.
Related
I am making a list of contacts. The contact number is a sum item in the list. If user clicks the phone button on that item then the number is called.
How to get the number? It is a sub-item in the list.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Get the selected item text from ListView
String selectedItem = (String) parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(),"item is"+selectedItem,Toast.LENGTH_SHORT).show();
}
});
I am only able to get the Item Value. Not the subItem
Try to implement a method on the adapter to return the subItem from the item List
without the code it is hard to give more details.
I have 2 listview in my activity and i want to highlight the item being selected using the adapter in adapter class(one item from the both the list at a time), So by default i want only one list view item to be selected i.e the first list view item and based on the clicks on the listview the highlight should also change,that on both the listview the first item is being highlighted in the beginning and on item click the highlight changes fine.
My problem is that when i click in the first listview the scond listview items should not be highlighted but it is being highlighted why??
Here is my implementation:
I have initialised two adapter variable of the same class for the two listview
ListView list1= (ListView)finViewById(R.id.list1);
CustomListview adapter= new CustomListview(this,item,"type(it is string)")
list1.setAdapter(adapter);
list1.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
adapter.notifyDataSetChanged();
adapter.setSelectedItem(position);
........
});
and in CustomListView adapter class i'm doing the following along with the rest of the code i.e
if (position == mSelectedItem && choice.equals("Strintype")) {
tv.setTextColor(getContext().getResources().getColor(R.color.primary_dark));
} else if (choice.equals("Stringtype")) {
tv.setTextColor(Color.BLACK);
}
You're using the item position to check what to highlight, if you do so you'll have highlighted also the item in the same position in list 2; try using the item id intstead of the position (obviously there's more work to do) or use two different adapters for the lists.
Can you post the entire code for the two lists, including the adapters?
I had a requirement in android spinner where I need to display the selected item as first element (which is element in zeroth position ) irrespective of item selected.Suppose I selected element in first position , then also zeroth position element only should be selected.How to make zeroth element to be selected irrespective of any item selected in spinner.
In onItemselected() method I have the item selected as zero , but still the clicked element is selected for as fraction of second.
Can anyone help me in sorting out this issue.
ok, then set it 0 at onItemSelected()...
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
mSpinner.setSelection(0);
}
I have a ListView with a CheckBox.
What I am trying to implement is an onItemClick (in my activity) that check/select my CheckBox if I click on the row, as you can see below:
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
MyItem row = ListofRows.get(position);
row.setSelected(true);
myAdapter.notifyDataSetChanged();
}
When I click in the row, the ListView is refreshed because of the method notifyDataSetChanged()
THE PROBLEM IS: this method refreshes the whole ListView and as a result, the ScroolBar goes to the beginning (the user is driven to the header of the list) even if the user is at the bottom of the list.
WHAT I NEED IS: Refresh just this single clicked row so the user won't be driven to the beginning of the list.
Are there any solutions to this problem?
use the selected index
View v = getListView.getChildAt(index);
Now update the view with the new values.
I have a custom listview. I have to select some items from this listview and display it in the next layout listview when a button click event is clicked.
lv5=(ListView)findViewById(R.id.ListView05);
lv5.setAdapter(new ArrayAdapter<String>(this,R.layout.productselecttext,R.id.pstext,arr));
lv5.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(view.findViewById(R.id.oi).getVisibility()==ImageView.VISIBLE){
ImageView icon = (ImageView)view.findViewById(R.id.oi);
icon.setImageResource(R.drawable.vi);
}
}
});
In this way I have selected using an imageview. How can I get only the selected items and display in another listview?
It all depends on what you call a selected item. We need some more code to understand how you "selected" items in the first listview.
But generally speaking, all you have to do is to build a new Adapter for the second list view that just lists the selected item. You could even recylcle the first adapter and the first view, just eliminating the items that are not selected from the adapter's list and calling
notifyDatasetChanged()
on your first adapter to update your first list.
Regards,
stéphane