Android:数据库详细信息显示问题

时间:2016-06-23 08:15:02

标签: android android-sqlite android-database

我试图使用SQLite创建一个非常简单的列表应用程序。数据库已成功创建并正常运行。它由ID,名称和电话号码等列组成。我已经在屏幕上的列表中显示了名称,并且想要在列表中单击其名称后查看该人员的其他详细信息。我将这个人的名字从一个意图传递给另一个意图。我创建了一个函数来使用此名称查看其他详细信息。但是,我无法从数据库中查看详细信息。它不显示错误,但也不显示任何细节。请帮助..

主要活动

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import java.util.ArrayList;


public class MainActivity extends AppCompatActivity {

public static ArrayList<String> Array_Names= new ArrayList<String>();
MyDBHandler dbHandler;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    dbHandler = new MyDBHandler(this, null, null, 1);
    dbHandler.addProduct(new Product("Mike", "222222222"));
    dbHandler.addProduct(new Product("Ashley", "333333333"));
    dbHandler.addProduct(new Product("Kevin", "444444444"));
    dbHandler.addProduct(new Product("Nathan", "555555555"));

    dbHandler.getAllContacts();

    ListView mylist= (ListView)findViewById(R.id.mylist);
    ArrayAdapter<String> myAdapter= new CustomAdapter(this,Array_Names);
    mylist.setAdapter(myAdapter);

    final Intent intent=new Intent(this,Display.class);
    mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String name=String.valueOf(parent.getItemAtPosition(position));
            intent.putExtra("Name",name);
            startActivity(intent);

        }
    });

}

}

产品类

public class Product {

private int _id;
private String _CName;
private String _phno;

public Product(){

}
public Product(String _CName, int _id, String _phno) {
    this._CName = _CName;
    this._id = _id;
    this._phno = _phno;
}

public Product(String _CName, String _phno) {
    this._CName = _CName;
    this._phno = _phno;
}

public String get_CName() {
    return _CName;
}

public void set_CName(String _CName) {
    this._CName = _CName;
}

public int get_id() {
    return _id;
}

public void set_id(int _id) {
    this._id = _id;
}

public String get_phno() {
    return _phno;
}

public void set_phno(String _phno) {
    this._phno = _phno;
}
}

数据库处理程序

import android.database.sqlite.*;
import android.content.*;
import android.database.Cursor;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;

public class MyDBHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1; //Change whenever db is modified.
private static final String DATABASE_NAME = "List.db";    //Db name
public static final String TABLE_NAME = "CustomerList";       //Table Name
public static final String COLUMN_ID = "Id";                  //Column Name
public static final String COLUMN_CNAME = "Name";
public static final String COLUMN_PHNO= "PhoneNumber";

public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);     //Context is always background info.
}


@Override
public void onCreate(SQLiteDatabase db) {
    String query = " CREATE TABLE " + TABLE_NAME + " ( " +

            COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT " + " , " +
            COLUMN_CNAME + " TEXT, " +
            COLUMN_PHNO + " TEXT " +
            " ); ";
    db.execSQL(query);



}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(" DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}

public void addProduct(Product product) {
    ContentValues values = new ContentValues();
    values.put(COLUMN_CNAME, product.get_CName());
    values.put(COLUMN_PHNO,product.get_phno());
    SQLiteDatabase db = getWritableDatabase();
    db.insert(TABLE_NAME, null, values);
    db.close();
}

public void removeProduct(String name) {
    SQLiteDatabase db = getWritableDatabase();
    db.execSQL(" DELETE FROM " + TABLE_NAME + " WHERE " + COLUMN_CNAME + " =\" " + name + " \"; ");
}


public List<Product> getAllContacts(){
    List<Product> contactList=new ArrayList<Product>();
    SQLiteDatabase db= this.getWritableDatabase();
    String Select_query= " SELECT * FROM " + TABLE_NAME + " ORDER BY " + COLUMN_CNAME;
    Cursor cursor= db.rawQuery(Select_query,null);

    if(cursor.moveToFirst()){
        do {
            Product contact= new Product();
            contact.set_id(Integer.parseInt(cursor.getString(0)));
            contact.set_CName(cursor.getString(1));
            contact.set_phno(cursor.getString(2));
            String name=cursor.getString(1) + "\n";
            MainActivity.Array_Names.add(name);
            contactList.add(contact);
        }while (cursor.moveToNext());
    }

    return contactList;
}
public Product getProduct(String name){
    SQLiteDatabase db=this.getWritableDatabase();
    String query= "SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_CNAME + "=\""+ name+"\"";
    Cursor cursor=db.rawQuery(query,null);
    Log.d("This is the Name",name);
    Product product= new Product();
    if(cursor.moveToFirst()){
        cursor.moveToFirst();
        product.set_id(Integer.parseInt(cursor.getString(0)));
        product.set_CName((cursor.getString(1)));
        product.set_phno(cursor.getString(2));
        cursor.close();
    }
    db.close();
    return product;


}
}

显示

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Display extends AppCompatActivity {

TextView pname;
TextView pno;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display);

    Bundle main_data=getIntent().getExtras();
    String message= main_data.getString("Name");
    pname=(TextView)findViewById(R.id.pname);
    pno=(TextView)findViewById(R.id.pno);


    pname.setText(message);
    MyDBHandler db=new MyDBHandler(this,null,null,1);
    Product product= db.getProduct(pname.getText().toString());
    pno.setText(product.get_phno());


}
}

自定义适配器

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
public class CustomAdapter extends ArrayAdapter<String> {

public CustomAdapter(Context context, ArrayList<String> names) {
    super(context, R.layout.custom_list, names);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater myinflater= LayoutInflater.from(getContext());
    View customView= convertView;
    if(customView==null){
        customView=myinflater.inflate(R.layout.custom_list,parent,false);

    }
    String singleItem=getItem(position);
    TextView mytext=(TextView) customView.findViewById(R.id.name);
    ImageView defimage=(ImageView) customView.findViewById(R.id.myimage);

    mytext.setText(singleItem);
    defimage.setImageResource(R.mipmap.contacts_default);
    return customView;


}

}

1 个答案:

答案 0 :(得分:0)

在methode getProduct()中你不能按名称搜索查询,你必须通过COLUMN_ID查询重复主键 字符串查询=“SELECT * FROM”+ TABLE_NAME +“WHERE”+ COLUMN_ID +“= \”“+ id +”\“”;