【问题标题】:Pass dynamic checkbox text to next activity (android)将动态复选框文本传递给下一个活动(android)
【发布时间】: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


【解决方案1】:

您可能应该首先阅读视图 ID 在 Android 上的工作原理。您不能在 for 循环中遍历增量 viewId;它不是那样工作的。

一种可能的循环方式是这样做:

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()) {
         ...
       }
    }
}

强烈建议您先阅读有关视图如何在 Android 中工作的文档。

【讨论】:

  • 谢谢!我真的只是在寻找一个快速的答案,以尽我所能,否则我今晚会做更多的研究。
  • Anoop,您能解释一下您的代码与 OP 的代码有何不同吗? OP也循环index而不是ID
  • 是的,没错,我检查了问题的第一版,它包含处理视图 ID 的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多