Search code examples
javaandroidlistviewadaptercustom-adapter

ListView all elements show "false" text next to correct text? Custom Adapter malfunction maybe?


I can't figure out why it says "false" on this listview. I am Guessing it is most likely because of the custom adapter, but can't find the problem.

ScreenShot:

I tried to change background color to black as a lazy escape but then "false" switched to white.

I used this tutorial, but they obviously didn't have the same problem.

Here is my code:

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;

      }

     }

UPDATE: Here is Contact class

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

}

rowbuttonlayout

<?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>


Solution

  • Your label textview's android:text property is set as android:text="@+id/label".

    And in your custom adapter, try holder.code.setText(mycode);, where mycode is a string value that you would like to display in the textview.