美好的一天,
我正在尝试刷新我在android编程中的知识 通过使用自定义适配器列表创建一个简单的备忘录应用
我的问题是,即使我打电话给#34; NotifyDataSetChanged();",
我列表中删除的值始终是最后一个
虽然当我刷新它时,它会显示正确的一个。
这是我的代码:
public class CustomAdapter extends BaseAdapter{
List<Memo> memos;
Context context;
int [] imageId;
private static LayoutInflater inflater = null;
public CustomAdapter(MainActivity mainActivity, List<Memo> _memos) {
memos = _memos;
context = mainActivity;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return memos.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder
{
TextView tv;
TextView tv2;
Button deleteMemoBtn;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder mViewHolder = null;
if (convertView == null) {
mViewHolder = new Holder();
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.list_memo, parent, false);
mViewHolder.tv = (TextView) convertView.findViewById(R.id.textView1);
mViewHolder.tv2 = (TextView) convertView.findViewById(R.id.textView2);
mViewHolder.deleteMemoBtn = (Button) convertView.findViewById(R.id.deleteMemoBtn);
mViewHolder.tv.setText(memos.get(position).getMemoTitle());
mViewHolder.tv2.setText(Integer.toString(memos.get(position).getMemoID()));
mViewHolder.tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "You Clicked " + memos.get(position).getMemoTitle(), Toast.LENGTH_LONG).show();
}
});
mViewHolder.deleteMemoBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "deleted " + Integer.toString(position), Toast.LENGTH_LONG).show();
memos.remove(position);
notifyDataSetChanged();
}
});
}
return convertView;
}
}
希望有人可以帮我解释为什么这段代码无效 谢谢!
答案 0 :(得分:1)
试试这段代码。
public class CustomAdapter extends BaseAdapter {
List<Memo> memos;
Context context;
int[] imageId;
private LayoutInflater inflater = null;
public CustomAdapter(Context mainActivity, List<Memo> _memos) {
memos = _memos;
context = mainActivity;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return memos.size();
}
@Override
public Object getItem(int position) {
return memos.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder {
TextView tv;
TextView tv2;
Button deleteMemoBtn;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder mViewHolder = null;
if (convertView == null) {
mViewHolder = new Holder();
convertView = inflater.inflate(R.layout.list_memo, parent, false);
mViewHolder.tv = (TextView) convertView.findViewById(R.id.textView1);
mViewHolder.tv2 = (TextView) convertView.findViewById(R.id.textView2);
mViewHolder.deleteMemoBtn = (Button) convertView.findViewById(R.id.deleteMemoBtn);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (Holder) convertView.getTag();
}
mViewHolder.tv.setText(memos.get(position).getMemoTitle());
mViewHolder.tv2.setText(Integer.toString(memos.get(position).getMemoID()));
mViewHolder.tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "You Clicked " + memos.get(position).getMemoTitle(), Toast.LENGTH_LONG).show();
}
});
mViewHolder.deleteMemoBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "deleted " + Integer.toString(position), Toast.LENGTH_LONG).show();
memos.remove(position);
notifyDataSetChanged();
}
});
return convertView;
}
}