0

画像とともに静的データを表示する ListView があります (アクティビティ A など)。ListVew アイテムをクリックすると、データがキャプチャされ、次のアクティビティ (アクティビティ B と呼びましょう) に渡されます。アクティビティ B では、選択した値を DB select where 句に渡し、データをフェッチして、選択に関連する関連情報を表示したいと考えています。これが私のコードスニペットです...

アクティビティ A :

String subproduct = ((TextView) view.findViewById(R.id.product_label)).getText().toString();
                      // Launching new Activity on selecting single List Item

                      SharedPreferences sharedPreferences = getSharedPreferences("pref",MODE_WORLD_READABLE);
                      SharedPreferences.Editor editor = sharedPreferences.edit();
                      editor.clear();
                      editor.putString("subproduct", subproduct);
                      editor.commit();
                      Intent myIntent = new Intent(getApplicationContext(),Details.class);                 
                      startActivity(myIntent);

そして、ここに私のアクティビティBがあります:enter code here

public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.details1);

SharedPreferences myPrefs = this.getSharedPreferences("pref", MODE_WORLD_READABLE); 
String subproduct = myPrefs.getString("subproduct","null");     
TextView txtProduct1 = (TextView) findViewById(R.id.detail_item);
txtProduct1.setText(subproduct);

ここに私のDBHelper.javaがあります。ここでは、選択した値を渡したいと思います。どうすればこれを達成できるか教えてください...

public Cursor getData(){
        //SharedPreferences myPrefs = this.getSharedPreferences("pref", 0);
        //String subproduct = myPrefs.getString("subproduct","null");
        Cursor c = myDataBase.rawQuery("SELECT * FROM Destmain WHERE destname= " + ? + " ", null);
        while (c.moveToNext()){
            DestInfo q = new DestInfo();
            q.setDestination(c.getString(1));
            q.setCity(c.getString(2));
            q.setCountry(c.getString(3));
            q.setPeriod(c.getString(4));
            q.setType(c.getString(5));
            q.setCurrency(c.getString(6));
            q.setBriefhistory(c.getString(7));
            q.setHighlights(c.getString(8));


        }
        c.close();
        return c;

ListView (アクティビティ A) で選択した値を DB データのフェッチに渡し、アクティビティ B に表示する方法を教えてください。ありがとうございます。

4

1 に答える 1

0

アクティビティ A のメソッドを使用しonItemcClickて ID を取得し、2 番目のアクティビティ B に渡すことができます。

onItemClick(AdapterView<?> adapterView, View view, int pos, long id)
newActivity.putExtra("key", id);

その後、アクティビティ B で、この ID を使用してデータベースを検索できます。

Intent newActivity = getIntent();
long id = newActivity.getLongExtras("key", -1);

他の場合には、getLunchDetailsメソッドを使用できます

于 2013-07-19T11:43:23.853 に答える