我创建了一个应用程序
在我的应用程序中,我创建了自定义ListView
,在列表视图中,我添加了一个CheckBox
,ImageView
和2个TextView
。现在我想在下一个被检查的活动中传递数据
我怎么能这样做?
ChannelListView.java
public class ChannelListView extends Activity implements OnItemClickListener {
CheckBox checkMarks;
ListView allChannels;
ArrayAdapter<DisplayChannelListData> adapter;
Button send, back;
DatabaseHelper dbHelper;
List<DisplayChannelListData> channelListData;
ChannelListView channelListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
channelListView = this;
dbHelper = new DatabaseHelper(getApplicationContext());
allChannels = (ListView) findViewById(R.id.listviewAllChannel);
send = (Button) findViewById(R.id.btnListSend);
back = (Button) findViewById(R.id.btnListBack);
adapter = new ListViewAdapter(this, getListViewModel());
allChannels.setAdapter(adapter);
allChannels.setOnItemClickListener(this);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ChannelListView.this, ChannelList.class);
startActivity(intent);
}
});
}
private List<DisplayChannelListData> getListViewModel() {
channelListData = dbHelper.displayChannelsList(channelListView);
return channelListData;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
checkMarks = (CheckBox) view.findViewById(R.id.chkAllCheckMark);
if (!checkMarks.isChecked()) {
checkMarks.setChecked(true);
} else {
checkMarks.setChecked(false);
}
}
}
ListViewAdapter.java
public class ListViewAdapter extends ArrayAdapter<DisplayChannelListData> {
private final List<DisplayChannelListData> list;
private final Activity context;
boolean checkAll_flag = false;
boolean checkItem_flag = false;
public ListViewAdapter(Activity context, List<DisplayChannelListData> list) {
super(context, R.layout.list_view, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView channelTitle, channelSubTitle;
protected ImageView channelImage;
protected CheckBox greenCheckMark;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.all_channel_listview_item, null);
viewHolder = new ViewHolder();
viewHolder.channelTitle = (TextView) convertView.findViewById(R.id.txtAllChannelName);
viewHolder.channelSubTitle = (TextView) convertView
.findViewById(R.id.txtAllChannelDesc);
viewHolder.channelImage = (ImageView) convertView.findViewById(R.id.imgAllChannelIcon);
viewHolder.greenCheckMark = (CheckBox) convertView.findViewById(R.id.chkAllCheckMark);
viewHolder.greenCheckMark
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
list.get(getPosition).setSelected(buttonView.isChecked());
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.chkAllCheckMark, viewHolder.greenCheckMark);
convertView.setTag(R.id.imgAllChannelIcon, viewHolder.channelImage);
convertView.setTag(R.id.txtAllChannelName, viewHolder.channelTitle);
convertView.setTag(R.id.txtAllChannelDesc, viewHolder.channelSubTitle);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.greenCheckMark.setTag(position);
viewHolder.greenCheckMark.setChecked(list.get(position).isSelected());
viewHolder.channelImage.setImageResource(list.get(position).getDrawable_path());
viewHolder.channelTitle.setText(list.get(position).getChannel_name());
viewHolder.channelSubTitle.setText(list.get(position).getChannel_desc());
return convertView;
}
}
DisplayChannelListData.java
public class DisplayChannelListData {
private boolean selected;
private int drawable_path;
private String channel_name;
private String channel_desc;
public DisplayChannelListData(int path,String name,String desc){
this.drawable_path = path;
this.channel_name = name;
this.channel_desc = desc;
}
public int getDrawable_path() {
return drawable_path;
}
public void setDrawable_path(int drawable_path) {
this.drawable_path = drawable_path;
}
public String getChannel_name() {
return channel_name;
}
public void setChannel_name(String channel_name) {
this.channel_name = channel_name;
}
public String getChannel_desc() {
return channel_desc;
}
public void setChannel_desc(String channel_desc) {
this.channel_desc = channel_desc;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
答案 0 :(得分:1)
在检查时维护一个ArrayList,如:
ArrayList<DisplayChannelListData> checkedChannelList=new ArrayList<DisplayChannelListData>();
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
checkMarks = (CheckBox) view.findViewById(R.id.chkAllCheckMark);
if (!checkMarks.isChecked()) {
checkMarks.setChecked(true);
checkedChannelList.add(channelListData.get(position));
} else {
checkMarks.setChecked(false);
checkedChannelList.remove(channelListData.get(position));
}
}
并且,在按钮上的onClickListener中,发送已检查频道列表列表:
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ChannelListView.this, ChannelList.class);
intent.putParcelableArrayListExtra("CHANNELSLIST", checkedChannelList);
startActivity(intent);
}
});