我有一个包含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);
}
}
}
答案 0 :(得分: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;
然后您可以使用行号来获取正确的类引用。
答案 1 :(得分: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,请使用带有scrollview的LinearLayout
答案 2 :(得分:0)
以下是答案: 我必须在列表声明
之后添加它list.setOnItemClickListener(this);