【发布时间】:2017-05-11 16:53:07
【问题描述】:
我正在尝试将动态复选框文本传递给下一个活动。我想按 ID 遍历复选框,看看它们是否被选中。如果是,我想从复选框中获取文本并将其连接到一个字符串上。当我运行它时,应用程序崩溃了。我知道这是因为意图。这是针对学校截止日期的快速、简单的解决方法 - 任何帮助都将不胜感激。
编辑:对 addtocart() 方法进行了一些更改,但仍然无法正常工作。
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) {
}
}
【问题讨论】:
-
请将崩溃日志附加到问题中。
-
它只说“服务器连接已停止”,logcat 中没有记录错误
-
如果说是服务器,那么专注于通信代码(http请求)?权限,服务器 URL ?贴出http请求相关的代码。
标签: android