2017-07-10 33 views
0

我有一個應用程序,其中包含與開關按鈕的列表視圖,當我想要從列表視圖中刪除項目時,我想要什麼,我已經嘗試了很多,但無法刪除項目。請幫助。如何在Android上打開時從列表視圖中刪除項目?

代碼適配器的: -

private LayoutInflater layoutInflater; 
private List<AppList> listStorage; 
private Context mContext; 
public AppAdapter(Context context, List<AppList> customizedListView) { 
    layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    listStorage = customizedListView; 
    this.mContext = context; 
} 

@Override 
public int getCount() { 
    return listStorage.size(); 
} 

@Override 
public Object getItem(int position) { 
    return position; 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

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

    ViewHolder listViewHolder; 
    if (convertView == null) { 
     listViewHolder = new ViewHolder(); 
     convertView = layoutInflater.inflate(R.layout.installed_app_list, parent, false); 

     listViewHolder.textInListView = (TextView) convertView.findViewById(R.id.list_app_name); 
     listViewHolder.imageInListView = (ImageView) convertView.findViewById(R.id.app_icon); 
     listViewHolder.switchCompat = (SwitchCompat) convertView.findViewById(R.id.toggleButton); 
     convertView.setTag(listViewHolder); 
    } else { 
     listViewHolder = (ViewHolder) convertView.getTag(); 
    } 
    listViewHolder.textInListView.setText(listStorage.get(position).getName()); 
    listViewHolder.imageInListView.setImageDrawable(listStorage.get(position).getIcon()); 
    listViewHolder.switchCompat.setTag(position); 

    listViewHolder.switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      if (isChecked) { 
       listStorage.get(position).getPackName(); 
       //here i want to remove item from listview 
      } else { 
       Toast.makeText(mContext, "Off", Toast.LENGTH_LONG).show(); 
      } 
     } 
    }); 
    return convertView; 
} 

static class ViewHolder { 
    SwitchCompat switchCompat; 
    TextView textInListView; 
    ImageView imageInListView; 
} 

}

+0

刪除項目:listStorage.get(position).remove();在那之後調用notifyDataSetChanged – Payal

+0

它在remove()中顯示「無法解析方法」 – Niraj

+0

我正在擴展BaseAdapter – Niraj

回答

3

刪除元素下面所提到: 從listStorage採取一個新的列表中刪除的元素添加到新陣列,加上他們在全球範圍

List<AppList> newarr = new ArrayList<AppList>(); 
int newarr_pos = 0; 

更新的代碼

listViewHolder.switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      if (isChecked) { 
       // Add your element to new array 
       AppList model = datalist0.get(position); 
       newarr.add(newarr_pos,model); 
       newarr_pos++; 
       //Then remove it from previous array 
       listStorage.remove(position); 
       notifyDataSetChanged(); 
       //here i want to remove item from listview 
      } else { 
       Toast.makeText(mContext, "Off", Toast.LENGTH_LONG).show(); 
      } 
     } 
    }); 
+0

感謝南希它的工作.....一件事,如果我想添加移除到另一個數組列表的位置項目,那麼我該怎麼做 – Niraj

+0

是的,它的工作原理,但在我的意見提供我解決方案的問題 – Niraj

+0

嘿我已更新代碼acc。根據你的要求進行檢查 –

0

1.去除由位置

listStorage.remove(position); 
notifyDataSetChanged(); 
  • 去除由對象

    listStorage.remove(listStorage.get(position)); 
    
    notifyDataSetChanged(); 
    
  • +0

    哪裏是arrayadapter在我的代碼 – Niraj

    +0

    這是你的適配器的名稱。 。不管是什麼 。 –

    相關問題