简体   繁体   中英

SQL and Android. Having trouble getting the hang of it

I'm having trouble retrieving specific data from the database. I'm only putting in two values into the database: a name and a phone number.

So I am basically trying to make a string of the phone number of the item in a list that they click on. Here is the onItemClickListener (that is in an activity that just has a listview on it that only has the name from the database): (I bolded the important parts)

 mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            String name = adapterView.getItemAtPosition(position).toString();
            **String phone = String.valueOf(mDatabaseHelper.getPhone(name));**
            Log.d(TAG, "onItemClick: You clicked on: " + name);
            Log.d(TAG, "onItemClick: String phone is: " + phone);

            //get the id associated with that name
            Cursor data = mDatabaseHelper.getItemID(name, phone);
            int itemID = -1;
            while(data.moveToNext()){
                itemID = data.getInt(0);

            }
            if (itemID == -1){
                toastMessage("No ID associated with that name");
            }else{
                Log.d(TAG, "onItemClick: The ID is: " + itemID);
                Intent intentEditScreen = new Intent(ListDataActivity.this, DetailsActivity.class);
                intentEditScreen.putExtra("ID", itemID);
                intentEditScreen.putExtra("NAME", name);
                **intentEditScreen.putExtra("PHONE" , phone);**
                startActivity(intentEditScreen);
            }
        }
    });

And here is my DatabaseHelper class. I'm just going to show the getPhone method:

public Cursor getPhone(String name){
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "SELECT " + COL_PHONE + " FROM " + TABLE_NAME + " WHERE " + COL_NAME + " = '" + name + "'";
    Cursor data = db.rawQuery(query, null);
    Log.d(TAG, "getPhone query:" + query);
    Log.d(TAG, "getPhone: " + data);
    return data;
}

I just want to get the phone number that was typed in into an extra so that I can display it as a textview in another activity.

Can I get some hints as to how to do this? I've tried reading a number of guides and watching videos but I just can't get a hold of this specific part.

If anymore information would be helpful just ask and I'll be glad to post more info.

public String getPhone(String name) 
{
    SQLiteDatabase db = this.getWritableDatabase();
    String get_PHONE = "";
    String query = "SELECT " + COL_PHONE + " FROM " + TABLE_NAME + " WHERE " + COL_NAME + " = '" + name + "'";
    Cursor c = db.rawQuery(query, null);

    if (c != null && c.moveToFirst()) 
    {
    get_PHONE = c.getString(0);
    }

    c.close();
    db.close();
    return get_PHONE;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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