Android:如何从Cursor适配器返回一个字符串

时间:2012-11-03 15:49:34

标签: android string cursor

我的应用程序的目标是显示从SQLite数据库中提取的模块列表。如果用户单击某个特定模块,则会开始一个新活动,该活动会提供与该特定模块相关的数据库中存储的更多信息。

我希望做的是获取行id并将其发送到intent中,但我不确定如何将游标适配器的行id转换为onClickListItem?任何建议都会非常感激。

光标适配器

public class TestCursorAdapter extends CursorAdapter {

    private LayoutInflater viewInflater;
    public TestCursorAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
        // TODO Auto-generated constructor stub
        viewInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public void bindView(View v, Context context, Cursor c) 
    {
        TextView text_modulecode = (TextView)v.findViewById(R.id.labelModuleCode);
        TextView text_modulename = (TextView)v.findViewById(R.id.labelModuleName);

        text_modulecode.setText(c.getString(c.getColumnIndex(database.KEY_MODULECODE)));
        text_modulename.setText(c.getString(c.getColumnIndex(database.KEY_MODULENAME)));

        String moduleId = c.getString(c.getColumnIndex(database.KEY_ROWID));
    }

    @Override
    public View newView(Context context, Cursor c, ViewGroup parent) {
        View v = viewInflater.inflate(R.layout.listcourses, parent, false);
        return v;
    }
}

来自主要活动

public void onListItemClick(ListView l, View v, int position, long id)
      {
          super.onListItemClick(l, v, position, id);
            Intent intent = new Intent(this,ViewCourse.class);

            //--->             <--
            intent.putExtra(TEST,moduleId);
            startActivity(intent);
      }

2 个答案:

答案 0 :(得分:2)

看一下onListItemClick

的参数
public void onListItemClick(ListView l, View v, int position, long id)

ID就在那里..这是由游标适配器自动填充行ID ..

答案 1 :(得分:0)

intent.putExtra(TEST, String.valueOf(id));