光标未正确加载到列表视图?

时间:2017-08-30 23:06:22

标签: android listview

我正在做简单的listview。我想加载联系人姓名,当我点击listview&#34; s项目时,我想设置Backbackcolor blue.But我有3个问题。首先我的光标没有加载到listview正确。它正在重复你自己。第二个问题是;当我点击listview中的任何项目时,我的列表视图正在绘制超过1个项目。第三个是:当我单击最后一项时,我的程序会出现异常。如何才能正确地将名称加载到listview?如何修复它?`< / p>

public class display extends AppCompatActivity {
ListView lv ;
    Button select;
    String phoneNumber;

    ArrayList <String> aa= new ArrayList<String>();





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView)findViewById(R.id.lv);
        select = (Button) findViewById(R.id.button1);
        getNumber(this.getContentResolver());}

    public void getNumber(ContentResolver contentResolver) {
        Cursor phones = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
        while (phones.moveToNext())
        {
            String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            System.out.println(".................."+phoneNumber);
            aa.add(name);
        }
        phones.close();// close cursor
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_selectable_list_item,aa);
        lv.setAdapter(adapter);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            parent.getChildAt(position).setBackgroundColor(Color.BLUE);
        }
    });
    }

}

我的布局文件:        

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:layout_above="@+id/button1" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Select" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

尝试这个。

public class display extends AppCompatActivity {
  ListView lv ;
  Button your_button; // don't use java data type "select"
  String name, phoneNumber;

  ArrayList<String> aa;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    your_button = (Button) findViewById(R.id.button1);
    lv = (ListView)findViewById(R.id.lv);
    aa= new ArrayList<String>();
    getNumber(this.getContentResolver());
  }

  public void getNumber(ContentResolver contentResolver) {
    Cursor phones = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
    if (phones.getCount() != 0) {
      if (phones.moveToFirst()) {
        do {
          name= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
          phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
          aa.add(name + "\n" + phoneNumber);
        } while (phones.moveToNext());
        phones.close();// close cursor
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_selectable_list_item, aa);
        lv.setAdapter(adapter);
      }      
    }
  }
}