将动态复选框文本传递到下一个活动(android)
我正在尝试将动态复选框文本传递给下一个活动。我想通过ID循环检查复选框,看看它们是否被选中。如果是,我想从复选框中提取文本并将其连接到字符串中。当我运行此程序时,应用程序崩溃。我知道这是出于意图。这是一个快速,简单的学校期限修复-任何帮助将不胜感激 编辑:对addtocart()方法进行了一些更改,但仍然不起作用将动态复选框文本传递到下一个活动(android),android,Android,我正在尝试将动态复选框文本传递给下一个活动。我想通过ID循环检查复选框,看看它们是否被选中。如果是,我想从复选框中提取文本并将其连接到字符串中。当我运行此程序时,应用程序崩溃。我知道这是出于意图。这是一个快速,简单的学校期限修复-任何帮助将不胜感激 编辑:对addtocart()方法进行了一些更改,但仍然不起作用 public class SearchResultsActivity extends Activity { public int count; @Override p
public class SearchResultsActivity extends Activity {
public int count;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_results);
Intent intent = getIntent();
String results = intent.getStringExtra("key");
if (results.equals("No results")) {
//do nothing
} else {
List<String> list = new ArrayList<String>(Arrays.asList(results.split("-")));
count = list.size() / 3;
//puthere.setText(list.get(0));
LinearLayout linear = (LinearLayout) findViewById(R.id.horizontal_ll);
int isbn = 1;
int title = 0;
int price = 2;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < count; i++) {
CheckBox checkBox = new CheckBox(this);
checkBox.setText("Title: " + list.get(isbn) + ", ISBN: " + list.get(title) + ", Price: " + list.get(price));
checkBox.setId(i);
checkBox.setLayoutParams(params);
linear.addView(checkBox);
isbn = isbn + 3;
title = title + 3;
price = price + 3;
}
}
}
public void addtocart(View v) {
String checked = "";
ViewGroup viewgroup=(ViewGroup) v.getParent();
int count = viewgroup.getChildCount();
for (int i = 0; i < count; i++) {
Object child = viewgroup.getChildAt(i);
if (child instanceof CheckBox) {
CheckBox checkboxchild = (CheckBox) child;
if(checkboxchild.isChecked()) {
String text = checkboxchild.getText().toString();
checked = checked+"-"+text;
}
}
Intent intent_name = new Intent(this, CheckOutActivity.class);
intent_name.putExtra("somethingnew", checked);
this.startActivity(intent_name);
}
}
public void test(View v) {
}
}
公共类SearchResultsActivity扩展活动{
公共整数计数;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u search\u results);
Intent=getIntent();
字符串结果=intent.getStringExtra(“键”);
如果(结果等于(“无结果”)){
//无所事事
}否则{
列表列表=新的ArrayList(Arrays.asList(results.split(“-”));
count=list.size()/3;
//setText(list.get(0));
LinearLayout linear=(LinearLayout)findViewById(R.id.horizontal_ll);
int-isbn=1;
int title=0;
国际价格=2;
LinearLayout.LayoutParams params=新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_内容,ActionBar.LayoutParams.WRAP_内容);
for(int i=0;i
您可能应该首先了解视图ID在Android上的工作原理。不能在for循环中循环通过增量viewIds;这样不行
循环的一种可能方式是:
ViewGroup viewgroup=(ViewGroup) view.getParent();
int count = viewgroup.getchildcount();
for (int i = 0; i < count; i++) {
if (viewGroup.getChildAt(i) instanceOf Checkbox) {
if (((Checkbox) viewGroup.getChildAt(i)).isChecked()) {
...
}
}
}
ViewGroup ViewGroup=(ViewGroup)view.getParent();
int count=viewgroup.getchildcount();
for(int i=0;i
强烈建议您先阅读有关视图在Android中如何工作的文档。k谢谢!我真的只是在寻找一个快速的答案,以尽可能地了解这一点,否则我今晚会做更多的研究。Anoop,你能解释一下你的代码与OP的代码有什么不同吗?同样,OP在
索引上循环
而不是ID
!是的,是的,我检查了问题的第一个版本,它包含处理视图ID的代码。请将崩溃日志附加到问题。它所说的是“服务器连接已停止”,日志中没有记录错误。如果它说是服务器,则重点关注通信代码(http请求)?权限,服务器URL?发布与http请求相关的代码。