The situation is as follows:
I have SeparatedListAdapter
and with it I create 3 sections for my listview
. Within the sections, I add 3 Adapters
and 3 ArrayLists
.
The problem is that in each of the items in the list, there is a remove button. I scheduled the removal within each% of% separated. It removes, the problem is that it does not refresh the list. So when I press the button next, the app stops because the list is empty, but it 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 for 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 SeparatedListAdapter code:
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;
}