1

私は3つのアイテムを含むリストビューを持っています。クリックされたアイテムに応じて新しいアクティビティに移動したいのですが、クリックすると各アイテムが別のアクティビティに移動するはずです。同様の問題に与えられた解決策を試しましたが、別のアクティビティに移動する方法がわかりませんでした

package com.example.wizer;

import java.util.ArrayList;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Options extends Activity implements OnItemClickListener {

    WifiManager wifi;
    BroadcastReceiver receiver;
    IntentFilter filter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_options);
        wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        filter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);


        registerReceiver(rcver, filter);

        ListView list = (ListView) findViewById(R.id.l1);

        ArrayList < String > List = new ArrayList < String > ();
        List.add("List available networks .");
        List.add("List APs .");
        List.add("List Networks according to thier signal strength .");

        ArrayAdapter < String > adp = new ArrayAdapter < String > (this, android.R.layout.simple_list_item_1, List);
        list.setAdapter(adp);

        wifi.startScan();
    }

    // On create
    final BroadcastReceiver rcver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {

        };

    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_options, menu);
        return true;
    }

    public void onItemClick(AdapterView <? > arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        if (arg3 == 0) {
            Intent NL = new Intent(Options.this, Networks.class);
            startActivity(NL);

        } else if (arg3 == 1) {

            Intent NL = new Intent(Options.this, AP.class);
            startActivity(NL);



        } else if (arg3 == 2) {
            Intent NL = new Intent(Options.this, Signal.class);
            startActivity(NL);
        }
    }

}
4

3 に答える 3

1
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

この行は、行番号とその特定のセルのビューへの参照を提供します。ビューで、後でそのアクティビティを開くことができるように、クラスにプロパティを追加できます。これには、カスタム ビュー クラスとカスタム アダプターが必要です。

または、リストが単純な場合は、行の順序に一致するクラスの配列を作成できます。

Class[] classes = new Class[2];
        classes[0] = Integer.class;
        classes[1] = View.class;

次に、行番号を使用して正しいクラス参照を取得できます。

于 2013-04-19T15:51:13.980 に答える
1

編集:

あなたのコードは次のようになります

public class Options extends Activity implements OnItemClickListener {


    Wizer wizer;
    BroadcastReceiver receiver;
    IntentFilter filter;
    HashMap<String,Class> map=new HashMap<String,Class>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_options);
        wizer.Wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        filter=new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);

        registerReceiver(rcver,filter );



        map.put("List available networks .",Networks.class);
        map.put("List APs .", AP.class);     // your second class name
        map.put("List Networks according to thier signal strength .",Signal.class); // third class name 
  }
     final BroadcastReceiver rcver = new BroadcastReceiver() {

         @Override
         public void onReceive(Context context, Intent intent) {

         };

};
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_options, menu);
        return true;
    }

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        Class c= map.get(arg0.getItemAtPosition(arg2).toString());
        Intent NL =new Intent(Options.this,c);
        startActivity(NL);
    }
}

これを試して

ハッシュマップを取る

HashMap<String,Class> map=new HashMap(String,Class);


map.put("List available networks .",Networks.class);
map.put("List APs .", Second.class);     // your second class name
map.put("List Networks according to thier signal strength .",Third.class); // third class name

そしてonItemClickで

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

    Class c= map.get(arg0.getItemAtPosition().toString());
    Intent NL =new Intent(Options.this,c);
    startActivity(NL);
}

:ListViewを使用しないアイテムが少ない場合は、代わりにスクロールビューでLinearLayoutを使用してください

于 2013-04-19T15:58:05.287 に答える