How to automatically check the previously checked items? - android

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.

Related

Handling button event in each row of Listview issue

I am new to android and am trying to develop a new android app. But I am struggling to siolve one of the problems in my project.
I am using a listview extended from baseadapter and need to add a button in each row of thelistview. When I click on the button in any row of the listview, I want that it should be removed. However when I do so, some of the other buttons also get removed in the listview.
How can I solve this problem? Thank you..
You have an adapter, activity and some sort of data source
In your adapter you attach some data to buttons to be able to tell one from another:
public class ExpAdapter extends ListAdapter {
#Override
public View getView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
/* SOME CODE HERE*/
convertViewButton.setTag(buttonId);
return convertView;
}
/* SOME CODE HERE*/
}
in your activity you mark button id as the one to be hidden:
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
storageOfHiddenButtonsIds.add((Long)arg1.getTag());
}};
and then ListAdapter changes like this:
#Override
public View getView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
/* SOME CODE HERE*/
convertViewButton.setTag(buttonId);
if(storageOfHiddenButtonsIds.contains(buttonId))
{
convertViewButton.setVisiblity(View.GONE);
}
return convertView;
}
and when you want your adatper to change you, don't forget to call
this.expAdapterAllTaks.notifyDataSetChanged();
Sorry for any errors in my code, but i just wanted to give you an idea.
I faced same type of problem. ListView's setOnItemClickListener not works if you add item like a button on every listView item. Solution is use onClick in the list Item layout(which you use in custom adapter file) as
<ImageButton
android:id="#+id/my_delete"
android:onClick="onDeleteButtonClickListener"
... and so on />
where onDeleteButtonClickListener is a method in the activity where you set the adapter in listview.
public void onDeleteButtonClickListener(View v) {
// your code
}
here listItem means the individual row item of a ListView
Helpful Link: Button in ListView item

How can I eliminate the dependency from of a ListView's onItemClick/getView and its row types?

As a simplified example, consider a ListView that can contain both sub-categories and book titles. If a book title is clicked, a new activity should start that shows the cover image. If a sub-category is clicked, a new list of books and categories is displayed.
A Row interface is defined as follows.
interface Row {
void onClick();
void draw(View v);
}
I would like to know how to prevent a dependency from the ListView's ArrayAdapter as well as from the implementer of onItemClickListener on the Row implementers (e.g.,Book and Category).
One of the forces driving this requirement is the "don't repeat yourself" (DRY) principle: the ArrayAdapter implementation does not need to change when new row types are introduced.
Forgive my chicken-scratched "UML" below, but here's how I do it.
class ListAdapter extends ArrayAdapter<Row<?>> {
...
public View getView(int position, View convertView, ViewGroup parent) {
View view = listViewRowViewRecycler.getView(convertView);
rowProvider.get().getRow(position).draw(view);
return view;
}
}
The implementer of OnItemClickListener has a method like this:
void onItemClick(AdapterView<?> adapterView, View view, int rowIndex, long rowId)
{
rowProvider.onItemClick(rowIndex);
}
Then RowProvider has a method like this:
void onItemClick(int rowIndex) {
rowList.get(rowIndex).onClick();
}
Then the Row interface has a method with signature void onClick() and there are Category and Book implementations of Row that provide the necessary behavior.
Another possibility is to use the setTag/getTag methods on each item's View in the list - this allows you to attach the 'Book' or 'Category' to its appropriate row.
For this to work each of the 'listed' items should implement a common interface that has an appropriate method to be called when it is clicked.
For instance:
interface Selectable {
void onSelected(); // Add parameters if necessary...
}
In your list adapter:
public View getView(int position, View convertView, ViewGroup parent) {
View itemView; // Logic to get the appropriate view for this row.
Selectable theObjectBeingRepresented;
//
// Logic to assign a Book/Category, etc to theObjectBeingRepresented.
// In this example assume there is a magic Book being represented by
// this row.
//
Book aMagicBook = new Book();
theObjectBeingRepresented = aMagicBook;
// If all objects that will be contained in your list implement the
// 'Selectable' interface then you can simply call setTag(...) with
// the object you are working with (otherwise you may need some conditional logic)
//
itemView.setTag( SELECTABLE_ITEM_KEY, theObjectBeingRepresented );
}
Then when an item is clicked:
void onItemClick(AdapterView<?> adapterView, View view, int rowIndex, long rowId)
{
Selectable item = (Selectable)view.getTag( SELECTABLE_ITEM_KEY );
// Depending on how you are populating the list, you may need to check for null
// before operating on the item..
if( null != item ) {
item.onClick(); // Appropriate implementation called...
}
}
A bit more detail on the setTag method:
setTag documentation
Hope this helps...

how should i filter corrosponding checkbox with the string

adapter = new ArrayAdapter<String(getApplicationContext(),android.R.layout.simple_list_item_multiple_choice,contactList);
setListAdapter(adapter);
there is a EditText on the top of my ListActivity which is use to filter the contact list. But when i use the code below to filter.
search.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
adapter.getFilter().filter(arg0);
}
}
Only the string value associated with each item get filtered out but the checkbox associated with it remains same.
Example. Say I have 3 contacts "A", "B", and "C". when I open this ListActivity and select "A". Now when I search for contact "C"(by entering "C" in the search box) contacts get filters out and I can see the contact "C" on the top. But, it shows that it has already been selected. Actually, its not...because the checkbox associated with "C" was never shorted out...and the current checkbox seems to be associated with "C" is of contact "A".
What should i do to get the filtering synchronized?
m.png
I was hit by this issue as well, solved it by extending ArrayAdapter with my own custom adapter, and overriding getView, in which i set each view as checked/unchecked according to my unfiltered list.
This is my getView code:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
CheckBox view = (CheckBox) super.getView(position, convertView, parent);
MyObject object = getItem(position);
view.setChecked(object.selected);
view.setOnClickListener(MyClass.this);
return view;
}
I've also had MyClass implement onClickListener, to toggle the selected state of the clicked MyObject and call myAdapter.notifyDataSetChanged();

Problem with custom Listview and Checkboxes/Buttons

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));
}

ListView and hidden Id. How it is possible?

I'm developing an application for android and now I have implemented a ListView that shows a list of courses, connected to a database.
I would like to know how to include, with the name, an hidden id (that come from the db) so that once the user click on the elements the app goes to the relative view of the selected courses.
And how can I maintain the id during the navigation inside the course-view?
At the moment my code just load the name of the courses from the db and set in the list view:
ArrayAdapter<String> result = new ArrayAdapter<String>(CourseActivity.this, android.R.layout.simple_list_item_1);
for (CourseRecord c : get())
result.add(c.getFullname());
lv.setAdapter(result);
obviously I'm able to do also c.getid() but I don't where to put the id.
Thank you very much.
P.S.: Maybe does someone have also a really nice graphics of list view?
change your array adapter like this.
private ArrayAdapter<String> result = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1){
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
v.setTag(getMyIdForPosition(position));
return convertView;
}
};
and have an item click handler to recieve the selected ids
private OnItemClickListener itemClickedHandler = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id)
{
String myId = (String)v.getTag();
doYourStuff(myId);
}
};
assign the listener to the list
myList= (ListView)findViewById(R.id.history);
myList.setOnItemClickListener(itemClickedHandler);
You can store the id in a hidden TextView. In the list item XML, add 'android:visibility="gone"' to the TextView. Likewise in the click handler you can read the id from the textview.
You can also store id using setTag(Object object) method of a View. Use getTag() method to extract that id from that view.

Categories

Resources