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

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 回答
0

这是答案:我必须在列表声明之后添加它

list.setOnItemClickListener(this);
于 2013-04-19T17:34:21.783 回答