【问题标题】:Using ListAdapter to show the ContactName and Number in listview using simple_list_item_2 , How to extract only Number in the onItemClick()?使用 ListAdapter 在 listview 中使用 simple_list_item_2 显示 ContactName 和 Number,如何仅提取 onItemClick() 中的 Number?
【发布时间】:2012-12-21 11:30:31
【问题描述】:

使用以下类,我想从具有联系人姓名和号码的列表中选择联系人。

public class MakeCallView extends Activity implements OnItemClickListener {

    Button callButtn;
    EditText number2call;
    private ListView lv1 = null;
    Cursor cursor = null;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.makeacall);

        cursor = getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
                null, null);
        callButtn = (Button) findViewById(R.id.CallBtn);
        number2call = (EditText) findViewById(R.id.number2Call);
        lv1 = (ListView) findViewById(R.id.listContact);

        ListAdapter adapter1 = new SimpleCursorAdapter(this,


            android.R.layout.simple_list_item_2, cursor, new String[] {
                        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                        ContactsContract.CommonDataKinds.Phone.NUMBER },
                new int[] { android.R.id.text1, android.R.id.text2 });

        lv1.setAdapter(adapter1);
        lv1.setOnItemClickListener(this);

        callButtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {


                // TODO Auto-generated method stub

            }

        });
    }



    @Override
    public void onItemClick(AdapterView<?> adapter, View arg1, int pos,
            long arg3) {

        try {

            Cursor c = (Cursor) adapter.getItemAtPosition(pos);

            View v = adapter.getFocusedChild();
            // TwoLineListItem row = (TwoLineListItem)
            // adapter.getAdapter().getView(pos, arg1,
            // (ViewGroup)adapter.getParent());
            // String a = (String)adapter.getItemAtPosition(pos);
            // number2call.setText( c.g);
            Log.v("GPS", "arg3    : " + c.getColumnCount());
            Log.v("GPS", "arg3    : " + c.getColumnNames());
            Log.v("GPS", "arg3    : " + c.toString());
            Log.v("GPS", "arg3    : " + c.getShort(1));

            Log.v("GPS", "arg3    : " + c.getColumnIndex("text2"));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Log.v("GPS", e.getMessage(), e);
        }

        // number2call.setText(((TextView)arg1).getText().toString());

        // Handle list2 click even

    }

}

从上面的代码中,在 onItemClick() 中,应该检索到我无法获取的所选联系人号码。请帮忙。

【问题讨论】:

    标签: android listview onitemclicklistener


    【解决方案1】:

    在你的 OnItemClickListener 中试试这个

    TextView txtNumber = (TextView) arg1.findViewById(android.R.id.text2)
    String str = txtNumber.getText()
    

    【讨论】:

      【解决方案2】:
      这将为您返回所选联系人的数量
       @Override
              public void onItemClick(AdapterView<?> adapter, View arg1, int pos,
                      long arg3) {
      
                  try {
      
                      String x = ((TwoLineListItem) arg1).getText2().getText()
                                  .toString();
                          number2call.setText(x);
                  } catch (Exception e) {
                      // TODO Auto-generated catch block
                      Log.v("GPS", e.getMessage(), e);
                  }
      
      
          }
      

      【讨论】:

        猜你喜欢
        • 2019-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-27
        • 2019-06-04
        • 2023-03-16
        • 1970-01-01
        • 2012-06-30
        相关资源
        最近更新 更多