通过包

时间:2015-11-24 13:03:07

标签: android

我尝试的只是将listview所选项ID传递给下一个活动,并在另一页的TextView中显示该ID。当我点击列表项时,我收到一个数字格式异常。请提出任何建议。

  

DataBaseWrapper是创建数据库的类。

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 ;
    }
}

活动1

  public void onCreate(Bundle savedInstanceState) {
    Button   btListe;

    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",id);
      startActivity(intent);
            }
        });

活动2

int value;
public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
        setContentView(R.layout.result);
TextView text = (TextView) findViewById(R.id.editText3);
Bundle extras = getIntent().getExtras();
if (extras != null) {
            long value = extras.getLong("key");
        text.setText(String.valueOf(value));// updated!!


        }


 }

3 个答案:

答案 0 :(得分:1)

更改

value =Integer.parseInt( extras.getString("key"));

 long value = extras.getLong("key");

也在你的Activity2中你应该移动

  super.onCreate(savedInstanceState);
  setContentView(R.layout.result);

onCreate的最开始,否则您的findViewById将返回null。

编辑。

根据您发布的代码,您有ArrayAdapter Dish。您的datasetArrayAdapter应该反映出来,这可以通过Generics完成。

要将行的id作为[{1}}的参数,您必须覆盖onItemClick。 E.g。

getItemId

答案 1 :(得分:0)

在活动1中,ID的类型为long,在活动2中,您尝试将其检索为String。 按extras.getLong("key");

检索

答案 2 :(得分:0)

您应该使用value = extras.getLong("key"));