【问题标题】:ListView button with click call带有点击调用的 ListView 按钮
【发布时间】:2017-02-23 10:07:59
【问题描述】:

我有一个带有按钮的列表视图,我想让每个按钮呼叫不同的号码。实际上该列表是医生列表,不知何故我需要为每个按钮分配一个手机号码。是否可以?如果可能,我该怎么做?

工作的 DoctorsActivity.java 文件如下。

    public class DoctorsActivity  extends AppCompatActivity {

    private JSONArray arrayAdapter;
    private static final String URL_FOR_BALANCE = "http://192.168.1.28/api2/doctors.php";

    String cancel_req_tag = "login";

    private ListView lv;
    ArrayList<HashMap<String, String>> contactList;
    Bitmap icon = null;

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

        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setCustomView(R.layout.toolbar_doctors);

        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#003764")));
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);

        SharedPreferences sharedPreferences = getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
        final String pid = sharedPreferences.getString(Config.UID_SHARED_PREF, null);

        contactList = new ArrayList<>();
        lv = (ListView) findViewById(R.id.list);

        StringRequest strReq = new StringRequest(Request.Method.POST,
                URL_FOR_BALANCE, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {

                try {
                    JSONObject jObj = new JSONObject(response);
                    boolean error = jObj.getBoolean("error");

                    if (!error) {

                        JSONArray contacts = jObj.getJSONArray("user");


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

                            String doctorTitle = c.getString("title");
                            String doctorName = c.getString("first_name");
                            String doctorSurname = c.getString("last_name");
                            String doctorPhoto = c.getString("photo"); //image URL
                            String doctorMobile = c.getString("mobile");

                            String doctorFullName = doctorTitle+" "+doctorName+" "+doctorSurname;

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

                            // adding each child node to HashMap key => value
                            contact.put("photo", doctorPhoto);
                            contact.put("doctor", doctorFullName);
                            contact.put("mobile", doctorMobile);

                            // adding contact to contact list
                            contactList.add(contact);
                        }

                        ListAdapter adapter = new SimpleAdapter(
                                DoctorsActivity.this, contactList,
                                R.layout.activity_doctors_list_item, new String[]{"photo", "doctor",
                                "mobile"}, new int[]{R.id.photo,
                                R.id.doctor, R.id.mobile});

                        lv.setAdapter(adapter);



                    } else {

                        String errorMsg = jObj.getString("error_msg");
                        Toast.makeText(getApplicationContext(),
                                errorMsg, Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_LONG).show();
            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                // Posting params to login url
                Map<String, String> params = new HashMap<String, String>();
                params.put("uid", pid);
                params.put("lang", Locale.getDefault().getDisplayLanguage());
                return params;
            }
        };
        // Adding request to request queue
        AppSingleton.getInstance(getApplicationContext()).addToRequestQueue(strReq,cancel_req_tag);
    }
}

【问题讨论】:

    标签: android listview android-button


    【解决方案1】:

    您可以参考here 只需创建自定义适配器

    【讨论】:

      【解决方案2】:

      您可以通过创建自定义适配器来实现此功能

      【讨论】:

        【解决方案3】:

        有关示例,请参阅 this。只是改变方法。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-02-09
          • 1970-01-01
          • 2017-02-12
          相关资源
          最近更新 更多