2

私はAndroidアプリに取り組んでいますが、問題が発生していListActivityます。Activityリスト内のどの項目をクリックするかによって、スタートを変えたいと思います。

リストを作成しsetListAdapterてJavaで参照しましたが、で参照する方法がわかりませんOnListItemClick。リスト内の位置を参照する必要があると思います。

Activityを使用してButtons、を設定し、各に対してaを指定してステートメントをOnClickListener使用できます。に相当するものは何ですか?switchcaseButtonListActivity

これが私のコードです:

public class Options extends ListActivity implements {

    String myHistory[]= { "Item 1", "Item 2", "Item 3" };

    //---Set ListView---
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String> ( Options.this,
                                   android.R.layout.simple_list_item_1, myHistory)));
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id); 

        //--if click position 1, do below
        //--Intent a = new Intent("show Item 1's corresponding page");
        //--startActivity(a);

        //--if click item in position 2, do below
        //--Intent b = new Intent("show Item 2's corresponding page");
        //--startActivity(b);

        //--if click item in position 3, do below
        //--Intent c = new Intent("show Item 3's corresponding page");
        //--startActivity(c);
    }
}
4

4 に答える 4

5

他の方法があるかどうかはわかりませんが、次のように行います。クリックリスナーをアクティビティクラスに設定します(アダプタークラスではなく、より理にかなっていると思います)。呼び出したいクラスの配列を保存します。

Class[] classList = new Class[]{class1.class, class2.class....};

リストビューにリスナーを追加します

    lv.setOnItemClickListener(listviewClickListener());

次に、onItemClickListenerメソッド:

private OnItemClickListener listviewClickListener() {
    OnItemClickListener clickListener = new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {


            //And then call the corresponing one
            Intent I= new Intent(thisActivity, classList[i]);
            int id = listOfObjects.get(position).getId();//do whatever you want with the object  on the postition "position"
            Bundle bundle = new Bundle();
            bundle.putInt("id", id);
            i.putExtras(bundle);
            thisActivity.startActivity(i);
        }
    };
    return clickListener;
}

クラスリテラルの配列はテストしていませんが、機能するはずです。

編集:リストビューのすべてのアイテムが同じグループに属しているため、アイテムごとに異なるアクティビティを作成することを検討しています(これについて考えると、あまり意味がありません(コンテキストがない)。同じように使用されます)。それ以外の場合は、アクティビティのリストは必要ありません。目的のアクティビティをインテントに追加するだけです。

于 2012-06-19T07:58:48.717 に答える
2

見た目では、各アイテムには個別のアクティビティ(ゲームのメニューリストなど)があるため、実行時ではなくリストにある特定のデータはごくわずかであるため、短くシンプルにすることをお勧めします......。

    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id); 

        Intent a;
         switch(position){

        case 1:
            a = new Intent("show Item 1's corresponding page");
            break;
         case 2:
           a  = new Intent("show Item 2's corresponding page");
           break;
        case 3:
           a = new Intent("show Item 3's corresponding page");
           break;

        if(null!=a)
        startActivity(a);
     }
}
于 2012-06-19T07:59:19.023 に答える
0

Adapterオブジェクトを使用し、ListViewオブジェクトで設定する必要があります。

ListView listView= getListView();
Adapter  medicineListAdapter = new ArrayAdapter<String>(
        Options.this, android.R.layout.simple_list_item_1, myHistory);

listView.setAdapter(medicineListAdapter);

これを追加し、クラス でonItemClickListenerを実装します。これはそのonItemClickメソッドを参照します。

于 2012-06-19T07:58:28.133 に答える
0
onListItemClick(ListView l, View v, int position, long id) 
position Corresponding your item of myHistory
you can judge  position  to do something :
switch (position) {
 case 1:
      Intent a = new Intent("show Item 1's corresponding page");
    //--startActivity(a);

等々...

于 2012-06-19T07:58:36.933 に答える