java—如何在警报对话框中按顺序显示选定的listview项?

mklgxw1f  于 2021-07-05  发布在  Java
关注(0)|答案(0)|浏览(155)

这是我的代码,我不知道该怎么写来显示单个选中的项目。如何在所选listview视图项中显示用户id、名称和其他详细信息以提醒对话框。盒子。主要问题是所选项目的位置。
列表项
private string tag=mainactivity.class.getsimplename();

private ProgressDialog pDialog;
private ListView lv;

ArrayList<HashMap<String, String>> contactList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    contactList = new ArrayList<>();

    lv = findViewById(R.id.list);

    new GetContacts().execute();
}

/**
 * Async task class to get json by making HTTP call
 */
@SuppressLint("StaticFieldLeak")
private class GetContacts extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        HttpHandler sh = new HttpHandler();

        // Making a request to url and getting response
        // URL to get contacts JSON
        String url = "https://api.androidhive.info/contacts/";
        String jsonStr = sh.makeServiceCall(url);

        Log.e(TAG, "Response from url: " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                JSONArray contacts = jsonObj.getJSONArray("contacts");

                // looping through All Contacts
                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);

                    String id = c.getString("id");
                    String name = c.getString("name");
                    String email = c.getString("email");
                    String address = c.getString("address");
                    String gender = c.getString("gender");

                    // Phone node is JSON Object
                    JSONObject phone = c.getJSONObject("phone");
                    String mobile = phone.getString("mobile");
                    String home = phone.getString("home");
                    String office = phone.getString("office");

                    // tmp hash map for single contact
                    HashMap<String, String> contact = new HashMap<>();

                    // adding each child node to HashMap key => value
                    contact.put("\n"+"id", id);
                    contact.put("name", name);
                    contact.put(""+  "email", email);
                    contact.put(""+"mobile", mobile);
                    contact.put("address",address);
                    contact.put("\n"+"gender",gender);
                    contact.put("\n"+  "home",home);
                    contact.put("\n"+"office",office);

                    // adding contact to contact list
                    contactList.add(contact);
                }
            } catch (final JSONException e) {
                Log.e(TAG, "Json parsing error: " + e.getMessage());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Json parsing error: " + e.getMessage(),
                                Toast.LENGTH_LONG)
                                .show();
                    }
                });

            }
        } else {
            Log.e(TAG, "Couldn't get json from server.");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "Couldn't get json from server. Check LogCat for possible errors!",
                            Toast.LENGTH_LONG)
                            .show();
                }
            });

        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();
        final ListAdapter adapter = new SimpleAdapter(
                MainActivity.this, contactList,
                R.layout.list_item, new String[]{"name", "email",
                "mobile", "address", "gender"}, new int[]{R.id.name,
                R.id.email, R.id.mobile, R.id.address});

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

                AlertDialog.Builder alert =  new AlertDialog.Builder(MainActivity.this);
                View mView = getLayoutInflater().inflate(R.layout.list_item,null);
                final ListAdapter adapter = new SimpleAdapter(
                        MainActivity.this,contactList,

                        R.layout.list_item, new String[]{"name", "email",
                        "mobile", "address", "gender"}, new int[]{R.id.name,
                        R.id.email, R.id.mobile, R.id.address});

                contactList.get(position);
                alert.setAdapter(adapter, null);

                alert.setView(mView);
                final androidx.appcompat.app.AlertDialog alertDialog = alert.create();
                alertDialog.setCanceledOnTouchOutside(false);
                btn_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        alertDialog.dismiss();

                    }
                });
                ok.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        textView.setText(text.getText().toString());

                    }
                });
                alertDialog.show();

            }
        });
    }

}

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题