2016-11-29 5 views
-1

次のコードは、オブジェクトを選択して別のリストビューにボタンを押して表示する必要があるデータベースから取得したアイテムのリストです。 状況を参考にして助けてください。ありがとう!チェックボックスの付いたAndroidリストビュー

public class ParticipantsSelectedActivity extends AppCompatActivity implements View.OnClickListener{ 

DataBaseHandler myDb; 
ListView listViewParticipants; 
ParticipantsSelectedListAdapter participantsSelectedListAdapter; 
Participants participants; 
Button btn_save_participants_selected; 
CheckBox ckb; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_participants_selected); 

    myDb = new DataBaseHandler(this); 
    ckb = (CheckBox)findViewById(R.id.ckb); 

    listViewParticipants = (ListView) findViewById(R.id.listViewParticipantsSelected); 
    btn_save_participants_selected = (Button) findViewById(R.id.btn_save_participants_selected); 

    final ArrayList<Participants> listData = new ArrayList<>(); 
    final Cursor cursor = myDb.getListParticipants(); 

    if(cursor.getCount() == 0){ 
     Toast.makeText(this, "There is no participants", Toast.LENGTH_SHORT).show(); 
    }else { 
     while(cursor.moveToNext()){ 
      int id =(Integer.valueOf(cursor.getString(0))); 
      String firstName = cursor.getString(1); 
      String position = cursor.getString(3); 

      participants = new Participants(id,firstName,position, true); 
      listData.add(participants); 


      participantsSelectedListAdapter = new ParticipantsSelectedListAdapter(this,android.R.layout.simple_list_item_multiple_choice,listData); 
      listViewParticipants.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
      listViewParticipants.setAdapter(participantsSelectedListAdapter); 
      btn_save_participants_selected.setOnClickListener(this); 

     } 

    } 
     /* 

    listViewParticipants.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      Participants participants = listData.get(position); 

      Toast.makeText(getApplicationContext(), "Clicked on Id..." + participants.getId(), Toast.LENGTH_SHORT).show(); 
     } 
    }); 

    */ 

} 


@Override 
public void onClick(View v) { 
    //another activity to fill another listView 
} 



} 

これは、カスタムアダプタです

public class ParticipantsSelectedListAdapter extends ArrayAdapter<Participants> { 

public ParticipantsSelectedListAdapter(Context context,int a, ArrayList<Participants> participants) { 
    super(context, 0, participants); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // Get the data item for this position 
    Participants participants = getItem(position); 
    // Check if an existing view is being reused, otherwise inflate the view 
    if (convertView == null) { 
     convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_participants_selected, parent, false); 
    } 
    // Lookup view for data population 
    TextView tvFirstName = (TextView) convertView.findViewById(R.id.tvFirstName); 
    TextView tvPosition = (TextView) convertView.findViewById(R.id.tvPosition); 
    CheckBox ckb = (CheckBox) convertView.findViewById(R.id.ckb); 

    // Populate the data into the template view using the data object 
    tvFirstName.setText(participants.getFirstName()); 
    tvPosition.setText(participants.getPosition()); 
    ckb.setChecked(false); 

    // Return the completed view to render on screen 

    return convertView; 
} 

}

そして、これはXMLアイテムです

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <CheckBox 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:focusable="false" 
     android:id="@+id/ckb"/> 
    <TextView 
     android:id="@+id/tvFirstName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
    <TextView 
     android:id="@+id/tvPosition" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

答えて

0

参加者のためのモデルクラスでは、このような1つのブール変数を追加選択と選択解除を行う:

データベースのデータにParticipant.class

private boolean isSelected; 

public void setSelected(boolean selection){ 
this.isSelected = selection; 
} 

public boolean isSelected(){ 
this.isSelected; 
} 

は、あなたが何度も何度もそれは間違っているアダプタを設定している取得し、必要がやるんします。 参加者リストから選択項目を取得するためのOnClickイベントが追加されました。

public class ParticipantsSelectedActivity extends AppCompatActivity implements View.OnClickListener { 

    DataBaseHandler myDb; 
    ListView listViewParticipants; 
    ParticipantsSelectedListAdapter participantsSelectedListAdapter; 
    Participants participants; 
    Button btn_save_participants_selected; 
    CheckBox ckb; 
    final ArrayList<Participants> listData = new ArrayList<>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_participants_selected); 

     myDb = new DataBaseHandler(this); 
     ckb = (CheckBox) findViewById(R.id.ckb); 

     listViewParticipants = (ListView) findViewById(R.id.listViewParticipantsSelected); 
     btn_save_participants_selected = (Button) findViewById(R.id.btn_save_participants_selected); 


     final Cursor cursor = myDb.getListParticipants(); 

     if (cursor.getCount() == 0) { 
      Toast.makeText(this, "There is no participants", Toast.LENGTH_SHORT).show(); 
     } else { 
      while (cursor.moveToNext()) { 
       int id = (Integer.valueOf(cursor.getString(0))); 
       String firstName = cursor.getString(1); 
       String position = cursor.getString(3); 

       participants = new Participants(id, firstName, position, true); 
       listData.add(participants); 
      } 

      //Setting adapter after all data retrieve from database 
      participantsSelectedListAdapter = new ParticipantsSelectedListAdapter(this, android.R.layout.simple_list_item_multiple_choice, listData); 
      listViewParticipants.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
      listViewParticipants.setAdapter(participantsSelectedListAdapter); 
      btn_save_participants_selected.setOnClickListener(this); 

     } 
     /* 

    listViewParticipants.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      Participants participants = listData.get(position); 

      Toast.makeText(getApplicationContext(), "Clicked on Id..." + participants.getId(), Toast.LENGTH_SHORT).show(); 
     } 
    }); 

    */ 

    } 

    @Override 
    public void onClick(View v) { 
     //another activity to fill another listView 
     if (v.getId() == R.id.btn_save_participants_selected) { 
      ArrayList<Participants> selectedListData = new ArrayList<>(); 
      if (listData != null && listData.size() > 0) { 
       for (int i = 0; i < listData.size(); i++) { 
        if (listData.get(i).isSelected()) { 
         selectedListData.add(listData.get(i)); 
        } 
       } 
       //Pass this selectedListData to new activity to show a new Listview 
      } 
     } 

    } 
} 

選択と選択解除のためにアダプタコードが変更されました。

public ParticipantsSelectedListAdapter(Context context, int a, ArrayList<Participants> participants) { 
     super(context, 0, participants); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // Get the data item for this position 
     final Participants participants = getItem(position); 
     // Check if an existing view is being reused, otherwise inflate the view 
     if (convertView == null) { 
      convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_participants_selected, parent, false); 
     } 
     // Lookup view for data population 
     TextView tvFirstName = (TextView) convertView.findViewById(R.id.tvFirstName); 
     TextView tvPosition = (TextView) convertView.findViewById(R.id.tvPosition); 
     CheckBox ckb = (CheckBox) convertView.findViewById(R.id.ckb); 

     // Populate the data into the template view using the data object 
     tvFirstName.setText(participants.getFirstName()); 
     tvPosition.setText(participants.getPosition()); 

     if(participants.isSelected) { 
      ckb.setChecked(true); 
     }else { 
      ckb.setChecked(false); 
     } 

     ckb.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       participants.setSelected(!participants.isSelected()); 
       notifyDataSetChanged(); 
      } 
     }); 

     // Return the completed view to render on screen 

     return convertView; 
    } 
関連する問題