這是我顯示列表的簡單方法。
1)創建一個名爲MySimpleArrayAdapter
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final ArrayList<String> values;
public MySimpleArrayAdapter(Context context, ArrayList<String> values) {
super(context, R.layout.rowlayout, values);
this.context = context;
this.values = values;
}
@Override
public void notifyDataSetChanged() {
// TODO Auto-generated method stub
super.notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = null;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.rowlayout, parent, false);
// Displaying a textview
TextView textView = (TextView) rowView.findViewById(R.id.label);
textView.setText(values.get(position));
return rowView;
}
2)類中創建名爲rowlayout.xml佈局的XML文件。我們有textview,因爲 我們想顯示項目,但你可以顯示一個圖像的項目。使用ImageView的標籤
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="30sp" >
</TextView>
</LinearLayout>
3)返回到主活動的xml文件(activity_main)。我們有你的wifiscan列表
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="@+id/wifiScanResList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/wifiScanButton" >
</ListView>
</LinearLayout>
4)最後,在主要活動,我們適配器設置爲您的列表
public class MainActivity extends Activity {
// The adapter that we gonna use
MySimpleArrayAdapter adapter;
// List of wifi results
ArrayList<String> wifi_results= new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new MySimpleArrayAdapter(this, wifi_results);
setListAdapter(adapter);
getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
// DO something if the user clicked on the item
}
});
}
}
爲什麼會有一個虛擬目錄? – njzk2