代码之家  ›  专栏  ›  技术社区  ›  Hatay Berkay Işıkoğlu

光标未正确加载到listview?

  •  -1
  • Hatay Berkay Işıkoğlu  · 技术社区  · 8 年前

    我正在做简单的listview。我想加载联系人姓名,当我单击listview的项目时,我想将BackgroundColor设置为蓝色。但我有3个问题。首先,我的光标无法正确加载到listview。这是重复你自己。第二个问题是;当我单击listview中的任何项目时,我的listview正在绘制多个项目。第三个是:当我点击最后一个项目时,我的程序出现异常。如何始终正确地将名称加载到listview?我该怎么修`

    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 回复  |  直到 8 年前
        1
  •  0
  •   Android Mediocre    8 年前

    试试这个。

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