我有自定义列表查看: 我想在我的应用程序中,单击按钮,删除项目列表视图
我不明白如何编辑我班级中的方法以进行删除 项目自定义列表视图... 请帮我写一个删除项目的方法.. 谢谢你
java代码是:
public class MainActivity extends Activity{
String[] name;
String[] family;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name=new String[]{"aaa","bbb","samad","yoset","hasan"};
family=new String[]{"AAA","Ahamadi","Ahamadi","Ahamadi","Ahamadi"};
ListView list=(ListView) findViewById(R.id.list_view);
MyAdapter adapter=new MyAdapter(getApplicationContext());
list.setAdapter(adapter);
}
private class MyAdapter extends BaseAdapter
{
LayoutInflater MyInflater;
public MyAdapter(Context context)
{
MyInflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return name.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder X=new ViewHolder();
View row=null;
Object user = getItem(position);
user.
if(convertView==null)
{
convertView=MyInflater.inflate(R.layout.list_item,null);
X.Name=(TextView) convertView.findViewById(R.id.Text_name);
X.Family=(TextView) convertView.findViewById(R.id.Text_family);
convertView.setTag(X);
}
else{
X=(ViewHolder)convertView.getTag();
}
X.Name.setText(name[position]);
X.Family.setText(family[position]);
// TODO Auto-generated method stub
return convertView;
}
//-class Holder
public class ViewHolder
{
TextView Name;
TextView Family;
}
}
}
我不明白如何编辑我班级中的方法以进行删除 项目自定义列表视图... 请帮我写一个删除项目的方法.. 谢谢你
答案 0 :(得分:0)
Object item = adapter.getItem(position);
adapter.remove(item);
adapter.notifyDataSetChanged();
答案 1 :(得分:0)
您正在扩展BaseAdapter,这就是为什么您没有获得remove方法,它在ArrayAdapter,SimplerAdapter中可用。不要担心在自定义适配器中编写自己的删除方法 -
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name=new String[]{"aaa","bbb","samad","yoset","hasan"};
family=new String[]{"AAA","Ahamadi","Ahamadi","Ahamadi","Ahamadi"};
List<String> nameList = new ArrayList<String>(Arrays.asList(name));
List<String> familyList = new ArrayList<String>(Arrays.asList(family));
ListView list=(ListView) findViewById(R.id.list_view);
MyAdapter adapter=new MyAdapter(getApplicationContext());
list.setAdapter(adapter);
}
private class MyAdapter extends BaseAdapter{
LayoutInflater MyInflater;
public MyAdapter(Context context)
{
MyInflater=LayoutInflater.from(context);
}
public void remove(int pos)
{
nameList.remove(pos);
familyList.remove(pos);
}
@Override
public int getCount() {
return nameList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder X=new ViewHolder();
View row=null;
Object user = getItem(position);
if(convertView==null)
{
convertView=MyInflater.inflate(R.layout.list_item,null);
X.Name=(TextView) convertView.findViewById(R.id.Text_name);
X.Family=(TextView) convertView.findViewById(R.id.Text_family);
convertView.setTag(X);
}
else{
X=(ViewHolder)convertView.getTag();
}
X.Name.setText(nameList.get(position));
X.Family.setText(familyList.get(position));
// TODO Auto-generated method stub
return convertView;
}
//-class Holder
public class ViewHolder
{
TextView Name;
TextView Family;
}
}
现在删除项目使用此代码 -
((MyAdapter)list.getAdapter()).remove(position);
((MyAdapter)list.getAdapter()).notifyDataSetChanged();
并且您的适配器设计不太好,请尝试改进它。并使用ArrayList作为适配器而不是数组,它更灵活。
如果它解决了您的问题,请不要忘记标记为正确。