mardi 29 septembre 2015

How To Display SQlite datas in Listview

This is My Custom Layout and i want to display the datas like this in a ListviewHere i am created a database students.db and i will do a CURD Operation i will done Insert,Update and delete but i dont know how to display the inserted data in Listview plz help with my custom layout

public class DatabaseHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME="students.db";
private static final String TABLE_NAME="student_details";
private static final String COL_1="ID";
private static final String COL_2="NAME";
private static final String COL_3="SURNAME";
private static final String COL_4="MARKS";
private static final int DATABASE_VERSION=1;

public DatabaseHelper (Context context)
{
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

}



@Override
public void onCreate(SQLiteDatabase db) {


db.execSQL("create table " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,SURNAME TEXT,MARKS INTEGER)");

    Log.d("info", "TABLE WAS CREATED");


}

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

    db.execSQL("DROP TABLE IF EXIST " + TABLE_NAME);
    onCreate(db);
    Log.d("Info","TABLE WAS UPDATED");

}

public boolean insertdata(String name,String surname,String marks)
{

    SQLiteDatabase db= this.getWritableDatabase();

    ContentValues contentValues= new ContentValues();
    contentValues.put(COL_2,name);
    contentValues.put(COL_3, surname);
    contentValues.put(COL_4, marks);

    long result=db.insert(TABLE_NAME,null,contentValues);
    if (result==-1)
        return false;
    else
        return true;

}
public Cursor getallData()
{
    SQLiteDatabase db= this.getWritableDatabase();

    Cursor res=db.rawQuery("SELECT * FROM "+TABLE_NAME,null);
    return res;

}
public boolean updateData(String id,String name,String surname,String marks) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues contentValues = new ContentValues();
   // contentValues.put(COL_1, id);
           contentValues.put(COL_2, name);
    contentValues.put(COL_3, surname);
    contentValues.put(COL_4, marks);

    db.update(TABLE_NAME, contentValues, COL_1 + " =? ", new String[]{id});
    return true;

}
public Cursor getData(int id){
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "select * from student_details where ID="+id+"", null );
    if(res != null && res.moveToFirst()) {
        return res;
    }
    return res;
}
public int deleteData(String id)
{
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete(TABLE_NAME, COL_1 + " =?", new String[]{id});

}

Main Activity

public class MainActivity extends AppCompatActivity {

DatabaseHelper myDB;
EditText editname,editsurname,editmark;
EditText uname,usurname,umark,uid;
Button bsave,bview,bupdate,bshow,bdel;

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

    myDB= new DatabaseHelper(this);

    editname= (EditText) findViewById(R.id.name);
    editsurname= (EditText) findViewById(R.id.surname);
    editmark= (EditText) findViewById(R.id.marks);
    uname= (EditText) findViewById(R.id.editText2);
    uid= (EditText) findViewById(R.id.editText);
    usurname= (EditText) findViewById(R.id.editText3);
    umark= (EditText) findViewById(R.id.editText4);
    bsave= (Button) findViewById(R.id.save);
    bview= (Button) findViewById(R.id.view);
    bupdate= (Button) findViewById(R.id.button2);
    bshow= (Button) findViewById(R.id.button);
    bdel= (Button) findViewById(R.id.button3);
    bshow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(uid.getText().toString().equals(""))
                return;
            int id = Integer.parseInt(uid.getText().toString());
            Cursor res=myDB.getData(id);
            String NAME = res.getString(res.getColumnIndex("NAME"));
            String SURNAME = res.getString(res.getColumnIndex("SURNAME"));
            String MARKS = res.getString(res.getColumnIndex("MARKS"));

            uname.setText(NAME);
            usurname.setText(SURNAME);
            umark.setText(MARKS);
            //Cursor res=myDB.getData(0);


        }
    });
    bdel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Integer deleteAccount = myDB.deleteData(uid.getText().toString());
            if (deleteAccount > 0) {
                Toast.makeText(MainActivity.this, "Data Deleted ", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(MainActivity.this, "Data Not Deleted ", Toast.LENGTH_LONG).show();
            }
        }
    });
    AddData();
  ViewAll();
    UpdateData();


}

public void AddData()
{
    bsave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            boolean isInserted = myDB.insertdata(editname.getText().toString(), editsurname.getText().toString(),
                    editmark.getText().toString());
            if (isInserted = true)
                Toast.makeText(MainActivity.this, " Data Inserted Sucessfully", Toast.LENGTH_LONG).show();
            else
                Toast.makeText(MainActivity.this, " Data Not Inserted", Toast.LENGTH_LONG).show();

        }

    });
}
public void ViewAll()
{
    bview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(MainActivity.this,DisplayData.class);
            startActivity(intent);


           /* Cursor res = myDB.getallData();
            if (res.getCount() == 0) {
                //show error message
                ShowMessage("ERROR", "Nothing Found");
                return;
            }
            StringBuffer buffer = new StringBuffer();
            while (res.moveToNext()) {
                buffer.append("ID :" + res.getString(0) + "\n");
                buffer.append("Name :" + res.getString(1) + "\n");
                buffer.append("SurName :" + res.getString(2) + "\n");
                buffer.append("Marks :" + res.getString(3) + "\n");
            }
            ShowMessage("DATA", buffer.toString());*/

        }
    });

}

Aucun commentaire:

Enregistrer un commentaire