-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>