仅将一部分数据显示到列表视图中并获取新活动 android studio 中的所有值?

Posted

技术标签:

【中文标题】仅将一部分数据显示到列表视图中并获取新活动 android studio 中的所有值?【英文标题】:Show only one part of data into listview and get all values in new activity android studio? 【发布时间】:2020-08-04 11:36:33 【问题描述】:

我有一个问题。 我是 android studio 的新手,尝试了很多不同的测试方法。 现在我制作了一个小程序,我可以在其中单击一个按钮,它会使用一个列表视图启动一个新活动,该列表视图向我显示从 json 数据中提取的 mysql 中的一些数据。

到目前为止,当您单击列表视图中的产品时,它会打开一个带有 textview 的新活动测试,并显示列表视图中过去的数据。

我的问题如下。

我想从列表视图中的选定行加载所有数据并在新活动中显示该数据。

但是我该怎么做呢?

我对 loadintolistview 的代码部分是这样的。:

    private void loadIntoListView(String json) throws JSONException 
    JSONArray jsonArray = new JSONArray(json);
    String[] stocks = new String[jsonArray.length()];
    //String[] stockstot = new String[jsonArray.length()];
    for (int i = 0; i < jsonArray.length(); i++) 
        JSONObject obj = jsonArray.getJSONObject(i);
        stocks[i] = obj.getString("DatumTot") + " " +  obj.getString("Product")+ " " + obj.getString("Prod_Nr")+ " " + obj.getString("Batch_Nr")+ " " + obj.getString("Orders")+ " " + obj.getString("Lijn")+ " " + obj.getString("FP");
        //stockstot[i] = obj.getString("DatumTot") + " " +obj.getString("Product")+ " " +obj.getString("Prod_Nr")+ " " +obj.getString("Batch_Nr")+ " " +obj.getString("Orders")+ " " +obj.getString("Lijn")+ " " +obj.getString("FP")+ " " +obj.getString("PeroxideType")+ " " +obj.getString("PeroxideProcent")+ " " +obj.getString("FlakeA")+ " " +obj.getString("FlakeSiloA")+ " " +obj.getString("FlakeB")+ " " +obj.getString("FlakeSiloB")+ " " +obj.getString("AggloProcent")+ " " +obj.getString("AggloSilo")+ " " +obj.getString("RecycleProcent")+ " " +obj.getString("RecycleSilo")+ " " +obj.getString("MVRml10ml")+ " " +obj.getString("MVRSTD")+ " " +obj.getString("MFRg10ml")+ " " +obj.getString("AshProcent")+ " " +obj.getString("VochtProcent")+ " " +obj.getString("KleurL")+ " " +obj.getString("KleurA")+ " " +obj.getString("KleurB")+ " " +obj.getString("Operator1")+ " " +obj.getString("FlakeType1")+ " " +obj.getString("FlakeType2")+ " " +obj.getString("Productielijn1")+ " " +obj.getString("Productielijn2")+ " " +obj.getString("FlakeBatchNr1")+ " " +obj.getString("FlakeBatchNr2")+ " " +obj.getString("FlakeVochtProcent1")+ " " +obj.getString("FlakeVochtProcent2")+ " " +obj.getString("FlakeKleurL1")+ " " +obj.getString("FlakeKleurL2")+ " " +obj.getString("FlakeKleurA1")+ " " +obj.getString("FlakeKleurA2")+ " " +obj.getString("FlakeKleurB1")+ " " +obj.getString("FlakeKleurB2")+ " " +obj.getString("Remark1")+ " " +obj.getString("Remark2")+ " " +obj.getString("Remark3")+ " " +obj.getString("MeltDens")+ " " +obj.getString("Reg_sample");

    
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, stocks);
    listView.setAdapter(arrayAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
            String selectedItem = (String) parent.getItemAtPosition(position);

            //Create the bundle
            Bundle bundle = new Bundle();

            //Add your data to bundle
            bundle.putString("stuff", selectedItem);

            Toast.makeText(getApplicationContext(), selectedItem, Toast.LENGTH_LONG).show();//display the text of button1
            Intent appInfo = new Intent(Extrusie1Activity.this, Extrusion1infoActivity.class);

            //Add the bundle to the intent
            appInfo.putExtras(bundle);

            startActivity(appInfo);
        
    );


如您所见,带有 String[] stocktot 的部分是我希望在新活动中看到的所有数据。 有人可以帮助我吗?

【问题讨论】:

