I have a custom listview, that has 2 textviews and 2 buttons (play and delete button) I want when I click the delete button to delete the current line.
My adapter cl
You need not make ArrayList
static.
You need to delete the data from the list which populates listview. You call notifyDataSetChanged();
to refresh the lsitview.
You can remove the static key word and use
Button b2 = (Button) row.findViewById(R.id.button1);
b2.setTag(arg0);
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
int pos = (int)arg0.getTag();
lista.remove(pos);
SunetePreferateAdaptor.this.notifyDataSetChanged(); }
});
Alternative :
You can pass the list to the constructor of adapter class.
ListView lv = (ListView) this.findViewById(R.id.listView1);
ArrayList lista = new ArrayList();
for (int i = 1; i <= 20; i++) {
lista.add(new ob("text", "text"+i));
}
lv.setAdapter(new SunetePreferateAdaptor(this,lista));
Then have this in a separate .java file
class ob {
String titlu, descriere;
public ob(String titlu, String descriere) {
this.titlu = titlu;
this.descriere = descriere;
}
}
Then
public class SunetePreferateAdaptor extends BaseAdapter {
ArrayList lista;
Context context;
public SunetePreferateAdaptor(Context context, ArrayList lista ) {
this.context = context;
this.lista= lista;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return lista.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return lista.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.fg, arg2, false);
Button b2 = (Button) row.findViewById(R.id.button1);
b2.setTag(arg0);
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
int pos = (int)arg0.getTag();
lista.remove(pos);
SunetePreferateAdaptor.this.notifyDataSetChanged(); }
});
TextView titlu = (TextView) row.findViewById(R.id.textView1);
titlu.setText(lista.get(arg0).titlu);
titlu.setTextColor(Color.WHITE);
TextView descriere = (TextView) row.findViewById(R.id.textView2);
descriere.setText(lista.get(arg0).descriere);
return row;
}
}