2012-11-03 34 views
0

我的应用程序的目标是显示从SQLite数据库中提取的模块列表。如果用户点击一个特定的模块,一个新的活动就开始了,这个新的活动给出了存储在数据库中与该特定模块有关的更多信息Android:如何从光标适配器返回字符串

我希望做的是获取行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

看看的onListItemClick

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

ID的论点是有..这是自动与该行填充由光标适配器标识..

+0

哦对。但是,这是否意味着如果row_id 19被删除,例如row_id 19不会出现在onListItemClick? – Calgar99

+0

如果您的列表视图在删除后仍然有行ID 19,则应在点击项目时获得ID 19。但是,您的适配器/列表视图应该对从列表中删除和删除做出反应 – dymmeh

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