需要帮助将变量从listview项传递到另一个活动

时间:2012-12-25 06:22:09

标签: android listview listadapter

我需要一些解释,说明如何从数据库中为列表视图项传递我想要的任何数据库列值。

我希望能够获取任何值,并通过putExtras将其传递给另一个活动。我知道如何做到这一点,我没有强烈掌握的是如何在通过意图发送之前从列表视图中获取我想要的值。

以下是我的代码和一些帮助您帮助我的详细信息:

Day.java:

package com.dd.gfit;

public class Day {

    private long id;
    private long id_routine;
    private String name;
    private String day;

    public long getId() { return id; }  
    public long getIdRoutine() { return id_routine; }   
    public String getName() { return name; }    
    public String getDay() { return day; }

    public void setId(long id) { this.id = id; }    
    public void setIdRoutine(long id_routine) { this.id_routine = id_routine; } 
    public void setName(String name) { this.name = name; }  
    public void setDay(String day) { this.day = day; }

} 

这是存储和检索值的位置。我知道了。

DaysDataSource.java:

package com.dd.gfit;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DaysDataSource {

    private SQLiteDatabase database;
    private MySQLiteHelper dbHelper;
    private String[] allColumns = { MySQLiteHelper.COLUMN_ID, MySQLiteHelper.COLUMN_ID_ROUTINE, MySQLiteHelper.COLUMN_NAME, MySQLiteHelper.COLUMN_DAY };

    public DaysDataSource(Context context) {
        dbHelper = new MySQLiteHelper(context);
    }

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

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

    public Cursor fetchAllDays(long id) {     
        Cursor cursor = database.query(MySQLiteHelper.TABLE_DAYS, allColumns, MySQLiteHelper.COLUMN_ID_ROUTINE + " = " + id, null, null, null, null);
        if (cursor != null) { cursor.moveToFirst(); }
        return cursor;
    }

} 

这是我可以将所有日期及其列值检索到游标中的数据源,以便在simplecursoradapter中应用于列表视图。我也明白了。

DaysActivity.java:

package com.dd.gfit;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.widget.SimpleCursorAdapter;

public class DaysActivity extends ListActivity {

    private DaysDataSource datasource;
    private SimpleCursorAdapter dataAdapter;

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

        datasource = new DaysDataSource(this);
        datasource.open();

        Cursor cursor = datasource.fetchAllDays(routineDataID);
        String[] columns = new String[] { MySQLiteHelper.COLUMN_NAME, MySQLiteHelper.COLUMN_DAY };
        int[] to = new int[] { R.id.listitem_day_name, R.id.listitem_day_day };
        dataAdapter = new SimpleCursorAdapter(this, R.layout.listitem_day, cursor, columns, to, 0);
        setListAdapter(dataAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {     
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_days, menu);
        return true;        
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long thisID)
    {
        Day day = (Day)getListAdapter().getItem(position);
        long id = day.getId();

        Intent startDayActivity = new Intent(this, DayActivity.class);
        startDayActivity.putExtra("routineDataID", id);
        this.startActivity(startDayActivity);
    }

} 

这是我没有得到的部分。首先,listview显示了我想要的完美。 onListItemClick()函数导致应用程序在触发时崩溃(当我单击列表项时)。 id似乎是空的还是什么?这就像从未为列表项分配值。

我需要知道我在这里缺少什么。我需要有人帮助我并告诉我需要添加到我的代码中以及添加它的位置以便我能够更好地了解将来如何正确地执行此操作。

我假设我需要在尝试使用day函数之前应用getId()的值?任何帮助都会很棒。

编辑:

我在listview中有一个按钮,我也希望能够在点击此按钮时抓住id

使用我想要转换为simplecursoradapter的数组适配器继承代码:

public void onClick(View view) {    
    @SuppressWarnings("unchecked")
    ArrayAdapter<Day> adapter = (ArrayAdapter<Day>)getListAdapter();
    ListView lv = getListView();
    int position = lv.getPositionForView(view);
    Day day = (Day) getListAdapter().getItem(position);
    long id = day.getId();
}

再次编辑:

想出来。下面是代码:

    ListView l = getListView();
    int position = l.getPositionForView(view);

    Cursor cursor = ((SimpleCursorAdapter)l.getAdapter()).getCursor();
    cursor.moveToPosition(position);
    long id = cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.COLUMN_ID));
    String name = cursor.getString(cursor.getColumnIndex(MySQLiteHelper.COLUMN_NAME));

2 个答案:

答案 0 :(得分:1)

更改您的onListItemClick代码,以便从Cursor:

获取所选值
@Override
    protected void onListItemClick(ListView l, View v, int position, long thisID)
    {
      Cursor cursor = ((SimpleCursorAdapter)l.getAdapter()).getCursor();
      cursor.moveToPosition(position);
      long id = cursor.getLong(cursor.getColumnIndex("YOUR_COLUMN_NAME_HERE"));

      Intent startDayActivity = new Intent(DaysActivity.this, DayActivity.class);
      startDayActivity.putExtra("routineDataID", id);
      startActivity(startDayActivity);
    }

答案 1 :(得分:0)

使用此:

String selectedData =(String)(l.getItemAtPosition(position));