0

使用具有a的數組適配器來顯示線性佈局列表。佈局有3個文本字段和一個隱藏按鈕,當點擊列表項時顯示。我試圖做的是將一個onclicklistener附加到按鈕,將從數組列表/適配器中刪除該行並更新數據集。我有以下代碼:無法更新ArrayAdapter中的數據

public class CheckAdapter extends ArrayAdapter<Check> { 
    Context context; 
    int layoutResourceId; 
    ArrayList<Check> data = null; 

    public CheckAdapter(Context context, int resource, ArrayList<Check> objects) { 
     super(context, resource, objects); 
     this.context = context; 
     this.layoutResourceId = resource; 
     this.data = objects; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder = null; 

     if(convertView == null){ 
      LayoutInflater inflater = ((Activity)context).getLayoutInflater(); 
      convertView = inflater.inflate(layoutResourceId, parent, false); 

      holder = new ViewHolder(convertView.findViewById(R.id.checklistRoot)); 
      holder.checkName = (TextView) convertView.findViewById(R.id.checklistname); 
      holder.checkNumber = (TextView) convertView.findViewById(R.id.checklistnumber); 
      holder.checkTotal = (TextView) convertView.findViewById(R.id.checklisttotal); 
      holder.deleteButton = (Button) convertView.findViewById(R.id.deletecheck); 

      holder.swipeButtons(); 

      convertView.setTag(holder); 
     } 
     else 
     { 
      holder = (ViewHolder)convertView.getTag(); 
      holder.swipeButtons(); 
     } 

     String name = data.get(position).getName(); 
     Double total = data.get(position).getAmmount(); 
     Long number = data.get(position).getCheckNumber(); 

     holder.checkName.setText(name); 
     holder.checkNumber.setText(number.toString()); 
     holder.checkTotal.setText(total.toString()); 

     holder.deleteButton.setTag(position); 
     holder.deleteButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Integer position = (Integer) view.getTag(); 
       Log.d("Check Adapter", "Button Clicked: " + position); 
       data.remove(position); 
       notifyDataSetChanged(); 
      } 
     }); 
     return convertView; 

    } 

    static class ViewHolder{ 
     TextView checkName; 
     TextView checkNumber; 
     TextView checkTotal; 
     Button deleteButton; 
     View rootView; 


     ViewHolder(View rootView) { 
      this.rootView = rootView; 
     } 

     public void swipeButtons(){ 
      rootView.setOnTouchListener(new View.OnTouchListener() { 
       @Override 
       public boolean onTouch(View view, MotionEvent motionEvent) { 
        deleteButton.setVisibility(View.VISIBLE); 
        return false; 
       } 
      }); 
     } 
    } 
} 

我的日誌顯示我有正確的位置被髮送到data.remove(位置);然而,ListView似乎沒有更新,數據似乎仍然存在。有什麼建議麼?

非常感謝!

+0

ListView listvw =(ListView)parent; listvw.invalidateViews(); – 2014-10-02 05:36:12

+0

你可以使用接口並在arrayadapter構造函數中傳遞它的實例。在deletebutton clicklisterner中,您可以調用此接口並向活動發送消息,在活動中,您可以在adapater實例上調用notifyDataSetChanged()。 – 2014-10-02 05:59:12

回答

5

您也可以嘗試使用getView方法本身提供的位置值。 也從getView方法中刪除行holder.deleteButton.setTag(position);

所以你的代碼會像下面:

@Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     ViewHolder holder = null; 

     // --- Your other code ---// 


     holder.deleteButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       data.remove(position); 
       notifyDataSetChanged(); 
      } 
     }); 
     return convertView; 

    } 

希望這會幫助你。

+1

這工作!我猜data.remove(整數)而不是int是罪魁禍首。非常感謝。 – 2014-10-02 06:01:30

+0

歡迎! @BrentAureli – AndyN 2014-10-02 06:04:48

+0

@ User22791太棒了! – Piyush 2014-10-02 06:15:43