Android 单击按钮从自定义列表视图中删除项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23103356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove item from custom listview on button click
提问by Boldijar Paul
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.
我有一个自定义列表视图,当我单击删除按钮删除当前行时,它有 2 个文本视图和 2 个按钮(播放和删除按钮)。
My adapter class
我的适配器类
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;
}
}
Well how can I do that? I've tried making the arraylist static and delete its items on click.. but no success..
那么我该怎么做呢?我试过将数组列表设为静态并在单击时删除其项目.. 但没有成功..
回答by Raghunandan
You need not make ArrayList
static.
你不需要使ArrayList
静态。
You need to delete the data from the list which populates listview. You call notifyDataSetChanged();
to refresh the lsitview.
您需要从填充列表视图的列表中删除数据。您调用notifyDataSetChanged();
以刷新 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(new SunetePreferateAdaptor(this,lista));
lv.setAdapter(new SunetePreferateAdaptor(this,lista));
Then have this in a separate .java file
然后把它放在一个单独的 .java 文件中
class ob {
String titlu, descriere;
public ob(String titlu, String descriere) {
this.titlu = titlu;
this.descriere = descriere;
}
}
Then
然后
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;
}
}
回答by Zeer0
try this
尝试这个
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
lista.remove(position);
SunetePreferateAdaptor.notifyDataSetChanged();
}
});
回答by Top Cat
You can done it by using your ArrayList lista. First remove the item in current position and the call adapter.notifyDataSetChanged() function.
您可以通过使用 ArrayList 列表来完成。首先移除当前位置的项目并调用 adapter.notifyDataSetChanged() 函数。
回答by Nikhil Khurana
This code worked absolutely fine for me.
这段代码对我来说非常好。
public class CustomAdapter extends ArrayAdapter<String>
{
Context c1;
String s1[];
int s2[];
CustomAdapter(Context c,String s[],int s3[])
{
super(c,R.layout.tcustom,s);
this.c1=c;
this.s1=s;
this.s2=s3;
}
public View getView(int position,View v,ViewGroup parent)
{
LayoutInflater li=(LayoutInflater) c1.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=li.inflate(R.layout.tcustom,null);
TextView tv=(TextView)v.findViewById(R.id.textView);
ImageView im=(ImageView)v.findViewById(R.id.imageview);
tv.setText(s1[position]);
im.setImageResource(s2[position]);
Button bt = (Button) v.findViewById(R.id.button);
bt.setTag(position); //important so we know which item to delete on button click
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
v.setVisibility(View.GONE);
notifyDataSetChanged();
int positionToRemove = (int)v.getTag(); //get the position of the view to delete stored in the tag
removeItem(positionToRemove); //remove the item
}
});
return v;
}
public void removeItem(int position){
//convert array to ArrayList, delete item and convert back to array
ArrayList<String> a = new ArrayList<>(Arrays.asList(s1));
a.remove(position);
String[] s = new String[a.size()];
s=a.toArray(s);
s1 = s;
notifyDataSetChanged(); //refresh your listview based on new data
}
public int getCount() {
return s1.length;
}
public String getItem(int position) {
return s1[position];
}}