为什么我不能捕获多个名称并插入到sqlite数据库的表中?

时间:2012-07-30 03:00:59

标签: android listview android-sqlite

我为它创建了ListView,数据库和表。我在列表中喜欢5个名字。我需要在sqlite数据库的表中捕获多个名称,但我只能从列表中捕获名字。任何帮助,将不胜感激。 Thankss! =)

以下是代码。

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

2 个答案:

答案 0 :(得分:0)

你可以尝试我的代码:

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

Cursor c = managedQuery(uri, projection, selection, selectionArgs, sortOrder); 
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contacts_list, c, columns, views);
        this.setListAdapter(adapter);

^ - ^

答案 1 :(得分:0)

问题似乎在这里:

TextView contactName = (TextView) findViewById(R.id.contactName); String NameValue = contactName.getText().toString();

当您使用TextView时,您对列表中的所有视图使用相同的SimpleCursorAdapter。因此,当您说findViewById(R.id.contactName)时,您没有指定您想要的许多视图中的哪一个。

解决方案:您可以尝试使用position中的onListItemClick参数来准确识别光标中的哪个项目,然后存储该项目。