2017-01-27 7 views
1

を示すことは、ベースアダプタにアンドロイド - リストビューから別の電話番号を呼び出すことはできませんが、電話番号がリストビューにすべての異なるが表示されているものの以下は別の電話番号

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; 
} 

を拡張するリストビューアダプタでビュー方式でありますコールアイコンをクリックすると、すべてのリストアイテムの番号が同じになります。なぜですか? ありがとうございます。

答えて

1

代わり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インスタンス変数を作成すると、変数が同じ毎回ままとして新しい値で再初期化取得し続けます下/上にスクロールし、スクロールされた項目の値を正確に保持します

+0

のListViewにsetItemClickListenerあるThanks.Itはworked.Will upvoteがあり、受け入れのどちらか。 –

+0

welcome mate :) –

-1

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

第三の選択肢は