1
我同時從ListView中刪除項目有問題,如何從Android的ListView中正確刪除一個項目?
這是我的活動:
public class TestListItemsView1Activity extends ListActivity {
/** Called when the activity is first created. */
private EfficientAdapter efficientAdapter;
private String[] data = new String[] { "BBC", "Yahoo", "CNN", "Eenadu", "Hindu" };
private List<String> dataValues = Arrays.asList(data);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
efficientAdapter = new EfficientAdapter(this,dataValues);
setListAdapter(efficientAdapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
//dataValues.remove(position);
//efficientAdapter.notifyDataSetChanged();
Toast.makeText(this, "Click-" + String.valueOf(position), Toast.LENGTH_SHORT).show();
}
}
和適配器類:
public class EfficientAdapter extends BaseAdapter implements Filterable{
private LayoutInflater mInflater;
private static String TAG=EfficientAdapter.class.getSimpleName();
private Context context;
private HashMap<String,EfficientAdapter.ViewHolder> holders= new HashMap<String, EfficientAdapter.ViewHolder>();
//private String[] dataArray = new String[] { "BBC", "Yahoo", "CNN", "Eenadu", "Hindu" };
private List<String> data= new ArrayList<String>();//Arrays.asList(dataArray);
public EfficientAdapter(Context context,List<String> data) {
//super(context, R.layout.main, values);
mInflater = LayoutInflater.from(context);
this.context=context;
this.data.addAll(data);
}
public EfficientAdapter(Context context) {
//super(context, R.layout.main, values);
mInflater = LayoutInflater.from(context);
this.context=context;
//data.addAll(data);
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = mInflater.inflate(R.layout.adaptor_content, null);
holder = new ViewHolder();
holder.textLine = (TextView) convertView.findViewById(R.id.textLine);
holder.buttonLine =(Button) convertView.findViewById(R.id.buttonLine);
holder.textLine.setText(this.data.get(position));
convertView.setOnClickListener(new OnClickListener() {
private int pos= position;
public void onClick(View v) {
Toast.makeText(context, "Click-" + String.valueOf(pos), Toast.LENGTH_SHORT).show();
}
});
holder.buttonLine.setOnClickListener(new OnClickListener() {
private int pos= position;
public void onClick(View v) {
/*ViewHolder deleteHolder = (ViewHolder)v.getTag();
int delPosition=deleteHolder.position;
holders.remove(delPosition);
ViewHolder currentHolder;
for(int i=pos+1;i<getCount();i++){
currentHolder = holders.get(delPosition);
currentHolder.position=i-1;
}
data.remove(delPosition);
notifyDataSetChanged();*/
//View convertView1 = mInflater.inflate(R.layout.adaptor_content, null);
// ViewHolder deleteHolder = (ViewHolder)v.getTag();
data.remove(getItem(pos));
notifyDataSetChanged();
//final ArrayAdapter adapter = ((ArrayAdapter)getListAdapter());
//ListView lv = v.findViewById(R.layout.)
//v.invalidate();
Log.i(TAG, "Delete button pressed at position" + pos);
Toast.makeText(context, "Delete-" + String.valueOf(pos), Toast.LENGTH_SHORT).show();
}
});
//holder.position=position;
//holders.put(Integer.toString(position), holder);
convertView.setTag(holder);
} else {
holder =(ViewHolder) convertView.getTag();
}
return convertView;
}
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return data.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public Filter getFilter() {
// TODO Auto-generated method stub
return null;
}
static class ViewHolder {
TextView textLine;
ImageView iconLine;
Button buttonLine;
//int position;
}
}
每當其刪除最後一個項目不是其中之一點擊。
已經移動到其他條件,但仍然相同 – radhakrishna 2012-03-19 17:10:57
怎麼樣添加活動一個OnClickItemListener,然後調用'adapter.remove(int position)'。你必須在適配器中創建這個方法爲public,並執行'data.remove(position)'。那個怎麼樣? – youchy 2012-03-19 17:16:15
仍然不能正常工作 – radhakrishna 2012-03-19 17:28:59