我创建了一个简单的Adapter List视图,并显示了在sqlite数据库表中添加的项目。我只在MainActivity
中显示了Dishes的名字。我正在尝试这个:
我已经尝试了很多次,但我没有这样做。任何建议将不胜感激。我正在复制我的所有代码。
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 :我想要做上述内容的课程。
菜类
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 ;
}
}
答案 0 :(得分:0)
onItemClick
会为您提供点击项position
,您可以使用它来获取Dish
对象。看看示例代码。
public class MainActivity extends ListActivity {
private DishOperation dishDBoperation;
private List values; // create global list object
@Override
public void onCreate(Bundle savedInstanceState) {
...
values = dishDBoperation.getAllDishes();
...
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//get Dish object from select item position.
Dish dish = (Dish)values.get(position);
Intent intent = new Intent(MainActivity.this, Result.class);
intent.putExtra("key",dish.getId());
startActivity(intent);
}
});
}
}