2016-07-12 20 views
0

選択した項目をすべて強調表示できません。一度に1つの項目だけが強調表示されます。次のアイテムがクリックされると、前の強調表示されたアイテムは正常に戻ります。ここで 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

あなたのコーディングをお楽しみください、あなたもそれを達成することができますが、そのためには、リスト intの代わりにselectedIndex宣言する必要がありますまた、同じアクティビティを宣言し、それを渡して、より良い方法です – 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; 
} 
関連する問題