2016-06-16 4 views
4

checkboxのチェック項目を選択しようとしているため、チェックボックスをオンにしてサーバーに送信します。Android:RecyclerViewのChecked項目を取得してサーバーに送信する方法

私のアダプタは次のとおりです。

public class FollowTopicsAdapter extends RecyclerView.Adapter<FollowTopicsAdapter.MyViewHolder> { 

    ArrayList<String> topics_title, topics_id; 
    ArrayList<String> checked_items= new ArrayList<>(); 
    Context context; 
    LinearLayout linearLayout; 
    CheckBox checkBox; 

    public FollowTopicsAdapter(Context context, ArrayList<String> topics_title, ArrayList<String> topics_id){ 
     this.topics_title=topics_title; 
     this.context=context; 
     this.topics_id=topics_id; 
    } 

    @Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_for_follow_topics,parent,false); 

     return new MyViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(final MyViewHolder holder, final int position) { 
     holder.checkBox.setText(topics_title.get(position)); 
     holder.checkBox.setTag(topics_title.get(position)); 

     holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton compoundButton, boolean b) { 
       if(b) 
       { 
        checked_items.add(position,topics_id.get(position)); 
        holder.checkBox.setChecked(b); 
        linearLayout.setBackgroundResource(R.drawable.checked); 
       } 
       else 
       { 
        checked_items.remove(position); 
        holder.checkBox.setChecked(b); 
        linearLayout.setBackgroundResource(R.drawable.custom_checkbox); 
       } 

      } 
     }); 
    } 


    @Override 
    public int getItemCount() { 
     return topics_title.size(); 
    } 

    public class MyViewHolder extends RecyclerView.ViewHolder { 
     CheckBox checkBox; 

     public MyViewHolder(View view) { 
      super(view); 
      linearLayout=(LinearLayout)itemView.findViewById(R.id.linearLayout); 
      checkBox= (CheckBox)itemView.findViewById(R.id.checkbox); 
     } 
    } 
} 
+0

だからあなたの質問は何ですが? – Egor

+0

アイテムをチェックすると、特定の位置にアイテムを追加する必要はありません。そのトピックIDを追加するだけで、そのトピックIDを削除する必要があります。 このリストにはチェックされたトピックが含まれています。 – user3024215

+0

@エゴル:私はリサイクラービューの項目をクリックすることができません –

答えて

4

とはい私は答えました:

public class AdapterForFollowTopics extends BaseAdapter { 

    private Context context; 
    ArrayList<String> topics_title, topics_id; 
    ArrayList<String> checked_items= new ArrayList<>(); 
    private static LayoutInflater inflater=null; 
    ArrayList<String> lstChk= new ArrayList<>(); 

    public AdapterForFollowTopics(Context context, ArrayList<String> topics_title, ArrayList<String> topics_id){ 
     this.context = context; 
     this.topics_title = topics_title; 
     this.topics_id= topics_id; 
     inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

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

    @Override 
    public Object getItem(int i) { 
     return topics_title.get(i); 
    } 

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

    public class Holder 
    { 
     CheckBox check; 
     LinearLayout lv; 
    } 



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

     final Holder holder = new Holder(); 
     final View rowView; 

     rowView = inflater.inflate(R.layout.layout_for_follow_topics, null); 
     holder.check = (CheckBox) rowView.findViewById(R.id.checkbox); 
     holder.lv = (LinearLayout) rowView.findViewById(R.id.linearLayout); 
     holder.check.setText(topics_title.get(position)); 
     rowView.setTag(holder); 

     if(lstChk.contains(topics_title.get(position))) 
     { 
      holder.check.setChecked(true); 
     } 

     holder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton compoundButton, boolean b) { 
       if(compoundButton.isChecked()) 
       { 
        compoundButton.setChecked(true); 
        lstChk.add(topics_title.get(position)); 
        holder.lv.setBackgroundResource(R.drawable.checked); 

       } 
       else 
       { 
        compoundButton.setChecked(false); 
        lstChk.remove(topics_title.get(position)); 
        holder.lv.setBackgroundResource(R.drawable.custom_checkbox); 
       } 
      } 
     }); 

     return rowView; 

    } 
} 
0

あなたがここに私のコード

https://github.com/masaliev/android-dynamic-table

を見ることができます。私はあなたがそれがどのように行われているかを理解することを願っています

+0

ありがとうハムザ!! –

+0

あなたがそれを気に入ってうれしいです。 –

関連する問題