2016-07-12 152 views
0

我無法突出顯示所有選定項目。一次只能突出顯示一個項目。如果下一個項目被點擊,先前突出顯示的項目將恢復正常。ListView突出顯示所選項目

這裏是我的CustomAdapter

private class CustomAdapter extends ArrayAdapter<HashMap<String, Object>> { 

     public CustomAdapter(Context context, int textViewResourceId, ArrayList<HashMap<String, Object>> Strings) { 

      //let android do the initializing :) 
      super(context, textViewResourceId, Strings); 
     } 

     public void setSelectedIndex(int ind) 
     { 
      selectedIndex = ind; 
      notifyDataSetChanged(); 
     } 

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

     @Override 
     public HashMap<String, Object> getItem(int position) 
     { 
      return dataList.get(position); 
     } 

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

     //class for caching the views in a row 
     private class ViewHolder { 

      TextView not, isRead; 
      LinearLayout noti_linear; 
     } 

     //Initialise 
     ViewHolder viewHolder; 

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

      if (convertView == null) { 

       //inflate the custom layout 
       convertView = inflater.from(parent.getContext()).inflate(R.layout.notification_layout, parent, false); 
       viewHolder = new ViewHolder(); 

       //cache the views 
       viewHolder.noti_linear = (LinearLayout) convertView.findViewById(R.id.not_layout); 
       viewHolder.not = (TextView) convertView.findViewById(R.id.textview_noti); 
       viewHolder.isRead = (TextView) convertView.findViewById(R.id.textview_isRead); 

       //link the cached views to the convertview 
       convertView.setTag(viewHolder); 
      } else 
       viewHolder = (ViewHolder) convertView.getTag(); 

      //set the data to be displayed 
      viewHolder.not.setText(dataList.get(position).get("CheckList").toString()); 

      if(selectedIndex!= -1 && position == selectedIndex) 
      { 
       viewHolder.noti_linear.setBackgroundColor(Color.TRANSPARENT); 
      } 
      else 
      { 
       viewHolder.noti_linear.setBackgroundColor(Color.LTGRAY); 
      } 


//   if (getItemId(position) == StringHolder.mSelectedItem) { 
//    viewHolder.noti_linear.setBackgroundColor(Color.LTGRAY); 
// 
//   } else { 
//    viewHolder.noti_linear.setBackgroundColor(Color.TRANSPARENT); 
//   } 

      return convertView; 
     } 
    } 

這裏是我的onItemClick

public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 


       cardAdapter.setSelectedIndex(position); 
      } 
     }); 
+0

你可以嘗試做一個數組,包含所有已選擇的項目,然後像那樣使用它們 – MNM

+0

嘗試使用多選列表http://www.androidbegin.com/tutorial/android-delete-multiple-selected-items-listview-tutorial/和https ://techienotes.info/2015/09/13/how-to-select-multiple-items-in-android-listview/ –

+0

@MNM你能說明一下嗎?或者任何鏈接?謝謝 –

回答

1

你可以做下面的代碼,而不是在適配器類應用, 檢查下面的代碼,

public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

      parent.getChildAt(position).setBackgroundColor(Color.TRANSPARENT); 
      //cardAdapter.setSelectedIndex(position); 
     } 
    }); 
+0

感謝它的工作原理。 –

+0

享受你的編碼,你也可以通過你的方式來實現它,但是你需要聲明列表 selectedIndex而不是int,然後還需要在活動中聲明相同的內容,然後傳遞它,然後使用for循環,而不是這樣是更好的方法 – Vickyexpert

+0

有沒有什麼方法可以存儲這個選擇。如果我再次加載列表,我會丟失突出顯示的位置。 –

0
OnItemClickListener listViewOnItemClick = new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) { 
      mSelectedItem = position; 
      mAdapter.notifyDataSetChanged(); 
    } 
}; 


@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    final View view = View.inflate(context, R.layout.item_list, null); 

    if (position == mSelectedItem) { 
     // set your color 
    } 

    return view; 
}