Android 带有复选框的列表视图填充了对象
Android Listview with checkbox fill with objects
以下代码是从数据库中获取的项目列表,我应该选择对象并通过按下按钮将它们显示在另一个列表视图中。
请在给定情况下帮助我提供建议。谢谢!
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>
在您的模型 class 中,为参与者添加一个这样的布尔变量以进行选择和取消选择:
在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;
}
以下代码是从数据库中获取的项目列表,我应该选择对象并通过按下按钮将它们显示在另一个列表视图中。 请在给定情况下帮助我提供建议。谢谢!
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>
在您的模型 class 中,为参与者添加一个这样的布尔变量以进行选择和取消选择:
在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;
}