2012-07-11 16 views
10

Tôi muốn xóa một hàng nhất định khỏi listView khi nhấp vào ImageView. Chế độ xem danh sách của tôi trông giống như sau: enter image description hereremoveView (View) không được hỗ trợ trong AdapterView

Tôi muốn rằng khi hình ảnh cuối cùng được nhấp để xóa hàng đó. Dưới đây là bộ chuyển đổi của tôi:

public class UserItemAdapter extends ArrayAdapter<Photos.Record> { 
     private ArrayList<Photos.Record> photos; 

     public UserItemAdapter(Context context, int textViewResourceId, ArrayList<Photos.Record> photos) { 
      super(context, textViewResourceId, photos); 
      this.photos = photos; 
     } 

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


      if (v == null) { 
       LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       v = vi.inflate(R.layout.photorowlist, null); 
           v.setClickable(true); 
           v.setFocusable(true); 
      } 

      Photos.Record user = photos.get(position); 
      if (user != null) { 
       TextView photo_name = (TextView) v.findViewById(R.id.photoname); 

       if (photo_name != null) { 
        photo_name.setText(user.photo_name); 
       } 

      } 
         v.setOnClickListener(new OnClickListener() { 

         @Override 
         public void onClick(View view) { 
          //Toast.makeText(view.getContext(), "Clicked", Toast.LENGTH_SHORT).show(); 

          ImageView delete_photo = (ImageView) view.findViewById(R.id.deletephoto); 
          delete_photo.setOnClickListener(new OnClickListener(){ 
          @Override 
          public void onClick(View v) { 
           Toast.makeText(Photos.this, "Delete Button Clicked", Toast.LENGTH_SHORT).show(); 
           listView.removeView(v); 
           myadapter.notifyDataSetChanged(); 


       }}); 
         } 

         }); 


      return v; 
     } 
    } 

    public class Record { 
     public String photo_name; 

     public Record(String photo_name) { 
      this.photo_name = photo_name; 
     } 
    } 

tôi đã cố gắng để xóa các hàng sử dụng này:

listView.removeView(v); 
myadapter.notifyDataSetChanged(); 

và tôi nhận được lỗi: ERROR AndroidRuntime java.lang.UnsupportedOperationException: removeView(View) is not supported in AdapterView

đâu mystake của tôi? Bất kỳ ý tưởng?

Trả lời

20

Bạn không phải xóa View nhưng xóa các mục trong danh sách ảnh của mình.

photos.remove(yourPhoto); 
notifyDataSetChanged(); 

Hơn nữa, bạn nên sử dụng ViewHolders, có rất nhiều tuts trong Google. Hy vọng điều này sẽ giúp bạn.

+0

Không, nhưng tôi nghĩ bạn đang làm sai cách. Bạn không nên viết ClickListener của bạn trong bộ điều hợp của bạn nhưng trong hoạt động của bạn (hoặc đoạn). Sau đó, bạn đặt OnItemClickListener cho chế độ xem của mình và xóa ảnh của bạn tại đây. Tôi không chắc chắn rằng tôi rõ ràng, nhưng bạn nên có một cái nhìn trên [Cyril Mottier của blog] (http://android.cyrilmottier.com/?p=525) – AMerle

+0

Tôi đã thử giải pháp như vậy (loại bỏ dữ liệu từ danh sách bộ điều hợp mảng và sau đó làm mới) nhưng không hoạt động. Đã mở một vấn đề khác tại đây: http://stackoverflow.com/questions/38250987/removing-an-item-from-the-dropdown-list-of-a-baseadapter –

1

Bạn nên thử nó trên thread UI để nó hoạt động ngay lập tức:

 listView.remove(position); 
     runOnUiThread(new Runnable() { 
      public void run() { 
       adapter.notifyDataSetChanged(); 
      } 
     }); 
9

Trong onItemClick (AdapterView phụ huynh, Xem xem, vị trí int, id dài) sử dụng

parent.removeViewInLayout(view);

thay vì

parent.removeViewAt(position);