简体   繁体   English

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

[英]why can't I capture more than one name and insert into table of sqlite database?

I've created ListView, database and a table for it. 我已经创建了ListView,数据库和一个表。 I've like 5 names in the list. 我喜欢列表中的5个名字。 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. 我需要在sqlite数据库的表中捕获多个名称,但是我只能从列表中捕获名字。 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()
}

U can try my code: 你可以尝试我的代码:

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

^-^ ^-^

The problem seems to be here: 问题似乎在这里:

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

You are using the same TextView for all the views in the list when you are using the SimpleCursorAdapter . 使用SimpleCursorAdapter时,列表中的所有视图都使用相同的TextView So when you say findViewById(R.id.contactName) you are not specifying which among the many views you want. 因此,当您说findViewById(R.id.contactName)您并没有指定要在多个视图中选择哪个。

Solution: you can try using the position parameter you are getting in onListItemClick to identify exactly which item in the cursor was clicked, and store that item. 解决方案:您可以尝试使用在onListItemClick中获得的position参数来准确确定光标中的哪个项目被单击,并存储该项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM