活动A 有listView
,活动B 有checkBox
并保存button
。
单击 B 中的保存button
后,它将返回到活动A.当按下活动A中的列表时,将检入如何显示check box
如果在返回 A 之前检查了活动B ,
活动B
if(getIntent().getExtras()!=null) // when Activity A list is pressed
{
final String from = getIntent().getStringExtra("from");
travelFrom.setText(from);
// how to show check box is checked ?
}
save.setOnClickListener(new View.OnClickListener() { // if save button clicked, return from and outstation to A
@Override
public void onClick(View v) {
String from=travelFrom.getText().toString();
returnIntent.putExtra("from", from);
if(checkbox.isChecked()) {
returnIntent.putExtra("outstation", checkbox.getText().toString());
}
}
});
活动A
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { // if listView is clicked
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
mClickedPosition = position;
Object o = listview.getItemAtPosition(position);
SearchResults fullObject = (SearchResults) o;
String from=fullObject.getFrom();
Intent i = new Intent(getApplication(),B.class);
i.putExtra("from",from);
startActivityForResult(i, PROJECT_REQUEST_CODE);
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { // receive from Activity B and populate ListView A
if (resultCode == RESULT_OK) {
if (requestCode == PROJECT_REQUEST_CODE) {
from=data.getStringExtra("from");
check=data.getStringExtra("outstation");
objMyCustomBaseAdapter.addNewItem(from,check); // save from and "outstation" into MySQL
}
}
}
当我按下A到B中的列表时,始终不会选中复选框。如何检查?
答案 0 :(得分:2)
执行类似于为复选框实现setOnclicklistner的操作。并假设一个int变量为零。当我们点击复选框时,每次都会将数字增加1。当我们从一个活动移动到另一个活动时,将int变量的数据发送到活动B.检查活动将条件int变量检查为变量%2如果值等于1,则选中复选框。否则它不是。
答案 1 :(得分:1)
从活动B返回活动A时,您必须保存所选列表项的已检查状态。
为此,您只需在活动A中存储当前所选列表项的信息即可。
(例如TreeMap<View, Boolean>
- item-&gt; isSelected)
然后在onActivityResult中,您可以设置是否对最后选择的项目进行了检查。再次选择时,在创建意图之前,您可以检查是否已选择此项目。
答案 2 :(得分:1)
要知道ActivityA ListItem是否在ActivityB中检查(即CheckBox)。请查看下面的代码希望这有帮助
ActivityA 东西
struct leo
ActivityB 东西
int selectedPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//your UI setup and findViewById stuff
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectedPosition = position;
startActivityForResult(activityBIntent, REQ_CODE);
}
});
}
int REQ_CODE = 7;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQ_CODE)
if (resultCode == RESULT_OK) {
adapter.changeStatus(selectedPosition, true);//update adapter
} else {
//checkbox not checked
adapter.changeStatus(selectedPosition, false);//update adapter
}
}
答案 3 :(得分:1)
假设你有一些对象......
class MyData {
private boolean check;
public boolean getCheck() { return check; }
}
这是一些粗略的 伪代码 来演示这个概念,所以 不 复制粘贴值得< / p>
活动A - 传入您想要进行下一个活动的布尔值
class ActivityA {
private ListView listView
onCreate() {
listView = (ListView) findViewById(R.id.list);
ArrayAdapter adapter = new ArrayAdapter();
listView.setAdapter(adapter);
listView.setOnItemClickListener( new ItemClickListener() {
onClick(int position) {
MyData listObject = adapter.getItem(position);
boolean checked = listObject.getCheck();
Intent i = new Intent(ActivityA.this, ActivityB.class);
i.putExtra("checkTheBox", check);
startActivity(i);
}
});
}
}
活动B - 从意图中获取值并将复选框设置为选中
class ActivityB {
private Checkbox checkbox;
onCreate() {
checkbox = (Checkbox) findViewById(R.id.checkbox);
Bundle extras = getIntent().getExtras();
if (extras != null) {
checkbox.setChecked(extras.getBooleanExtra("checkTheBox"));
}
}
}