i´m in trouble with a custom Listview and (Checkboxes or Button). I follow a guide (the android devolopers´s cookbook) and my custom Listview show correctly. An visisble Error occured when selected Checkboxes are scrolling.(Wrong Checkboexes are checked)
I followed the guide on
http://www.vogella.de/articles/AndroidListView/article.html#listviews_performance
but it doesn´t work. How to save the state correctly?
Greeting
Andreas
You will have to save the checked items in a list variable (in your Adapter subclass) and set the correct state (checked/unchecked) depending upon whether the item is present in the list variable.
private List<MyItem> mCheckedItems; //In your adapter subclass
Add/remove an item from the list variable:
//The AdapterView.OnItemClickListener, is present where you set myListView.setOnItemClickListener(this);
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
{
MyItem item = myAdapter.getItem(position);
myAdapter.updateCheckedItems(item);
}
public void updateCheckedItems(MyItem item) //In your adapter subclass
{
if(!mCheckedItems.contains(item))
{
mCheckedItems.add(item);
}
else
{
mCheckedItems.remove(item);
}
}
Set the correct state of the checkbox:
public View getView(int position , View view , ViewGroup parent) //In your adapter subclass
{
final MyItem item = getItem(position);
CheckBox checkBox = (CheckBox)view.findViewById(R.id.checkBox);
checkBox.setChecked(mCheckedItems.contains(item));
}
Related
I have been developing an android application and i am stuck in the middle.
The problem is i am using SimpleAdapter to do the adapter stuff and show items in the Listview and as far as i know i cannot override the getView() method of SimpleAdapter class to bound click listeners to the items.
There is a other way to handle click events of sub items like using XML, you can write in the XML like android:clickable="true"and android:onClick="clicklistenr", using this i can get the item but my problem is if i use this then i cannot get the position of the adapter which i need to get adapter item values and handle other tasks. So i am stuck here any help would be appreciable. thanks.
For example i have a ListView which contains one image, TextView, like Button, share Button in each of its items. And there is no way i can find that either its image or button clicked using setOnItemClickListener. So i need a way to handle click events of these sub items of a ListView, i am using SimpleAdapter.
Just call listView.setOnItemClickListener() with your implementation of the listener.
and use like
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
Where list=(ListView)findViewById(R.id.list); and list.setAdapter(your_adapter);
For More details Follow: http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
Hope It Will Help You.. :)
You can use like
myListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HashMap<String, Object> obj = (HashMap<String, Object>) adapter.getItem(position);
String result= (String) obj.get("name");
Log.d("Yourtag", name);
}
});
First of all your problem is how to handle the items of a custom listview.. not sub. Anyways...
If you are using SimpleAdapter , you may use getView().But if you are using SimpleAdapter you don't need to use the getView() as the it handles the mapping of data to your layout via the resource. For inforamtion check the SimpleAdapter in developer site.
Another thing is there is not mandatory for using any particular adapter. You can create any adapter class which will extends the BaseAdapter which will implement the getView().
Inside getView() you can able to inflate your custom layout(which contains the image,button etc..) to your view or convertview.something like:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.yourcustomlayout, null);
//Initialize your custom views here
TextView someText= (TextView) vi.findViewById(R.id.tvSometext);
Button likeButton = (Button ) vi.findViewById(R.id.btnLike);
//put data or call onclicklistener or
//whatever you want to do with custom views
likeButton .setOnClickListener(...){
//on click
}
}
Ans simply call this through your constructor with some data and appropriate context.
Amir,
I am not sure you found the solution or not. You can do this way:
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
//for each subitem, set click listener & set tag having position as
// value.
ImageView imv = (ImageView)view.findViewById(R.id.live_view);
imv.setTag(i);
imv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("INFO","Live view clicked");
//This will give you position in listview.
int position = (int) v.getTag();
}
});
}
I hope it will help you and others looking to find relevant answer.
May be there is some other solution too, but it will surely work.
I would like to create an android app which displays a list in a listview and then allow the user to delete an item when on onItemlongclick and then displays checkboxes to select which items to delete.
I know it have to call the OnItemLongClickListener but do not know how to implement that. Any ideas to do this?
lv.setOnItemLongClickListener(this);
#Override
public boolean onItemLongClick(AdapterView<?> parent, View v, int pos, long id) {
// TODO Auto-generated method stub
return false;
}
I'm so lost in ideas how to do this. I googled but unfortunately didn't find any relevant tutorials.
Help is much appreciated. Thanks.
I did this just recently, but it was an ad hoc fix, so this might not be the best way.
In the layout for my ListView items (rows), I included a CheckBox whose visibility will be toggled, but is initially not visible, i.e., View.GONE.
Then, in my Adapter, I included a member boolean variable, selectable, and a public method to set it and refresh:
private boolean selectable = false;
public void setSelectable(boolean selectable)
{
this.selectable = selectable;
notifyDataSetChanged();
}
In the getView() method of the Adapter, selectable is checked and the CheckBox's visibility is set accordingly.
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
...
cbx.setVisibility(selectable ? View.VISIBLE : View.GONE);
...
}
I used a ToggleButton to change the selection mode, but in your case, you'll need to do something a little different. I would add an additional method to the Adapter:
public boolean isSelectable()
{
return selectable;
}
Then, you can toggle the selectable state on long clicks:
#Override
public boolean onItemLongClick(AdapterView<?> parent, View v, int pos, long id)
{
...
yourAdapter.setSelectable(!yourAdapter.isSelectable);
...
}
To respond to clicks on list items, you need to put this in onCreate():
ListView lv = (ListView) findViewById (R.id.list);
lv.setOnItemLongClickListener (this);
lv.setLongClickable (true);
Then, onItemLongClickListener will be called whenever the user long-touches a list item. Put a breakpoint there and be satisfied you understand this much.
I have a ListView with a TextView and a Checkbox on every row. I'm trying to create a sequence where the user has to click on the Checkbox of the first row to enable the next row and remove a grayed out effect. Clicking on the Checkbox in this row will do the same to the next row and so on.
Is this possible? and how?
Create a custom Object to keep the state of your row. For example:
class Row {
private TextView text;
private CheckBox box;
private boolean grayedOut;
//implementation...
}
Inside your Activity you make an ArrayList<Row>() and give it as source to the Adapter of your ListView.
onCreate(..){
mArrayList = new ArrayList<Row>();
// fill arraylist
Adapter mAdapter = new CustomAdapter(... , ... , mArrayList);
ListView lv = findView..
lv.setAdapter(mAdapter);
}
The Adapter for your ListView is custom. You can follow the example found here. In your case some adjustments will be as followed:
public class CustomAdapter extends ArrayAdapter<Row>{
public CustomAdapter(...) {
// do stuff
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// ....
if (item at position has gray state){
set layout to gray
} else {
set layout to active
}
// ....
return row;
}
Now we have a Listview, a CustomAdapter and for each item in the list we have an Row which provides a certain state. Default all the states will be set to gray-state except the first item. Using the onItemClickedListener, we can unlock other items..
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View v, int position, Long id){
// do stuff...
mArrayList.get(position+1).setGrayedOut(false);
}
}
Good luck!
How do i access the controls of an list view on item Click listener in a list view? As i have set the customize layout for a single list view item.
Attaching an item click listener
In the actvity where your listview is created try something similar to these
private void setupListView() {
//find by view id
customListView = ((ListView) findViewById(R.id.lv_listview);
//set custom adapter
customListView.setAdapter(
new CustomListAdapter(this, R.id.lv_listview, variableList));
//attach listener
customListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
/* whatever happens when you click the list item goes here */
}
});
}
I have list of items with check boxes using this code:
ProcessList = (ListView) findViewById(R.id.list);
ProcessList.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,nameList ));
ProcessList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
ProcessList.setTextFilterEnabled(true);
I am getting items checked list from:
ProcessList.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id)
{
selectApp = (String) ProcessList.getItemAtPosition(position);
I am storing all checked list in array and now I want that the previously 'checked' items automatically 'checks' on restart of my app. Is there any way to do it ? (by extending my above code)
I don't know if there is an automatic way.
Overload the ArrayAdapter class and override the function
getView(int position, View convertView, ViewGroup parent)
in a way which returns checked or not based on you previous array.