簡體   English   中英

Android - 無法從列表視圖撥打不同的電話號碼,但顯示不同的電話號碼

[英]Android - cannot call different phone number from list view but showing different phone number

下面是列表視圖適配器擴展基本適配器中的視圖方法

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null) {
        vi = inflater.inflate(R.layout.contact_people_list_item, null);
        callPeopleContact = (ImageButton) vi.findViewById(R.id.people_contact_call_icon);
        try {
            jsonTotalObject = this_dataJsonArray.getJSONObject(position);
            jsonUserObject = jsonTotalObject.getJSONObject("User");
            telephone = jsonUserObject.getString("office_phone");
            intentToCall = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + telephone));
            callPeopleContact.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (ActivityCompat.checkSelfPermission(this_context, android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                            // TODO: Consider calling
                            //    ActivityCompat#requestPermissions
                            // here to request the missing permissions, and then overriding
                            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                            //                                          int[] grantResults)
                            // to handle the case where the user grants the permission. See the documentation
                            // for ActivityCompat#requestPermissions for more details.
                            return;
                        }
                        this_context.startActivity(intentToCall);
}
            });

            });
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return vi;
    }
    return vi;
}

盡管電話號碼在列表視圖中顯示的完全不同,但當我單擊呼叫圖標時,所有列表項的號碼都是相同的。為什么會這樣? 感謝您的時間。

快速解決

不是創建intentToCall類級別變量,而是使用final修飾符在本地定義它,它會完成工作。

改變

intentToCall = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + telephone));

final Intent intentToCall = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + telephone));

詳細說明:

當您創建intent實例變量時,變量每次都將保持不變,並在您向下/向上滾動時繼續使用新值重新初始化,並將精確保存滾動項目的值

ListView 有一個缺點,當您在 getView 中使用 onClickListener 時,它會給您錯誤的項目位置。 您使用 RecyclerView 或以下代碼

    class DetailAdapter extends BaseAdapter {
        Context ctx;
        int countt = 1;
        int j;
        private int mSelectedPosition = -1;
        DetailAdapter(Context c) {
            ctx = c;
            layinfa = LayoutInflater.from(ctx);
        }

        public int getSelectedPosition() { return mSelectedPosition; }
        // getter and setter methods for the field above
        public void setSelectedPosition(int selectedPosition) {
            mSelectedPosition = selectedPosition;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            Log.i("*******", new StringBuilder().append(list_vehidetai.size()).toString());
            return list_vehidetai.size();
        }

        @Override
        public Object getItem(int i) {
            return list_vehidetai.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

       public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null) {
        vi = inflater.inflate(R.layout.contact_people_list_item, null);
        callPeopleContact = (ImageButton) vi.findViewById(R.id.people_contact_call_icon);
        try {
            jsonTotalObject = this_dataJsonArray.getJSONObject(position);
            jsonUserObject = jsonTotalObject.getJSONObject("User");
            telephone = jsonUserObject.getString("office_phone");
            intentToCall = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + telephone));
            callPeopleContact.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
 if (mSelectedPosition == position) {


                    if (ActivityCompat.checkSelfPermission(this_context, android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                            // TODO: Consider calling
                            //    ActivityCompat#requestPermissions
                            // here to request the missing permissions, and then overriding
                            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                            //                                          int[] grantResults)
                            // to handle the case where the user grants the permission. See the documentation
                            // for ActivityCompat#requestPermissions for more details.
                            return;
                        }
                        this_context.startActivity(intentToCall);
}
}
            });

            });
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return vi;
    }
}

獲取所選 itemposition 的 getter setter

第三個選項是 listView 上的 setItemClickListener

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM