【问题标题】:Android load data from SQLite Database into next activityAndroid 将 SQLite 数据库中的数据加载到下一个活动中
【发布时间】:2014-02-06 02:36:09
【问题描述】:

我有一个从 SQLite 数据库加载数据的 listView,现在我想在列表中实现一个 onclicklistener。当用户点击列表时,它应该将他们带到下一个活动并将相应的数据加载到 TextView 中。我的问题是我将如何传递列表的数据,例如它是“主题”列表,用户单击“我的家”主题。我想将主题“我的家”传递给下一个活动,以便我知道分别要检索哪些相应的数据。

我该怎么做?我是否“将Extras”添加到新意图?或者还有另一种方法。以下是显示列表视图的部分代码:

ListView listContent = (ListView) findViewById(R.id.contentlist);
        mySQLiteAdapter = new SQLiteAdapter(this);
        mySQLiteAdapter.openToRead();

        Cursor cursor = mySQLiteAdapter.queueAll();
        startManagingCursor(cursor);

        String[] from = new String[] { SQLiteAdapter.KEY_CONTENT };
        int[] to = new int[] { R.id.text };

        SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.listrow, cursor, from, to);

        listContent.setAdapter(cursorAdapter);

        mySQLiteAdapter.close();

        //Onclick ListView setlistener
        listContent.setTextFilterEnabled(true);
        listContent.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Intent summaryIntent = new Intent(DocumentListActivity.this, ViewDocumentActivity.class);
//              summaryIntent.putExtra("SummTopic", value);
            }
        });

编辑: 这部分在下一个活动中。

Intent i = getIntent();
        extraTopic = i.getStringExtra("SummTopic");

        mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
        Cursor allrows  = mydb.rawQuery("SELECT * FROM "+  TABLE + " WHERE topic = \" " + extraTopic + " \" " , null);

        Integer cindex = allrows.getColumnIndex("topic");
        Integer cindex1 = allrows.getColumnIndex("text1");
        Integer cindex2 = allrows.getColumnIndex("text2");

从数据库中检索时出错:

android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0

请帮忙。

谢谢。

【问题讨论】:

    标签: android listview


    【解决方案1】:
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Intent summaryIntent = new Intent(DocumentListActivity.this, ViewDocumentActivity.class);
                Cursor c = (Cursor)parent.getItemAtPosition(position);
                summaryIntent.putExtra("SummTopic", c.getString(c.getColumnIndex(SQLiteAdapter.KEY_CONTENT)));
                startActivity(summaryIntent);
            }
    

    或者您可以传递这一行的id (summaryIntent.putExtra("SummTopicId", id);) 并在下一个活动中“询问数据库”以获取具有此 ID 的主题

    编辑:

    protected void onCreate(Bundle savedInstanceState){
        Intent i = getIntent(); 
        String extraTopic = i.getStringExtra("SummTopic"); 
        //or long extraTopic = i.getLongExtra("SummTopic"); if you put id there (which is better)
            mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
        String[] args = new String[] { extraTopic };
        //or String[] args = new String[] { Long.toString(extraTopic) }; with id version
            Cursor singleRow  = mydb.rawQuery("SELECT * FROM "+  TABLE + " WHERE topic=?" , args);
        //args is better then escaping special chars in query
        //and it should be single row so we've changed var name :)
        if(singleRow.moveToFirst()){ //we should do moveToFirst before we can use Cursor
            Integer cindex = allrows.getColumnIndex("topic");
            Integer cindex1 = allrows.getColumnIndex("text1");
            Integer cindex2 = allrows.getColumnIndex("text2");
            //setup views and stuff ....
        }else{
            Toast.makeText(this, "Oops we did not find topic, detail activity was closed", Toast.LENGTH_SHORT).show();
            finish();
        }
    }
    

    【讨论】:

    • 嗨,这确实有帮助。谢谢。但我在检索下一个活动时出错。你能帮忙在编辑部分参考我上面的代码吗?谢谢
    • 亲爱的 Selvin,我还有一个问题,当我按下后退按钮返回从数据库加载数据的 ListView 时,我无法获取列表。知道如何再次从 db 重新加载 ListView 吗?是使用 notifyDataSetChanged(); 吗?有这方面的例子吗?谢谢。
    • 我不知道简单的方法...在您的位置上,我会选择 ContentProvider 和 CursorLoader ...但是要做的工作很多...也许您应该提出新问题并有人会帮助你
    • hmmm 只是在 Activity 中快速尝试,列表中的 cursorAdapter 比 onResume 添加 super.onResume(); cursorAdapter.notifyDataSetChanged();
    【解决方案2】:

    在你使用setContentView(...) 之后,你需要引用你的字符串并获取诸如...的文本

    EditText et = (EditText) findViewById(R.id.my_edit_text);
    String theText = et.getText().toString();
    

    要将其传递给另一个 Activity,您需要使用 Intent。示例...

    Intent i = new Intent(this, MyNewActivity.class);
    i.putExtra("text_label", theText);
    startActivity(i);
    

    在新的 Activity (in onCreate()) 中,您获取 Intent 并检索字符串...

    public class MyNewActivity extends Activity {
    
        String uriString;
    
        @Override
        protected void onCreate(...) {
    
            ...
    
            Intent i = getIntent();
            uriString = i.getStringExtra("text_label");
    
        }
    }
    

    已编辑:

    要从 Listview 获取字符串,您可以应用以下代码并获取 ITEM 字符串并将其传递给下一个活动:

     listContent.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
        String ITEM=listContent.getItemAtPosition(position);
                    //Intent summaryIntent = new Intent(DocumentListActivity.this, // ViewDocumentActivity.class);
    //              summaryIntent.putExtra("SummTopic", value);
                }
            });
    

    【讨论】:

    • 如何从 ListView 中获取文本?也许我没有正确强调。我想获得属于 ListView 的“主题”。
    • 编辑了我的答案。通过它。
    【解决方案3】:

    您可以为此使用 Intent。

    Intent intent= new Intent(context,classname.class);
    intent.putExtra("name",string);
    

    您可以使用intent.getextra()在目标类名中获取它。

    【讨论】:

      【解决方案4】:

      我猜你正在使用一个适配器来用一些数据填充你的列表。所以你需要在你的适配器中覆盖 getItem(int position) 方法:

       @Override
          public Object getItem(int position) {
             //Note that return type should be the same you you use to initilise items oof the adapter. E.g. String or some custom Topic class
             Object someObject = mObjects[position];
      
             return someObject;    
          }
      

      然后你需要为你的列表设置一个项目点击监听器

      mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                  @Override
                  public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                      Object someObject = adapterView.getItemAtPosition(i);
      
                      Intent i = new Intent(this, MyNewActivity.class);
                      i.putExtra("text_label", someObject.toString());
                      startActivity(i);
                  }
              });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-13
        • 1970-01-01
        • 2016-10-02
        相关资源
        最近更新 更多