简体   繁体   中英

Android listview fetch the data from sqlite database

I want to develop a small project in android SQlite..

I want to store en employees information to the database.. the fields are name,city,mail id and phone number..

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..

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.

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