简体   繁体   English

Android-从列表活动返回数据

[英]Android- Returning data from list activity

My application has a main activity which can spawn one of four list activities, the goal is to have the user select items from the lists one after another and upon submit from the main activity push those choices to a database. 我的应用程序有一个主要活动,它可以产生四个列表活动之一,目标是让用户一个接一个地从列表中选择项目,并在从主要活动submit后将这些选择推送到数据库中。

Issue: I am unsure how to return and store the results of the lists in the main activity. 问题:我不确定如何在主要活动中返回并存储列表的结果。

I am not using a list view for the other activities as it wouldn't allow for a 'done' button on the same screen. 我不在其他活动中使用列表视图,因为它不允许在同一屏幕上使用“完成”按钮。

So far this is what I have 到目前为止,这就是我所拥有的

Main: 主要:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button1 = (Button) findViewById(R.id.listOneSub);
        button2 = (Button) findViewById(R.id.listTwoSub);
        button3 = (Button) findViewById(R.id.listThreeSub);
        button4 = (Button) findViewById(R.id.listFourSub);

        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
    }


    public void onClick(View v) {
        if (v == button1) {
            startActivity(new Intent("net.learn2develop.SecondActivity"));
        } else if (v == button2) {
            startActivity(new Intent("net.learn2develop.ThirdActivity"));
        } else if (v == button3) {
            startActivity(new Intent("net.learn2develop.FourthActivity"));
        } else if (v == button4) {
            startActivity(new Intent("net.learn2develop.FifthActivity"));
        }
    }

List Activities (all four are similar, they just have different items): 列出活动(所有四个都是相似的,只是有不同的项目):

String [] lstOne;

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondactivity);
        ListView lstView = (ListView)findViewById(R.id.android_listOne);
        lstView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        //lstView.setTextFilterEnabled(true);

        lstOne = getResources().getStringArray(R.array.one);

        lstView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, lstOne));

        }




    public void onClick(View view){
        ListView lstView = (ListView)findViewById(R.id.android_listOne);

        String itemsSelected =  "Selected items: \n";
        for(int i=0; i<lstView.getCount(); i++){
            if(lstView.isItemChecked(i)){
                itemsSelected += lstView.getItemAtPosition(i) + "\n";
            }
        }
        Toast.makeText(this, itemsSelected, Toast.LENGTH_LONG).show();
        finish();
    }

The lists are being generated from the strings.xml file. 这些列表是从strings.xml文件生成的。 I would like to update the main view under the buttons to display what has been chosen from each list before submitting. 我想更新按钮下的主视图,以显示提交之前从每个列表中选择的内容。

屏幕截图

Use startActivityForResult instead of startActivity and onActivityResult will be called on your main. 使用startActivityForResult而不是startActivityonActivityResult将在您的主设备上调用。

I would also suggest merging these 4 classes into 1 and sending identifier of string array you want to display. 我还建议将这4个类合并为1,然后发送要显示的字符串数组的标识符。 Code duplication is very bad. 代码重复非常糟糕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM