Android,如何获取列表视图的一行并将其发送到其他选项卡?
Android, how can i get a row of a listview and send to other tab?
我在选项卡 1 的列表视图中扫描了 wifi 网络。所以当我点击一个网络时,我想转到 tab2 并显示点击的网络,但还有更多关于它的高级信息。
如何将网络名称(ssid)发送到另一个选项卡,然后只在选项卡 2 中列出选择的网络?
public class Tab1 extends TabActivity {
WifiManager wifiManager;
WifiScanReceiver wifiReciever;
String ssid;
int channel;
int level;
ArrayAdapter adapter;
ListView list;
ArrayList<String> wifis;
WifiInfo wifiInfo;
TabHost tabHost;
// TabSpec Names
private static final String TAB1 = "Tab1";
private static final String TAB2 = "Tab2";
private static final String TAB3 = "Tab3";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab1);
tabHost = new TabHost(this);
list = (ListView) findViewById(R.id.text);
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiReciever = new WifiScanReceiver();
wifiInfo = wifiManager.getConnectionInfo();
wifis = new ArrayList<String>(); //initialize wifis
wifis.add("loading...");
adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, wifis);
list.setAdapter(adapter);
wifiManager.startScan(); //make sure this is the last call
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
tabHost.setCurrentTab(1);
}
}); }
protected void onPause() {
unregisterReceiver(wifiReciever);
super.onPause();
}
public static int convertFrequencyToChannel(int freq) {
if (freq >= 2412 && freq <= 2484) {
return (freq - 2412) / 5 + 1;
} else if (freq >= 5170 && freq <= 5825) {
return (freq - 5170) / 5 + 34;
} else {
return -1;
}
}
protected void onResume() {
registerReceiver(wifiReciever, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
class WifiScanReceiver extends BroadcastReceiver {
@SuppressLint("UseValueOf")
public void onReceive(Context c, Intent intent) {
List<ScanResult> wifiScanList = wifiManager.getScanResults();
wifis.clear(); //add this
for (int i = 0; i < wifiScanList.size(); i++) {
ssid = wifiScanList.get(i).SSID; //Get the SSID
level = wifiScanList.get(i).level;
channel = wifiScanList.get(i).frequency;
//use add here:
wifis.add(ssid + " " + level + " dBm "); //append to the other data
wifis.toString();
}
adapter.notifyDataSetChanged(); //add this
wifiManager.startScan(); //start a new scan to update values faster
}
}
}
您可以将您的值从一个选项卡传递到另一个选项卡,或者从选项卡 activity 传递到
下面的 tab.like
//Create the public static reference of TabHost and intent in your TabActivity. i.e
public static TabHost tabHost;
public static Intent intent;
//in TabActivity initialize it inside OnCreate
tabHost = getTabHost();
intent =getIntent();
//now inside listitem click pass the values like
YourTabActivity.intent.putExtra("listvalue","your list value");
YourTabActivity.tabHost.setCurrentTab(1);
//now inside the other tabs override the OnResume method to get the value and update your UI
@Override
protected void onResume() {
super.onResume();
String value = YourTabActivity.intent
.getStringExtra("listvalue");
if (null != value)
//use this value :)
}
我在选项卡 1 的列表视图中扫描了 wifi 网络。所以当我点击一个网络时,我想转到 tab2 并显示点击的网络,但还有更多关于它的高级信息。
如何将网络名称(ssid)发送到另一个选项卡,然后只在选项卡 2 中列出选择的网络?
public class Tab1 extends TabActivity {
WifiManager wifiManager;
WifiScanReceiver wifiReciever;
String ssid;
int channel;
int level;
ArrayAdapter adapter;
ListView list;
ArrayList<String> wifis;
WifiInfo wifiInfo;
TabHost tabHost;
// TabSpec Names
private static final String TAB1 = "Tab1";
private static final String TAB2 = "Tab2";
private static final String TAB3 = "Tab3";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab1);
tabHost = new TabHost(this);
list = (ListView) findViewById(R.id.text);
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiReciever = new WifiScanReceiver();
wifiInfo = wifiManager.getConnectionInfo();
wifis = new ArrayList<String>(); //initialize wifis
wifis.add("loading...");
adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, wifis);
list.setAdapter(adapter);
wifiManager.startScan(); //make sure this is the last call
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
tabHost.setCurrentTab(1);
}
}); }
protected void onPause() {
unregisterReceiver(wifiReciever);
super.onPause();
}
public static int convertFrequencyToChannel(int freq) {
if (freq >= 2412 && freq <= 2484) {
return (freq - 2412) / 5 + 1;
} else if (freq >= 5170 && freq <= 5825) {
return (freq - 5170) / 5 + 34;
} else {
return -1;
}
}
protected void onResume() {
registerReceiver(wifiReciever, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
class WifiScanReceiver extends BroadcastReceiver {
@SuppressLint("UseValueOf")
public void onReceive(Context c, Intent intent) {
List<ScanResult> wifiScanList = wifiManager.getScanResults();
wifis.clear(); //add this
for (int i = 0; i < wifiScanList.size(); i++) {
ssid = wifiScanList.get(i).SSID; //Get the SSID
level = wifiScanList.get(i).level;
channel = wifiScanList.get(i).frequency;
//use add here:
wifis.add(ssid + " " + level + " dBm "); //append to the other data
wifis.toString();
}
adapter.notifyDataSetChanged(); //add this
wifiManager.startScan(); //start a new scan to update values faster
}
}
}
您可以将您的值从一个选项卡传递到另一个选项卡,或者从选项卡 activity 传递到
下面的 tab.like//Create the public static reference of TabHost and intent in your TabActivity. i.e
public static TabHost tabHost;
public static Intent intent;
//in TabActivity initialize it inside OnCreate
tabHost = getTabHost();
intent =getIntent();
//now inside listitem click pass the values like
YourTabActivity.intent.putExtra("listvalue","your list value");
YourTabActivity.tabHost.setCurrentTab(1);
//now inside the other tabs override the OnResume method to get the value and update your UI
@Override
protected void onResume() {
super.onResume();
String value = YourTabActivity.intent
.getStringExtra("listvalue");
if (null != value)
//use this value :)
}