在我的列表視圖中,我正在使用某些數據gettinng可用wi-fi的列表,我想要的是將數組項目[0]顯示爲項目和數組項目[2]作爲一個子項目,而不使用自定義適配器和消耗性列表視圖如何在不使用自定義適配器的情況下將子項目添加到列表視圖
這裏是我將數據添加到列表視圖中的代碼。
import java.util.List;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;
public class MainActivity extends Activity {
WifiManager mainWifiObj;
WifiScanReceiver wifiReciever;
ExpandableListView list;
String wifis[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ExpandableListView)findViewById(R.id.ListView1);
mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiReciever = new WifiScanReceiver();
mainWifiObj.startScan();
}
protected void onPause() {
unregisterReceiver(wifiReciever);
super.onPause();
}
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 = mainWifiObj.getScanResults();
wifis = new String[wifiScanList.size()];
for(int i = 0; i < wifiScanList.size(); i++){
wifis[i] = ((wifiScanList.get(i)).toString());
}
String filtered[] = new String[wifiScanList.size()];
int counter = 0;
for (String eachWifi : wifis) {
String[] temp = eachWifi.split(",");
filtered[counter] = temp[0].substring(5).trim()+"\n" + temp[2].substring(12).trim()+"\n" +temp[3].substring(6).trim();//0->SSID, 2->Key Management 3-> Strength
counter++;
}
list.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, filtered));
}
}
}
爲什麼你不希望使用自定義適配器?懶惰? – Metehan 2014-09-19 06:02:34
我不認爲這是可能的。 – 2014-09-19 06:03:31
@Metehan然後請告訴我如何使用自定義適配器來實現它。我已經使用自定義適配器搜索了很多關於此的內容,但它們都很難理解 – user3853169 2014-09-19 06:09:18