0

我有一个从 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

请帮忙。

谢谢你。

4

4 回答 4

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 的主题的下一个活动中“询问 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();
    }
}
于 2012-11-27T08:54:33.983 回答
0

您可以为此使用 Intent。

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

您可以使用intent.getextra().

于 2012-11-27T07:43:30.280 回答
0

使用后,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);
            }
        });
于 2012-11-27T07:46:11.503 回答
-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);
            }
        });
于 2012-11-27T08:31:19.480 回答