LISTING 1:
XML對於每個列表項的佈局 -
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/
apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/listImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:background="#ffcccccc" />
<TextView
android:id="@+id/listTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/listImage"
android:layout_toRightOf="@+id/listImage"
android:text="A List item title"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/listDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/listTitle"
android:layout_marginTop="5dp"
android:maxLines="4"
android:layout_toRightOf="@+id/listImage"
android:text="The List item description"
android:textSize="14sp" />
</RelativeLayout>
清單2:
XML佈局包含列表視圖 -
<?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="match_parent"
android:gravity="center"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:fadingEdge="vertical"
android:fadingEdgeLength="10dp"
android:longClickable="true"
android:listSelector="@drawable/list_selector_background"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
<!-- -->
<TextView
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Loading feed data..." />
<!-- -->
</LinearLayout>
#1 Listview with id of "list"
#2 TextView with id of "empty"
清單3:
ListActivity爲動態的ListView -
public class DynamicListViewActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
ImageListAdapter adapter = new ImageListAdapter(this);
setListAdapter(adapter);
LoadFeedData loadFeedData = new LoadFeedData(adapter);
loadFeedData.execute();
}
}
清單4:
用於裝載供給數據
LoadFeedData類 -
public class LoadFeedData extends
AsyncTask<void, void,="" arraylist<entry="">> {
private final String mUrl =
"URL_QUERING_TO_SERVER";
private final ImageListAdapter mAdapter;
public LoadFeedData(ImageListAdapter adapter) {
mAdapter = adapter;
}
private InputStream retrieveStream(String url) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = null;
httpGet = new HttpGet(url);
HttpResponse httpResponse = null;
try {
httpResponse = client.execute(httpGet);
HttpEntity getResponseEntity = httpResponse.getEntity();
return getResponseEntity.getContent();
} catch (IOException e) {
httpGet.abort();
}
return null;
}
@Override
protected ArrayList<Entry> doInBackground(Void... params) {
InputStream source = retrieveStream(mUrl);
Reader reader = null;
try {
reader = new InputStreamReader(source);
} catch (Exception e) {
return null;
}
Gson gson = new Gson();
SearchResult result = gson.fromJson(reader,SearchResult.class);
return result.getFeed().getEntry();
}
protected void onPostExecute(ArrayList<Entry> entries) {
mAdapter.upDateEntries(entries);
}
}
清單5:
ImageListAdapter用於填充與數據和圖像的ListView -
public class ImageListAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mLayoutInflater;
private ArrayList<Entry> mEntries = new ArrayList<Entry>();
private final ImageDownloader mImageDownloader;
public ImageListAdapter(Context context) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mImageDownloader = new ImageDownloader(context);
}
@Override
public int getCount() {
return mEntries.size();
}
@Override
public Object getItem(int position) {
return mEntries.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView,
ViewGroup parent) {
RelativeLayout itemView;
if (convertView == null) {
itemView = (RelativeLayout) mLayoutInflater.inflate(
R.layout.list_item, parent, false);
} else {
itemView = (RelativeLayout) convertView;
}
ImageView imageView = (ImageView)
itemView.findViewById(R.id.listImage);
TextView titleText = (TextView)
itemView.findViewById(R.id.listTitle);
TextView descriptionText = (TextView)
itemView.findViewById(R.id.listDescription);
String imageUrl = mEntries.get(position).getContent().getSrc();
mImageDownloader.download(imageUrl, imageView);
String title = mEntries.get(position).getTitle().get$t();
titleText.setText(title);
String description =
mEntries.get(position).getSummary().get$t();
if (description.trim().length() == 0) {
description = "Sorry, no description for this image.";
}
descriptionText.setText(description);
return itemView;
}
public void upDateEntries(ArrayList<Entry> entries) {
mEntries = entries;
notifyDataSetChanged();
}
}
大!這樣您將從服務器加載動態數據並將其顯示在列表視圖中。 很顯然,您可以更改其中的一些或更多以添加適合您的應用程序。 乾杯!
這看起來像是一個ListView的教科書。你有沒有考慮過使用一個? – matiash
我做過了,但由於幾個原因,它不太適合我的使用案例。在這種情況下,我有一個ScrollView,它包含一個LinearLayout,我希望爲從數據庫中拉出的每個項目插入上面的代碼。 – opticon
這仍然聽起來像一個ListView的教科書案例... :)嗯,從技術上講,你描述的方法是可行的,但它看起來像'ListView'已經做了什麼(可能更有效率,例如不創建所有兒童觀看,重複使用,&c)。 – matiash