Android将SQLite数据库中的数据加载到下一个活动中

时间:2012-11-27 07:34:10

标签: android listview

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

我该如何解决?我是否将“putExtras”添加到新的Intent中?或者还有另一种方式。以下是显示列表视图的代码的一部分:

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

EDITED: 这部分是关于下一个活动。

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

请帮忙。

谢谢。

4 个答案:

答案 0 :(得分: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);
        }

或者您可以传递此行的idsummaryIntent.putExtra("SummTopicId", id);)并在下一个主题活动中使用此ID“询问db”

编辑:

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

答案 1 :(得分:0)

您可以使用Intent。

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

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

答案 2 :(得分:0)

使用setContentView(...)之后,您需要引用您的String并获取诸如......

之类的文本
EditText et = (EditText) findViewById(R.id.my_edit_text);
String theText = et.getText().toString();

要将其传递给另一个使用Intent的Activity。实施例...

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

在新的Activity(in onCreate())中,您将获得Intent并检索String ...

public class MyNewActivity extends Activity {

    String uriString;

    @Override
    protected void onCreate(...) {

        ...

        Intent i = getIntent();
        uriString = i.getStringExtra("text_label");

    }
}

已编辑:

从Listview获取String您可以应用以下代码并获取ITEM String并将其传递给Next Activity:

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

答案 3 :(得分:-1)

我猜你正在使用适配器用一些数据填充你的列表。所以你需要覆盖适配器中的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);
            }
        });