【问题标题】:How to show all data according to particular item clicked in ListView to new Intent如何根据在 ListView 中单击的特定项目将所有数据显示到新 Intent
【发布时间】:2016-09-07 17:57:37
【问题描述】:

我想显示,数据从 mysql 读取到 setOnItemClickListener。就像whatsapp一样,我希望从数据库中读取的数据只在一行中显示(...),在每一行结束后,点击任何listview项目后,所有数据都将以具有textview的新意图打印。

我的 java 代码从 listview 获取数据以显示数据。

private class JsonReadTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(params[0]);
            try {
                HttpResponse response = httpclient.execute(httppost);
                jsonResult = inputStreamToString(
                        response.getEntity().getContent()).toString();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        private StringBuilder inputStreamToString(InputStream is) {
            String rLine = "";
            StringBuilder answer = new StringBuilder();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));

            try {
                while ((rLine = rd.readLine()) != null) {
                    answer.append(rLine);
                }
            } catch (IOException e) {
                // e.printStackTrace();
                Toast.makeText(getApplicationContext(),
                        "Error..." + e.toString(), Toast.LENGTH_LONG).show();
            }
            return answer;
        }

        @Override
        protected void onPostExecute(String result) {
            try {
                ListDrwaer();
                pdialog.dismiss();
            } catch (Exception e) {
                pdialog.dismiss();
                Toast.makeText(getApplicationContext(), "Error: Unreachable Database", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        protected void onPreExecute() {
            pdialog = ProgressDialog.show(Home.this, "Loading Content", "Please Wait...", true);
            super.onPreExecute();
        }
    }

    public void accessWebService() {
        JsonReadTask task = new JsonReadTask();
        // passes values for the urls string array
        task.execute(new String[]{url});
    }

    public void ListDrwaer() {
        dataList = new ArrayList<>();
        String str = "", str2 = "";
        try {
            JSONObject jsonResponse = new JSONObject(jsonResult);
            JSONArray jsonMainNode = jsonResponse.optJSONArray("works");
            for (int i = 0; i < jsonMainNode.length(); i++) {
                JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                String name = (jsonChildNode.getString("name"));
                String username = jsonChildNode.getString("username");
                String password = jsonChildNode.getString("password");
                String details = jsonChildNode.getString("details");
                String outPut = "\n\nName: " + name + "\n\n" + "Username: " + username + "\n\n" + "Password: " + password + "\n\n" + "Details: " + details + "\n\n";
                dataList.add(createList("details", outPut));
            }
            Collections.reverse(dataList);


        } catch (JSONException e) {
            Toast.makeText(getApplicationContext(), "Error" + e.toString(),
                    Toast.LENGTH_SHORT).show();
        }


        simpleAdapter = new SimpleAdapter(this, dataList,
                android.R.layout.simple_list_item_1,
                new String[]{"details"}, new int[]{android.R.id.text1});

        listView.setAdapter(simpleAdapter);

    }

    private HashMap<String, String> createList(String key, String value) {
        HashMap<String, String> data = new HashMap<>();
        data.put(key, value);
        return data;
    }

这是layout 文件,我在其中显示listview 中的所有数据。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.mr_robot.app_proj.Home"
    tools:showIn="@layout/activity_home">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView2"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="62dp" />
</RelativeLayout>

这是新意图的布局文件,我想在点击特定项目后显示所有数据。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.mr_robot.temp_proj.Show"
    android:background="#ffffff">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="false">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/list_tag"
            android:layout_alignParentTop="true"
            android:textColor="#000000"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="44dp" />
    </ScrollView>
</RelativeLayout>

【问题讨论】:

    标签: android mysql database listview android-intent


    【解决方案1】:

    您可以使用 2 个数据列表,一个包含真实数据,一个包含文本的某些部分(使用子字符串和添加 (...))。你可以使用第二个来显示,但是当它被点击时,你会从真实列表中发送数据

    ListView lv = (ListView) findViewById(R.id.listview2);
    lv.setOnItemClickListener(new OnItemClickListener(){
      @Override
      public void onItemClick(AdapterView<?> adapter, View v, int position,
            long arg3) 
      {
            String value = (String)adapter.getItemAtPosition(position); 
            // assuming string and if you want to get the value on click of list item
            // your code using intent
            Intent intent = new Intent(FirstActivity.this, ShownActivity.class);
            // put your real data here
            intent.putExtra("keydata", datalist2[position]);
            startActivity(intent);
      }
    });
    

    然后你可以使用 String data = getIntent.getExtra("keydata");在下一个活动中显示您的数据

    【讨论】:

    • 好的,我明白你说的。我是菜鸟,对我来说问题是,我是怎么做的?能给我看看么?当点击特定项目时,我不知道如何在意图中显示准确的数据。
    • 我使用了 'List> dataList;' 及其错误 "Array type expected"
    猜你喜欢
    • 2023-03-10
    • 1970-01-01
    • 2013-01-21
    • 2019-08-02
    • 1970-01-01
    • 2016-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多