mercredi 27 mai 2015

Get position and display info and not using if statements?

I'm trying to make an app for android using SQLite Database that is prepopulated with data. The app is like a library with books and when the user click the listview the next activity shows the content of the book. I can access the data and display it with no problem, but the code is becoming pretty big and it does not look nice. Each book have one method in the databasehelper class and a corresponding method in a if statement that gets the position in the listview and displays the corresponding info on another activity. As you can imagine this does not look nice and I'm wondering if there is another way to do this, maybe to have one method to determine what position the user clicked and another for fetching the data from the database, instead of 20( 10 books) that i have now. Appreciate all help!

Code in the activity from where the user click the listview.

 public class MainActivity2Activity extends ActionBarActivity {


listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int        position, long id) {


            if (position == 0){
                Cursor cursor = dbHelper.readBook();
                StringBuilder sb = new StringBuilder();
                while (cursor.moveToNext()) {
                    sb.append(cursor.getString(4) + "\n");

                  Intent i = new Intent(MainActivity2Activity.this,
                   Books.class);
                    i.putExtra(null, sb.toString());
                    startActivity(i);

                }

            }

           else if (position == 1){
                Cursor cursor = dbHelper.readBook2();
                StringBuilder sb = new StringBuilder();
                while (cursor.moveToNext()) {
                    sb.append(cursor.getString(4) + "\n");

                  Intent i = new Intent(MainActivity2Activity.this, 
                     Books.class);
                    i.putExtra(null, sb.toString());
                    startActivity(i);

                }

            }

And so on and so forth for 10 books

 public class DatabaseHelper extends SQLiteOpenHelper {

public static String id = "1";
public static String id2 = "2";
public static String id3 = "3";
//and so on and so forth to ten id's...


 //code to fetch info from the database.
 public Cursor readBook(){
      SQLiteDatabase db = this.getWritableDatabase();
       String sql = "select * from " + TABLE_NAME+ " 
  where " + COL_ID + " = '" + id + "';";

   return db.rawQuery(sql,null);
}

this continues for each book.(I know the sql statement is'nt the proper way to do it and the cursor should be closed but this is a test app and the code will be a little bit different in the final version)

Code used to display the data in the third activity.

public class Books extends ActionBarActivity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_boken);

    openBookLayout = (TextView)findViewById(R.id.openBookLayout);
    String book = getIntent().getExtras().get(null).toString();
    openBookLayout.setText(book); 

Aucun commentaire:

Enregistrer un commentaire