mardi 24 novembre 2015

Passing clicked list item database columns to another activity

I have made a simple Adapter List view and display the items that are added in sqlite database table. I am displaying just the names of Dishes in MainActivity. I am trying this:

  • When i click on any item it's data table id which is a primary key should be passed to next activity and display ingredients of the dish.

I have tried this many times but i failed to do this. Any suggestions will be appreciated. I am copying all my code.

MainActivity

public class MainActivity  extends ListActivity   {


    private DishOperation dishDBoperation;

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

        dishDBoperation = new DishOperation(this);
        dishDBoperation.open();

        final List values = dishDBoperation.getAllDishes();
        final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1 ,values);
        setListAdapter(adapter);
         ListView listView = getListView();


        listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                Intent intent = new Intent(MainActivity.this, Result.class);
                intent.putExtra("key",position);
                startActivity(intent);
            }
        });
 }

ResultActivity: class where i want to do the above mentioned stuff.

Dish Class

public class Dish {
    private int id;
    private String name;
    private String ingredients;

    public long getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {

        return this.name;
    }

    public void setName(String name) {

        this.name = name;
    }

    public String getIngredients() {
        return this.ingredients;
    }

    public void setIngredients(String ingredients) {
        this.ingredients = ingredients;
    }
    @Override
    public String toString() {

        return "Dish Name: " + name + " || Ingredients: " + ingredients;

    }
}

DishOperation

public class DishOperation {

    // Database fields
    private DataBaseWrapper dbHelper;
    private String[] DISHES_TABLE_COLUMNS = { DataBaseWrapper.DISHES_ID, DataBaseWrapper.DISHES_NAME,DataBaseWrapper.DISHES_INGREDIENTS };

    private SQLiteDatabase database;

    public DishOperation(Context context) {
        dbHelper = new DataBaseWrapper(context);
    }

    public void open() throws SQLException {
        database = dbHelper.getWritableDatabase();
    }

    public void close()
    {
        dbHelper.close();
    }

    public Dish addDishes(String name,String ingredients) {

        ContentValues values = new ContentValues();

        values.put(DataBaseWrapper.DISHES_NAME, name);
        values.put(DataBaseWrapper.DISHES_INGREDIENTS, ingredients);


        long dishId = database.insert(DataBaseWrapper.DISHES, null, values);

        // now that the student is created return it ...
        Cursor cursor = database.query(DataBaseWrapper.DISHES,
                DISHES_TABLE_COLUMNS, DataBaseWrapper.DISHES_ID + " = "

                        + dishId, null, null, null, null);

        cursor.moveToFirst();


        Dish newComment = parseDishes(cursor);
        cursor.close();
        return newComment;
    }

    public void deleteDishes(Dish comment) {
        long id = comment.getId();
        System.out.println("Comment deleted with id: " + id);
        database.delete(DataBaseWrapper.DISHES, DataBaseWrapper.DISHES_ID + " = " + id, null);
    }

    public List getAllDishes() {
        List dishes = new ArrayList();

        Cursor cursor = database.query(DataBaseWrapper.DISHES, DISHES_TABLE_COLUMNS, null, null, null, null, null);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            Dish dish = parseDishes(cursor);
            dishes.add(dish);
            cursor.moveToNext();
        }

        cursor.close();
        return dishes;
    }

    public String getInformation(Dish i)
    {
        long id=i.getId();
       String ing= i.getIngredients();
        return ing;

    }


    private Dish parseDishes(Cursor cursor) {
        Dish dish = new Dish();
        dish .setId((cursor.getInt(0)));
        dish .setName(cursor.getString(1));
        dish .setIngredients(cursor.getString(2));

        return dish ;
    }
}

Aucun commentaire:

Enregistrer un commentaire