启用 String[] stocktot 和 onItemClick 的注释代码,将 String selectedItem = (String) parent.getItemAtPosition(position); 更改为 String selectedItem = stockstot[position]; 希望有帮助! 【参考方案1】:

当你点击一个你已经访问过字符串的项目时,只需通过意图将它传递给下一个活动,如下所示:

更新

这样做:

 ..........
 JSONArray jsonArray = new JSONArray(json);
 //add this
 List<String> items = new ArrayList<String>();
 String[] stocks = new String[jsonArray.length()];

循环

for (int i = 0; i < jsonArray.length(); i++) 

    JSONObject obj = jsonArray.getJSONObject(i);
    stocks[i] = obj.getString("DatumTot") + " " +  obj.getString("Product")+ " " + obj.getString("Prod_Nr")+ " " + obj.getString("Batch_Nr")+ " " + obj.getString("Orders")+ " " + obj.getString("Lijn")+ " " + obj.getString("FP");


    String full = obj.getString("DatumTot") + " " +obj.getString("Product")+ " " +obj.getString("Prod_Nr")+ " " +obj.getString("Batch_Nr")+ " " +obj.getString("Orders")+ " " +obj.getString("Lijn")+ " " +obj.getString("FP")+ " " +obj.getString("PeroxideType")+ " " +obj.getString("PeroxideProcent")+ " " +obj.getString("FlakeA")+ " " +obj.getString("FlakeSiloA")+ " " +obj.getString("FlakeB")+ " " +obj.getString("FlakeSiloB")+ " " +obj.getString("AggloProcent")+ " " +obj.getString("AggloSilo")+ " " +obj.getString("RecycleProcent")+ " " +obj.getString("RecycleSilo")+ " " +obj.getString("MVRml10ml")+ " " +obj.getString("MVRSTD")+ " " +obj.getString("MFRg10ml")+ " " +obj.getString("AshProcent")+ " " +obj.getString("VochtProcent")+ " " +obj.getString("KleurL")+ " " +obj.getString("KleurA")+ " " +obj.getString("KleurB")+ " " +obj.getString("Operator1")+ " " +obj.getString("FlakeType1")+ " " +obj.getString("FlakeType2")+ " " +obj.getString("Productielijn1")+ " " +obj.getString("Productielijn2")+ " " +obj.getString("FlakeBatchNr1")+ " " +obj.getString("FlakeBatchNr2")+ " " +obj.getString("FlakeVochtProcent1")+ " " +obj.getString("FlakeVochtProcent2")+ " " +obj.getString("FlakeKleurL1")+ " " +obj.getString("FlakeKleurL2")+ " " +obj.getString("FlakeKleurA1")+ " " +obj.getString("FlakeKleurA2")+ " " +obj.getString("FlakeKleurB1")+ " " +obj.getString("FlakeKleurB2")+ " " +obj.getString("Remark1")+ " " +obj.getString("Remark2")+ " " +obj.getString("Remark3")+ " " +obj.getString("MeltDens")+ " " +obj.getString("Reg_sample");

   //add to list

   items.add(full);


点击项目:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
        String selectedItem = (String) parent.getItemAtPosition(position);

        //get the full item to pass

        String fullItem = items.get(postion);

        Intent appInfo = new Intent(Extrusie1Activity.this, Extrusion1infoActivity.class);

        appInfo.putExtra("data",fullItem);

        startActivity(appInfo);
    
);

Extrusion1infoActivity活动中create():

String dataToShow = getIntent().getStringExtra("data");

【讨论】:

但是,如果您查看 //stockstot[i] 部分,这并不是我的全部数据。如果我加载所有这些,那么它会为列表视图而努力。我只想在我的列表视图中查看 json 文件的某些部分,如果我点击它,那么我想查看点击项目的所有数据 谢谢你,mutch,它的工作原理,就是我喜欢它:) 1 个问题,是否也可以拆分 JSON 文件,以便我可以将这些值用于数组列表?也许在那个屏幕上也是全局的?

以上是关于仅将一部分数据显示到列表视图中并获取新活动 android studio 中的所有值?的主要内容,如果未能解决你的问题,请参考以下文章

通过 REST API 检索结果并将其显示在新活动的列表视图中(列表视图出现小问题)

如何将一个自定义列表的选定数据显示到同一活动的另一个自定义列表视图

使用 onlongClick 搜索 Listview 显示错误结果

通过意图获取数据并将其显示在列表视图中时,应用程序正在停止

如何在列表视图的每个项目上加载 json 数据并显示在 android 的另一个活动中获取的数据?

在另一个活动的列表视图的编辑文本中获取另一个活动的价值