0

SQLiteデータベースからデータをロードするlistViewがあり、今はリストにonclicklistenerを実装したいと思います。ユーザーがリストをクリックすると、次のアクティビティに移動し、対応するデータをTextViewにロードする必要があります。私の質問は、リストのデータをどのように渡すかです。たとえば、「トピック」のリストであり、ユーザーがトピック「マイホーム」をクリックします。トピック「マイホーム」を次のアクティビティに渡して、それぞれ取得する対応するデータを確認したいと思います。

どうすればいいですか?新しいインテントに「putExtras」を追加しますか?または別の方法があります。以下は、リストビューを表示する私のコードの一部です。

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

別のアクティビティに渡すには、インテントを使用します。例...

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

新しいアクティビティ ( in onCreate()) では、インテントを取得し、文字列を取得します...

public class MyNewActivity extends Activity {

    String uriString;

    @Override
    protected void onCreate(...) {

        ...

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

    }
}

編集済み:

リストビューから文字列を取得するには、以下のコードを適用して 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 に答える