1

アプリでお気に入りを整理しようとしています。3つの列を持つデータベースがあります。

_id | name  | description |  star

1    London   some desc.
2    Berlin   some desc.    
3    Paris    some desc.     yes

お気に入りをリストビューで表示したい。refreshCursor()は、「star」列の値が「yes」の名前のリストを返します。

private static final String COLUMN_STAR = "star";

private void refreshCursor() {
        stopManagingCursor(cursor);
        Intent intent = getIntent();
        String TABLE_NAME = intent.getStringExtra("tableName");

        cursor = database.query(TABLE_NAME, null, COLUMN_STAR + " = ?",
                new String[] { "yes" }, null, null, null);

        startManagingCursor(cursor);
    }

大丈夫です。

次に、クリックした後Paris、クリックした位置で追加の文字列を送信します。

lvData.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                String entryID = new Integer(position).toString();

                Intent intent = new Intent();
                intent.setClass(FavouritesActivity.this, ViewFavsActivity.class);

                Bundle b = new Bundle();
                b.putString("elementId", entryID);
                intent.putExtras(b);
                startActivity(intent);
            }
        });

しかし、私は(のendtryID)位置を取得し、の説明0ViewFavsActivity表示しますLondon

の実際の位置を取得Cursorして送信するにはどうすればよいViewFavsActivityですか?助けてください。


(onCreate)の一部FavouritesActivity://メソッドrefreshCursorは上にあります

refreshCursor();
String[] from = new String[] { COLUMN_NAME };
        int[] to = new int[] { R.id.tvText };

    scAdapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor,
            from, to);
    lvData = (ListView) findViewById(R.id.list);
    lvData.setAdapter(scAdapter);

    lvData.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            String entryID = String.valueOf(id);

            Intent intent = new Intent();
            intent.setClass(FavouritesActivity.this, ViewFavsActivity.class);

            Bundle b = new Bundle();
            b.putString("elementId", entryID);
            intent.putExtras(b);
            startActivity(intent);

の一部ViewFavsActivity

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // opening DB

        refreshCursor();
    bundle = getIntent().getExtras();

    TextView titleText = (TextView) findViewById(R.id.titleText);
    setTitle = database.query(TABLE_NAME, new String[] { COLUMN_DESC },
            null, null, null, null, null);

    int entryID = Integer.parseInt(bundle.getString("elementId"));

    setTitle.moveToPosition(entryID);
    titleText.setText(setTitle.getString(setTitle
            .getColumnIndex(COLUMN_DESC)));

    }

    private void refreshCursor() {
        stopManagingCursor(cursor);
        Intent intent = getIntent();

        cursor = database.query(TABLE_NAME, new String[] { COLUMN_ID, COLUMN_STAR, COLUMN_DESC },
                "_id = ? AND star = ?",
                new String[] { intent.getStringExtra("elementId"), "yes" },
                null, null, null);
    }

追加した:

ペーストビンのFavouritesActivity

PastebinのViewFavsActivity

計画

4

2 に答える 2

2

本当の問題は、2つの異なるカーソルを使用していることです。あなたはposition == 0から得ます:

cursor = database.query(TABLE_NAME, null, COLUMN_STAR + " = ?",
            new String[] { "yes" }, null, null, null);

しかし、次に位置0を尋ねます:

setTitle = database.query(TABLE_NAME, new String[] { COLUMN_DESC },
            null, null, null, null, null);

したがって、異なる結果が得られる可能性は高くなります。
また、Bundleは、任意のプリミティブデータ型といくつかのより複雑なデータ型を保持できます。したがって、数値を文字列に変換してから元のデータ型に戻す必要はありません...

より安全なアプローチは、行のIDを使用することです。したがって、これをOnItemClickListener:で使用します。

b.putLong("elementId", id);

そしてでViewFavsActivity

long entryID = bundle.getLong("elementId");
setTitle = database.query(TABLE_NAME, new String[] { COLUMN_DESC },
            "_id = " + entryID, null, null, null, null);

(注:疑わしい場合は、SQLインジェクション攻撃を防ぐために常に?query()のパラメーターを使用しますが、インデックスとして取得したを使用しているため、このコンテキストでは安全です。)selectionArgslong

于 2012-09-26T23:05:05.247 に答える
0

ここでの問題は、カーソルが1つのオブジェクトしか生成しないことです。私はロンドだと信じています、それは正しいですか?そうであれば。リストビューの位置の結果には、常に要素が1つ追加されます。元:

リストビューにオブジェクトが1つしかない場合:

lvData.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            //Here will be showed the 0 position, not 3 like you whant.
            String entryID = new Integer(position).toString();
        }
    });

ソリューション:

メソッド内のビューを、アダプターに配置されたオブジェクトにキャストする必要があります。元:

lvData.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            //Here will be showed the 0 position, not 3 like you whant.
            MyObject object = (MyObject)view; //This object is placed in the Adapter.
            object.getId(); //This method is inserted in my object or you can Override the method    toString() to return the id of your query.

            //Now you can send your ID to another Activity.

            //Put your code here.

        }
    });

これです。私は私の解決策であなたを助けることを願っています。ご不明な点がございましたら、こちらにご質問ください。

-

チアゴL.シルバ

于 2012-09-26T22:05:38.137 に答える