Android 台灣中文網
標題: BaseAdapter 要如何取得其中的 item [打印本頁]
作者: pelrbaron 時間: 2016-6-22 03:24
標題: BaseAdapter 要如何取得其中的 item
各位大大,我想請問一下,我利用 BaseAdapter 取得附近的資料,但我想點擊某個 Item 後中的值,我利用
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
}
仍是無法取得其中的值,請知道的大大能告訴小弟,以下是程式碼的部分
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sac_wifi);
init();
Button();
}
private void Button() {
start.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v) {
getssid();
}
});
back.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v) {
startActivity(new Intent(SacWIFI.this,MainActivity.class));
finish();
}
});
}
protected void getssid() {
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
openWifi();
list = wifiManager.getScanResults();
ListView listView = (ListView) findViewById(R.id.listView);
if (list == null) {
Toast.makeText(this, "wifi未打开!", Toast.LENGTH_LONG).show();
}else {
listView.setAdapter(new MyAdapter(this,list));
}
}
private void init() {
start = (Button) findViewById(R.id.start);
back = (Button) findViewById(R.id.back);
}
/**
* 打开WIFI
*/
private void openWifi() {
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
}
}
public class MyAdapter extends BaseAdapter {
LayoutInflater inflater;
List<ScanResult> list;
public MyAdapter(Context context, List<ScanResult> list) {
// TODO Auto-generated constructor stub
this.inflater = LayoutInflater.from(context);
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = null;
view = inflater.inflate(R.layout.wifilist, null);
ScanResult scanResult = list.get(position);
TextView textView = (TextView) view.findViewById(R.id.textView);
textView.setText(scanResult.SSID);
TextView signalStrenth = (TextView) view.findViewById(R.id.signal_strenth);
signalStrenth.setText(String.valueOf(Math.abs(scanResult.level)));
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
//判断信号强度,显示对应的指示图标
if (Math.abs(scanResult.level) > 100) {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.stat_sys_wifi_signal_0));
} else if (Math.abs(scanResult.level) > 80) {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.stat_sys_wifi_signal_1));
} else if (Math.abs(scanResult.level) > 70) {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.stat_sys_wifi_signal_1));
} else if (Math.abs(scanResult.level) > 60) {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.stat_sys_wifi_signal_2));
} else if (Math.abs(scanResult.level) > 50) {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.stat_sys_wifi_signal_3));
} else {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.stat_sys_wifi_signal_4));
}
return view;
}
}
}
作者: ploglin 時間: 2016-6-22 09:18
你可以先重寫在adapter裡面的 getItem
- @Override
- public ScanResult getItem(int position) {
- return list.get(position);
- }
複製代碼
- # 先定義adapter
- MyAdapter adapter = new MyAdapter(this,list)
- listView.setAdapter(adapter);
複製代碼
- # 這樣可以在 onitemclick 的時候去呼叫
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- ScanResult result = adapter.getItem(position);
- }
複製代碼
作者: pelrbaron 時間: 2016-6-22 18:28
請問adapter要定義在哪,在onitemclick的時候 adapter會出現錯誤,能請大大告訴小弟嘛,感謝啊
| 歡迎光臨 Android 台灣中文網 (https://apk.tw/) |
Powered by Discuz! X3.1 |