【问题标题】:Get detailed contents of an item clicked in a listview in next activity在下一个活动中获取在列表视图中单击的项目的详细内容
【发布时间】:2016-05-02 02:38:25
【问题描述】:

您好,我正在编写一个代码,它在列表视图中显示一些详细信息,例如名称和价格,这些信息来自 MySQL 数据库。现在,当我想单击该列表视图中的特定项目时,我想要的是我想移动到布局相同的下一个活动,只有单击项目时数据会不断变化。

就像当我点击名字 A 时,我想打开一个新的活动,其中的名字以及它的地址密码,电话号码将详细显示。

我已经在列表视图中获取了所有详细信息,并在其中显示了一些我想要的内容,然后将下一个传递给详细的活动。 但我只得到第二个活动的 toast 和 settext 的键,即“NameAddressPincodePhone_Number”,而不是与它们关联的值。 请看我写的代码。请帮忙

带有列表视图的活动

public class Plumbers extends Activity {
    String myJSON;

    public static final String TAG_RESULTS="result";
    public static final String TAG_NAME = "Name";
    public static final String TAG_ADDRESS = "Address";
    public static final String TAG_PINCODE = "Pincode";
    public static final String TAG_PHONENUMBER = "Phone_Number";
    public static final String TAG_DOB = "DOB";
    public static final String TAG_AADHARNUMBER = "Aadhar_Number";
    public static final String TAG_Price = "price";
    public static final String TAG_Spl = "spl";

    JSONArray peoples = null;
    ArrayList<HashMap<String, String>> personList;

    ListView list;
    EditText search;
    Spinner spin;

    protected Object adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
        list = (ListView) findViewById(R.id.list1);
        spin=(Spinner)findViewById(R.id.spinner1);
        search = (EditText)findViewById(R.id.search);
        personList = new ArrayList<HashMap<String,String>>();
        getData();
    }

    protected void showList(){
        try {
            JSONObject jsonObj = new JSONObject(myJSON);
            peoples = jsonObj.getJSONArray(TAG_RESULTS);

            for(int i=0;i<peoples.length();i++){
                JSONObject c = peoples.getJSONObject(i);

                String Name = c.getString(TAG_NAME);
                String price = c.getString(TAG_Price);
                String spl = c.getString(TAG_Spl);

                HashMap<String,String> plumbers = new HashMap<String,String>();
                plumbers.put(TAG_NAME,Name);
                plumbers.put(TAG_Price,price);
                plumbers.put(TAG_Spl,spl);

                personList.add(plumbers);
            }

            final ListAdapter adapter = new SimpleAdapter(
                    Plumbers.this, personList, R.layout.plumber,
                    new String[]{TAG_NAME ,TAG_Spl,TAG_Price },
                    new int[]{R.id.Name , R.id.spl ,R.id.price}
            );

            list.setAdapter(adapter);
            list.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent,  View view,
                        int position, long id) {
                    adapter.getItem(position);

                    Intent intent = new Intent(Plumbers.this, Plumber1.class);
                    intent.putExtra("Name", TAG_NAME);
                    intent.putExtra("Address", TAG_ADDRESS);
                    intent.putExtra("Pincode", TAG_PINCODE);
                    intent.putExtra("Phone_Number", TAG_PHONENUMBER);

                    startActivity(intent);
                }
            });

            spin.setAdapter((SpinnerAdapter) adapter);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void getData(){
        class GetDataJSON extends AsyncTask<String, Void, String> {
            @Override
            protected String doInBackground(String... params) {
                DefaultHttpClient httpclient = new DefaultHttpClient(new   BasicHttpParams());
                HttpPost httppost = new HttpPost("http://192.168.2.7/homerun/plumbers.php");

                // Depends on your web service
                httppost.setHeader("Content-type", "application/json");

                InputStream inputStream = null;
                String result = null;
                try {
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();

                    inputStream = entity.getContent();
                    // json is UTF-8 by default
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                    StringBuilder sb = new StringBuilder();

                    String line = null;
                    while ((line = reader.readLine()) != null)
                    {
                        sb.append(line + "\n");
                    }
                    result = sb.toString();
                } catch (Exception e) {
                    // Oops
                }
                finally {
                    try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
                }
                return result;
            }

            @Override
            protected void onPostExecute(String result){
                myJSON=result;
                showList();
            }
        }

        GetDataJSON g = new GetDataJSON();
        g.execute();
    }
}

活动的下一个项目的详细信息

