我有一个有按钮的列表视图,我想让每个按钮调用不同的号码。实际上,这个列表是一个医生列表,不知怎么的,我需要为每个按钮分配一个移动电话号码。有可能吗?如果可能的话,我该怎么做?
工作的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);
    }
}发布于 2017-02-23 10:37:25
有关示例,请参见这。只要改变方法。
https://stackoverflow.com/questions/42412601
复制相似问题