java - ListView 所有元素在正确文本旁边显示 "false"文本?自定义适配器可能出现故障?

标签 java android listview adapter custom-adapter

我不明白为什么它在这个 ListView 上显示“false”。我猜这很可能是因为自定义适配器,但找不到问题。

截图:

我尝试将背景颜色更改为黑色作为懒惰的逃避,但随后“false”切换为白色。

我用了this tutorial , 但他们显然没有同样的问题。

这是我的代码:

public class ContactsList extends Activity{

String eventCode,message;
MyCustomAdapter dataAdapter = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contacts_list);
    setTitle("Invite Some People");
    Intent intent = getIntent();
    eventCode=intent.getStringExtra("eventcode");

    ArrayList<Contact> ctcs=getContacts();
    //create an ArrayAdaptar from the String Array
    dataAdapter = new MyCustomAdapter(this,
        R.layout.rowbuttonlayout, ctcs);
    ListView listView = (ListView) findViewById(R.id.listView1);
    // Assign adapter to ListView
    listView.setAdapter(dataAdapter);
}

public void checkButtonClick(View v) {
    //SmsManager smsManager = SmsManager.getDefault();
    //smsManager.sendTextMessage(phoneNo, null, message, null, null);

    Button myButton = (Button) findViewById(R.id.findSelected);
    myButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        StringBuffer responseText = new StringBuffer();
        responseText.append("The following were selected...\n");

        ArrayList<Contact> contactList = dataAdapter.contactList;
        for(int i=0;i<contactList.size();i++){
         Contact contact = contactList.get(i);
         if(contact.isChecked()){
          responseText.append("\n" + contact.getName());
         }
        }

        Toast.makeText(getApplicationContext(),
          responseText, Toast.LENGTH_LONG).show();

       }
      });

     }
private ArrayList<Contact> getContacts() {
    ArrayList<Contact> contacts = new ArrayList<Contact>();     

    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
    while (phones.moveToNext())
    {try{
        String contctName=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String contctNumb=getPhoneNumber(contctName,getApplicationContext());
        contacts.add(new Contact(contctName,contctNumb));
    }catch(SQLiteException e){
        }
    }
    phones.close();
    contacts.add(new Contact("Dummy",""));
    contacts.add(new Contact("Dummy2",""));
    return contacts;
  }

public void goHome(View v){
    startActivity(new Intent(this, HomeScreen.class));
}
public String getPhoneNumber(String name, Context context) {
    String ret = null;
    String selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" like'%" + name +"%'";
    String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER};
    Cursor c = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            projection, selection, null, null);
    if (c.moveToFirst()) {
        ret = c.getString(0);
    }
    c.close();
    if(ret==null)
        ret = "Unsaved";
    return ret;
    }
private class MyCustomAdapter extends ArrayAdapter<Contact> {

      private ArrayList<Contact> contactList;

      public MyCustomAdapter(Context context, int textViewResourceId, 
        ArrayList<Contact> contactList) {
       super(context, textViewResourceId, contactList);
       this.contactList = new ArrayList<Contact>();
       this.contactList.addAll(contactList);
      }

      private class ViewHolder {
       TextView code;
       CheckBox name;
      }

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {

       ViewHolder holder = null;
       Log.v("ConvertView", String.valueOf(position));

       if (convertView == null) {
       LayoutInflater vi = (LayoutInflater)getSystemService(
         Context.LAYOUT_INFLATER_SERVICE);
       convertView = vi.inflate(R.layout.rowbuttonlayout, null);

       holder = new ViewHolder();
       holder.code = (TextView) convertView.findViewById(R.id.label);
       holder.name = (CheckBox) convertView.findViewById(R.id.check);
       convertView.setTag(holder);

        holder.name.setOnClickListener( new View.OnClickListener() {  
         public void onClick(View v) {  
          CheckBox cb = (CheckBox) v ;  
          Contact contact = (Contact) cb.getTag();  
          Toast.makeText(getApplicationContext(),
           "Clicked on Checkbox: " + cb.getText() +
           " is " + cb.isChecked(), 
           Toast.LENGTH_LONG).show();
          contact.setChecked(cb.isChecked());
         }  
        });  
       } 
       else {
        holder = (ViewHolder) convertView.getTag();
       }

       Contact contact = contactList.get(position);
       holder.name.setText(contact.getName());
       holder.name.setChecked(contact.isChecked());
       holder.name.setTag(contact);

       return convertView;

      }

     }

更新:这是联系人类

public class Contact {
String name;
String num;
boolean selected;

public Contact(String name, String numb) {
    this.name = name;
    this.num = numb;
    selected = false;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getNum() {
    return num;
}

public void setNum(String numb) {
    this.num = numb;
}

public boolean isChecked() {
    return this.selected;
}

public void setChecked(boolean selected) {
    this.selected = selected;
}

行按钮布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@+id/label"
    android:textSize="19sp" >
</TextView>

<CheckBox
    android:id="@+id/check"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="10dp" >
</CheckBox>

最佳答案

您的 label textview 的 android:text 属性设置为 android:text="@+id/label"

然后在您的自定义适配器中,尝试 holder.code.setText(mycode);,其中 mycode 是您希望在 TextView 中显示的字符串值。

关于java - ListView 所有元素在正确文本旁边显示 "false"文本?自定义适配器可能出现故障?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23836145/

相关文章:

java - 需要有关如何使用数组元素对数组进行排序的帮助

android - 在这种情况下使用 Proguard 时签名有什么问题?

JavaFX ListView SetCellFactory 在 imageView 和 Text 之间添加边距

android - 尝试将 Gradle 项目导入 Android Studio 现有项目时出错

Java android 相对布局在滑动监听器上设置不适用于内部的 listView

android - 更新特定视频的媒体存储内容值

java - 从其他 Activity 调用函数时出现 ClassCastException

java - coSTLy(Time)如何在java中对csv文件进行读写操作?

java - 完美数表现

android - ListView 中的 Spinner 问题