0
The situation is as follows:
I have a SeparatedListAdapter and with it I create 3 sections for my listview. Inside the sections, add 3 Adapters and 3 ArrayLists.
The problem is that in each of the list items, there is a remove button. I have programmed the removal within each Adapter separate. It removes, the problem is that it does not update the list. So when I press the button then the app stops because the list is empty, but does not update dynamically. Any idea?
Button btt = (Button)convertView.findViewById(R.id.remover_item);
btt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Datastring.remove(position);
notifyDataSetChanged();
}
});
Follow the code of the 3 simple Adapters.
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
public class Adapter1 extends BaseAdapter {
Context ctx;
LayoutInflater lInflater;
ArrayList<String> Datastring=new ArrayList<String>();
public Adapter1(Context context,ArrayList<String> Items) {
ctx = context;
Datastring.addAll(Items);
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return Datastring.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = (View) lInflater.inflate(R.layout.my_list_item, parent, false);
}
TextView text=(TextView)convertView.findViewById(R.id.list_content1);
text.setText(Datastring.get(position));
Button btt = (Button)convertView.findViewById(R.id.remover_item);
btt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Datastring.remove(position);
notifyDataSetChanged();
}
});
return convertView;
}
}
And the code of the Separatedlistadapter:
import java.util.LinkedHashMap;
import java.util.Map;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
public class SeparatedListAdapter extends BaseAdapter {
public final Map<String,Adapter> sections = new LinkedHashMap<String,Adapter>();
public final ArrayAdapter<String> headers;
public final static int TYPE_SECTION_HEADER = 0;
public SeparatedListAdapter(Context context) {
headers = new ArrayAdapter<String>(context, R.layout.header);
}
public void addSection(String section, Adapter adapter) {
this.headers.add(section);
this.sections.put(section, adapter);
}
public Object getItem(int position) {
for(Object section : this.sections.keySet()) {
Adapter adapter = sections.get(section);
int size = adapter.getCount() + 1;
// check if position inside this section
if(position == 0) return section;
if(position < size) return adapter.getItem(position - 1);
// otherwise jump into next section
position -= size;
}
return null;
}
public int getCount() {
int total = 0;
for(Adapter adapter : this.sections.values())
total += adapter.getCount() + 1;
return total;
}
public int getViewTypeCount() {
int total = 1;
for(Adapter adapter : this.sections.values())
total += adapter.getViewTypeCount();
return total;
}
public int getItemViewType(int position) {
int type = 1;
for(Object section : this.sections.keySet()) {
Adapter adapter = sections.get(section);
int size = adapter.getCount() + 1;
// check if position inside this section
if(position == 0) return TYPE_SECTION_HEADER;
if(position < size) return type + adapter.getItemViewType(position - 1);
// otherwise jump into next section
position -= size;
type += adapter.getViewTypeCount();
}
return -1;
}
public boolean areAllItemsSelectable() {
return false;
}
public boolean isEnabled(int position) {
return (getItemViewType(position) != TYPE_SECTION_HEADER);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int sectionnum = 0;
for(Object section : this.sections.keySet()) {
Adapter adapter = sections.get(section);
int size = adapter.getCount() + 1;
// check if position inside this section
if(position == 0) {
return headers.getView(sectionnum, convertView, parent); // this is where your header names will get bind. correctly.
}
if(position < size) {
return adapter.getView(position - 1, convertView, parent);
}
// otherwise jump into next section
position -= size;
sectionnum++;
}
return null;
}
@Override
public long getItemId(int position) {
return position;
}
I think we’re gonna need more code, precisely the code of
Adapters.– mutlei
I could test with the
notifyDataSetInvalidated? The difference to thenotifyDataSetChangedis that in it the number of elements has not changed, only the data of each item.– Wakim
notifyDataSetInvalidated worked. However, only when I give a scroll in the list, when I "move" it. Statically it does not remove.
– Lucas Soares
I managed to resolve it. I added Adapter.registerDataSetObserver(new Cascadedatasetobserver()); to the addSection() method in the Big Adapter.
– Lucas Soares
Now another question. And to add elements dynamically ? I’m trying for the same concept but I haven’t found a solution yet. Any idea ?
– Lucas Soares
@If you have another question create a new question, you can even link this to help understand the problem. By the way if you have the answer to your question I would advise you to create an answer and then accept it.
– Jorge B.
@Lucassoares just noticed that the doubt is over a year old, but feel free to create the answer anyway.
– Jorge B.