public class Plumber1 extends Activity { 
    ImageView img,call;
    TextView pl1,nm,ad,phn,qu,pin;
    String  Name,Address,Pincode,Phone_Number;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.plumber1);
        img=(ImageView)findViewById(R.id.imag);
        call=(ImageView)findViewById(R.id.call);
        pl1=(TextView)findViewById(R.id.pl1);
        nm=(TextView)findViewById(R.id.name);
        ad=(TextView)findViewById(R.id.Address);
        pin=(TextView)findViewById(R.id.Pincode);
        phn=(TextView)findViewById(R.id.phn);
        qu=(TextView)findViewById(R.id.quotation);

        Intent intent = getIntent();
        if (intent.getExtras() != null) {

        Name = intent.getStringExtra("Name");
         Address = intent.getStringExtra("Address");
         Pincode = intent.getStringExtra("Pincode");
         Phone_Number = intent.getStringExtra("Phone_Number");
           Toast.makeText(getApplicationContext(),  Name + "" + Address + "" + Pincode + "" + Phone_Number +"" ,  Toast.LENGTH_LONG).show();
    }

        nm.setText(Name+"");
    ad.setText(Address+"");
    pin.setText(Pincode+"");
    phn.setText(Phone_Number+"");
        call.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Intent in = new Intent(android.content.Intent.ACTION_DIAL, Uri
                        .parse("tel:+918058706096"));
                startActivity(in);
            }
        });
    }
}

php文件

<?php
    mysql_connect("localhost","root","");  
    mysql_select_db("homerun");  
    $sql="select * from plumbers WHERE Status = '1'  GROUP BY price  ORDER BY  name ASC" ;  
    $res = mysql_query($sql);
    $result = array();
    while($row = mysql_fetch_array($res)){
        array_push($result,
        array('Name'=>$row[1],'spl'=>$row[8],'price'=>$row[7]));
    }

    echo json_encode(array("result"=>$result));

    mysql_close();
?>

【问题讨论】:

  • 看起来你至少会得到你的 TAG_DATA 值......也在你的 onItemClickListener 中你调用了 adapter.getItem(position) 但从不对其数据做任何事情。
  • 不,我只得到了 toast 和 settext 中的键,我得到了这个“NameAddressPincodePhone_Number”,它只得到键而不是值。请帮忙

标签: android listview android-intent listviewitem


【解决方案1】:

这是错误的

intent.putExtra("Name", TAG_NAME);

putExtra 有两个参数,第一个是 key,第二个是 value。在您的情况下,第一个是正确的,但您再次发送 value 参数一个TAG_NAME(这是错误的)

使用下面的代码

intent.putExtra("Name",personList.get(position).get(TAG_NAME));
intent.putExtra("Address",personList.get(position).get(TAG_ADDRESS));
intent.putExtra("Pincode",personList.get(position).get(TAG_PINCODE));


intent.putExtra("Phone_Number",personList.get(position).get(TAG_PHONENUMBER));

startActivity(intent);

只需复制上面的代码

还将您的 for 循环更新为以下代码

 for(int i=0;i<peoples.length();i++){
                JSONObject c = peoples.getJSONObject(i);

                String Name = c.getString(TAG_NAME);
                String price = c.getString(TAG_Price);
                String spl = c.getString(TAG_Spl);
                String Address = c.getString(TAG_ADDRESS);
                String Pincode = c.getString(TAG_PINCODE);
                String Phone_Number = c.getString(TAG_PHONENUMBER);

                HashMap<String,String> plumbers = new HashMap<String,String>();
                plumbers.put(TAG_NAME,Name);
                plumbers.put(TAG_Price,price);
                plumbers.put(TAG_Spl,spl);
                plumbers.put(TAG_ADDRESS,Address);
                plumbers.put(TAG_PINCODE,Pincode);
                plumbers.put(TAG_PHONENUMBER,Phone_Number);

                personList.add(plumbers);
            }

【讨论】:

  • 感谢您的回复,我按照您的要求做了,但现在在第二个活动中,我只得到名称值,其他值为 null。是因为在 listview 的第一个活动中我只显示了名称,如果是,那么请帮助我,因为首先我只想显示名称而不是地址 pincode 等想在下一个活动中显示它们。 intent.putExtra("姓名",personList.get(position).get(TAG_NAME)); intent.putExtra("地址",personList.get(position).get(TAG_ADDRESS)); intent.putExtra("Pincode",personList.get(position).get(TAG_PINCODE));请帮忙..谢谢
  • 先生,我没听懂您所说的“复制并低于代码”。先生,您能详细说明一下吗
  • 告诉我你想在 Listview 中显示什么以及在其他活动中显示什么。
  • 我想在我从 json 正确获取的列表视图中显示名称、专业化 'spl' 和价格。现在,当我单击该列表视图中的任何项目或该列表视图中的任何名称时,我想继续进行下一个活动,其中必须显示在该列表视图中单击的特定名称的名称、地址、密码和电话号码。谢谢先生的回复。请大佬帮忙
  • 实施我的更新答案时遇到的错误是什么?
猜你喜欢
  • 1970-01-01
  • 2016-09-12
  • 2017-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多