2012-03-19 89 views
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; 
     } 

} 

每當其刪除最後一個項目不是其中之一點擊。

回答

2

enter code here我認爲你沒有更新視圖的位置。

當你做if (convertView == null) { inflate........}你說的第一次創建視圖,必須做到這一點。但在Android中,ListView中的視圖是可重用的,所以如果您需要更新視圖的位置,則必須放棄此條件。

嘗試將ClickLsitener設置爲if (convertView = null) ...在else中。 試試吧......告訴我

public class EfficientAdapter extends BaseAdapter implements Filterable{ 

private LayoutInflater mInflater; 
private static String TAG=EfficientAdapter.class.getSimpleName(); 
private Context context; 

//private String[] dataArray = new String[] { "BBC", "Yahoo", "CNN", "Eenadu", "Hindu" }; 
private List<String> data= new ArrayList<String>(); 

public EfficientAdapter(Context context,List<String> data) { 
    super(); 
    mInflater = LayoutInflater.from(context); 
    this.context=context; 
    this.data.addAll(data); 
} 

public EfficientAdapter(Context context) { 
    super(); 
    mInflater = LayoutInflater.from(context); 
    this.context=context; 
} 
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); 
     convertView.setTag(holder); 

    } 
    holder =(ViewHolder) convertView.getTag(); 
    holder.textLine.setText(getItem(position)); 
    convertView.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      Toast.makeText(context, "Click-" + String.valueOf(position), Toast.LENGTH_SHORT).show(); 

     } 
    }); 
    holder.buttonLine.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      data.remove(position); 
      notifyDataSetChanged(); 
      Log.i(TAG, "Delete button pressed at position" + position); 
      Toast.makeText(context, "Delete-" + String.valueOf(position), Toast.LENGTH_SHORT).show(); 
     } 
    }); 

    return convertView; 
} 


public int getCount() { 
    // TODO Auto-generated method stub 
    return data.size(); 
} 

public String getItem(int position) { 
    // TODO Auto-generated method stub 
    return data.get(position); 
} 

public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return 0; 
} 

static class ViewHolder { 
    TextView textLine; 
    ImageView iconLine; 
    Button buttonLine; 
    //int position; 
} 

@Override 
public android.widget.Filter getFilter() { 
    return null; 
} 

}

工作!測試它:)

+0

已經移動到其他條件,但仍然相同 – radhakrishna 2012-03-19 17:10:57

+0

怎麼樣添加活動一個OnClickItemListener,然後調用'adapter.remove(int position)'。你必須在適配器中創建這個方法爲public,並執行'data.remove(position)'。那個怎麼樣? – youchy 2012-03-19 17:16:15

+0

仍然不能正常工作 – radhakrishna 2012-03-19 17:28:59

相關問題