Search code examples
androidlistviewsimplecursoradapter

CustomListView with Checkboxes


I am a novice with at android programming so please bear with me. I have looked around a lot on StackOverFlow for solution for this but I couldn't understand how it works. So I tried to make one myself. All I want is to be able to view the contacts in a phone in a listView with checkboxes. This should only happen when a button is clicked. The code is pasted below

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class AddContacts extends Activity implements OnClickListener {

static final String[] FROM = { ContactsContract.Contacts._ID,
        ContactsContract.Contacts.DISPLAY_NAME };
static final int[] TO = { R.id.contact_name, R.id.phone_number };
ListView list;
Cursor cursor;
SimpleCursorAdapter adapter;

Button AddNumber;
EditText number1;
EditText number2;
EditText number3;
String contact1;
String contact2;

static final String TAG = "In AddContacts";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.secondscreen);
    AddNumber = (Button) findViewById(R.id.AddNumbers);
    number1 = (EditText) findViewById(R.id.NumberField1);
    number2 = (EditText) findViewById(R.id.NumberField2);
    number3 = (EditText) findViewById(R.id.NumberField3);
    AddNumber.setOnClickListener(this);
    list = (ListView)findViewById(R.id.list);
    Log.d(TAG, "onCreate");


}

@Override
public void onClick(View v) {

    Uri uri = ContactsContract.Contacts.CONTENT_URI;
    String[] projection = new String[] { ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME };
    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP
            + " = '1'";
    String[] selectionArgs = null;
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
            + " COLLATE LOCALIZED ASC";

    cursor = managedQuery(uri, projection, selection,
            selectionArgs, sortOrder);

    adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, FROM, TO);
    list.setAdapter(adapter);


    // Intent intents = new Intent(Intent.ACTION_PICK,
    // ContactsContract.Contacts.CONTENT_URI);
    // startActivityForResult(intents, PICK_CONTACT);
    contact1 = number1.getText().toString();
    if (!contact1.matches(regularExpression)) {
        AddNumber.setEnabled(false);
    }

    Log.d(TAG, "onClicked method");
    Intent intent = new Intent(AddContacts.this, TrackLogic.class);
    intent.putExtra("contact1", contact1);

    Log.d(TAG, "contact values are: " + contact1);
    startActivity(intent);

    }
}

This code is for the individual row in the ListView

<TextView
    android:id="@+id/contact_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="John Doe"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/phone_number"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/contact_name"
    android:text="(999)999-9999"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="CheckBox" />

This code is for the ListView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView style="@style/AppTheme" />

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</ListView>

</LinearLayout>

This code run without an compilation error but crashes with a null pointer exception.

06-22 10:57:48.177: E/AndroidRuntime(18986): FATAL EXCEPTION: main 06-22 10:57:48.177: E/AndroidRuntime(18986): java.lang.NullPointerException 06-22 10:57:48.177: E/AndroidRuntime(18986): at >com.example.s.AddContacts.onClick(AddContacts.java:68) 06-22 10:57:48.177: E/AndroidRuntime(18986): at >android.view.View.performClick(View.java:4204) 06-22 10:57:48.177: E/AndroidRuntime(18986): at >android.view.View$PerformClick.run(View.java:17355) 06-22 10:57:48.177: E/AndroidRuntime(18986): at >android.os.Handler.handleCallback(Handler.java:725) 06-22 10:57:48.177: E/AndroidRuntime(18986): at >android.os.Handler.dispatchMessage(Handler.java:92) 06-22 10:57:48.177: E/AndroidRuntime(18986): at android.os.Looper.loop(Looper.java:137) 06-22 10:57:48.177: E/AndroidRuntime(18986): at >android.app.ActivityThread.main(ActivityThread.java:5041) 06-22 10:57:48.177: E/AndroidRuntime(18986): at >java.lang.reflect.Method.invokeNative(Native Method) 06-22 10:57:48.177: E/AndroidRuntime(18986): at >java.lang.reflect.Method.invoke(Method.java:511) 06-22 10:57:48.177: E/AndroidRuntime(18986): at >com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

Can anyone tell me what am i doing wrong here? It would be great Thanks.


Solution

  • ** You are loading other xml file in setContentView() so your Listview is never initialized that's why you get the NullPointerException

    other reasons might be
    I think you are accessing android.R.id.list instead of your-package-name.R.id.list.

    Try to change your Listview ID and see if it works..

    hope this helps....