从自定义列表视图按钮单击删除项目 [英] Remove item from custom listview on button click
问题描述
我有一个自定义列表视图,有2个textviews和2个按钮(播放和删除按钮) 我想,当我点击删除按钮,删除当前行。
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.
我的适配器类
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
public class SunetePreferateAdaptor extends BaseAdapter {
class ob {
String titlu, descriere;
public ob(String titlu, String descriere) {
this.titlu = titlu;
this.descriere = descriere;
}
}
ArrayList<ob> lista;
Context context;
public SunetePreferateAdaptor(Context context) {
this.context = context;
lista = new ArrayList<ob>();
for (int i = 1; i <= 20; i++) {
lista.add(new ob("text", "text2"));
}
}
@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.single_favsound_row, arg2, false);
Button b2 = (Button) row.findViewById(R.id.button2);
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// here, i want to delete the current row of the listview
//
//
}
});
TextView titlu = (TextView) row.findViewById(R.id.singleText2);
titlu.setText(lista.get(arg0).titlu);
titlu.setTextColor(Color.WHITE);
titlu.setTypeface(Global.font1);
TextView descriere = (TextView) row.findViewById(R.id.singleText1);
descriere.setText(lista.get(arg0).descriere);
descriere.setTextColor(Color.WHITE);
descriere.setTypeface(Global.font1);
return row;
}
}
嗯,我该怎么办呢? 我试着让ArrayList的静态和删除其项上点击..但没有成功。
Well how can I do that? I've tried making the arraylist static and delete its items on click.. but no success..
推荐答案
您不必进行静态的ArrayList
You need not make arraylist static.
您需要从其中填充列表视图列表中删除的数据。你叫 notifyDataSetChanged();
刷新lsitview。
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<ob> lista = new ArrayList<ob>();
for (int i = 1; i <= 20; i++) {
lista.add(new ob("text", "text"+i));
}
lv.setAdapter(新Sunete preferateAdaptor(这一点,LISTA));
lv.setAdapter(new SunetePreferateAdaptor(this,lista));
那么这些方法都有一个单独的.ajva文件
Then have this in a separate .ajva file
class ob {
String titlu, descriere;
public ob(String titlu, String descriere) {
this.titlu = titlu;
this.descriere = descriere;
}
}
然后
public class SunetePreferateAdaptor extends BaseAdapter {
ArrayList<ob> lista;
Context context;
public SunetePreferateAdaptor(Context context, ArrayList<ob> 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;
}
}
这篇关于从自定义列表视图按钮单击删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!