简体   繁体   English

Android ListView从sqlite数据库获取数据

[英]Android listview fetch the data from sqlite database

I want to develop a small project in android SQlite.. 我想在android SQlite中开发一个小项目。

I want to store en employees information to the database.. the fields are name,city,mail id and phone number.. 我想将员工信息存储到数据库中。这些字段是姓名,城市,邮件ID和电话号码。

Then I want to fetch the data in the List-view ..It will show by employees name.. When I click the name(another intent) It will show all the information of that name. 然后,我想在列表视图中获取数据。它将按员工姓名显示。当我单击姓名(另一个意图)时,它将显示该姓名的所有信息。

I have create DB helper class,and I can store the information..But I am unable to retrieve the database by name..can anyone help me????? 我已经创建了数据库帮助程序类,并且可以存储信息。但是我无法按名称检索数据库。

public class DBHelper extends SQLiteOpenHelper {
static final String DATABASE = "contactapp.db";
static final int VERSION = 1;
static final String TABLE = "contact";

static final String C_ID = "_id";
static final String C_ENAME = "ename";
static final String C_city = "city";
static final String C_phno = "ph_no";


public DBHelper(Context context) {
    super(context, DATABASE, null, VERSION);

}


@Override
public void onCreate(SQLiteDatabase db) {


    db.execSQL("CREATE TABLE " + TABLE + " ( " + C_ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + C_ENAME + " text, "
            + C_city + " text, " + C_phno + " integer )");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


    db.execSQL("Drop table " + TABLE);


    onCreate(db);
}

} }

and the management.java which store the name,city and phone number in the sqlite database.. 和management.java将名称,城市和电话号码存储在sqlite数据库中。

public class Management extends Activity {
EditText tname,tcity,tph_no;
Button button1;
DBHelper helper;
SQLiteDatabase db;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.management);
    helper = new DBHelper(this);
    tname = (EditText) findViewById(R.id.tname);
    tcity = (EditText) findViewById(R.id.tcity);
    tph_no = (EditText) findViewById(R.id.tph_no);
    button1=(Button) findViewById(R.id.button1);
    button1.setOnClickListener(new OnClickListener() {


    @Override
    public void onClick(View arg0) {


        Context context = getApplicationContext();
        int s2=Integer.parseInt(tph_no.getText().toString());

            ContentValues values = new ContentValues();
            values.put(DBHelper.C_ENAME, tname.getText().toString());
            values.put(DBHelper.C_city, tcity.getText().toString());
            values.put(DBHelper.C_phno, s2);


            db = helper.getWritableDatabase();
            db.insert(DBHelper.TABLE, null, values);
            db.close();

            Toast.makeText(context, "Employee Added Successfully",
                    Toast.LENGTH_LONG).show();




    }
    });
}

} }

now I want to retrieve/fetch only the name as a list..when I click the name it will go to the another intent and will show the name(again),city and phone number.. 现在我只想检索/获取名称作为列表。.当我单击名称时,它将转到另一个意图,并显示名称(再次),城市和电话号码。

Checkout this link 签出此链接

  1. SimpleCursorAdapter In case you work directly with the database you can use the SimpleCursorAdapter to define the data for your ListView. SimpleCursorAdapter如果直接使用数据库,则可以使用SimpleCursorAdapter定义ListView的数据。

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

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