I’ve created ListView, database and a table for it. I’ve like 5 names in the list. I need to capture more than one name into table of the sqlite database but I can only capture the first name from the list. Any help would be appreciated. Thankss! =)
Below are the codes.
package main.page;
import android.app.ListActivity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class ContactsProvider extends ListActivity
{
BuddiesDBAdapter buddiesDB = new BuddiesDBAdapter(this);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.contacts_list);
Uri allContacts = Uri.parse("content://contacts/people");
Cursor c = managedQuery(allContacts, null, null, null, null);
String[] columns = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts._ID};
int[] views = new int[]{R.id.contactName, R.id.contactID};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contacts_list, c, columns, views);
this.setListAdapter(adapter);
}
public void onListItemClick(ListView parent, View v, int position, long id)
{
buddiesDB.open();
long _id;
TextView contactName = (TextView) findViewById(R.id.contactName);
String NameValue = contactName.getText().toString();
_id = buddiesDB.insertContact(NameValue);
buddiesDB.close();
}
}
package main.page;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class BuddiesDBAdapter
{
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "buddy_name";
private static final String TAG = "BuddiesDBAdapter";
private static final String DATABASE_NAME = "anniversary";
private static final String DATABASE_TABLE = "buddiesList";
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_CREATE = "create table buddiesList(_id integer primary key autoincrement, "
+ "buddy_name text not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public BuddiesDBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try
{
db.execSQL(DATABASE_CREATE);
} catch (SQLException e)
{
e.printStackTrace();
}
}// end onCreate()
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS buddiesList");
onCreate(db);
}// end onUpgrade()
}//end DatabaseHelper class
public BuddiesDBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}// end open()
public void close()
{
DBHelper.close();
}// end close()
public long insertContact(String buddy_name)
{
ContentValues values = new ContentValues();
values.put(KEY_NAME, buddy_name);
return db.insert(DATABASE_TABLE, null, values);
}//end insertContact()
public boolean deleteContact(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}// end deleteContact()
public Cursor getAllContacts()
{
return db.query(DATABASE_TABLE, new String[]
{ KEY_ROWID, KEY_NAME }, null, null, null, null, null);
}// end getAllContacts()
public Cursor getContact(long rowId) throws SQLException
{
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID, KEY_NAME }, KEY_ROWID + "=" + rowId,
null, null, null, null, null);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}// end getContact()
public boolean updateContact(long rowId, String buddy_name)
{
ContentValues args = new ContentValues();
args.put(KEY_NAME, buddy_name);
return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}//end updateContact()
}
The problem seems to be here:
TextView contactName = (TextView) findViewById(R.id.contactName);String NameValue = contactName.getText().toString();
You are using the same
TextViewfor all the views in the list when you are using theSimpleCursorAdapter. So when you sayfindViewById(R.id.contactName)you are not specifying which among the many views you want.Solution: you can try using the
positionparameter you are getting inonListItemClickto identify exactly which item in the cursor was clicked, and store that item.