我希望在我的列表视图中插入额外的元素。例如,我有无限的图像列表视图,在每 20 张图像之后,我希望添加带有源页面链接的 web 视图。
根据sugested
Unfortunatly receive NPE onWebView sourse = (WebView) vi.findViewById(R.id.webViewSourse);
public class MediaItemAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
public MediaItemAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
@Override
public int getViewTypeCount() {
return 2; // any number what you need.
}
@Override
public int getItemViewType(int position) {
// view type is managed as zero-based index.
if (position % 11 != 0)
return 0;
else
return 1;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null) {
// View Recycling is managed separately based on its view type,
// so you don't need to worry about view corruptions.
int type = getItemViewType(position);
switch (type) {
case 0:
// inflate imageview here.
convertView = inflater.inflate(R.layout.item_composer, null);
case 1:
// inflate webview here.
convertView = inflater.inflate(R.layout.banner_row, null);
}
}
int type = getItemViewType(position);
switch (type) {
case 0:
// inflate imageview here.
ImageView thumb_image = (ImageView) vi.findViewById(R.id.imagePrev); // thumb
String value = add.get("first_photo_url");
if (value == null || value.trim().length() <= 0
|| value.equalsIgnoreCase("null")
|| value.trim().equalsIgnoreCase("")) {
thumb_image.setImageResource(R.drawable.stub);
} else {
imageLoader.DisplayImage(add.get("first_photo_url"),
thumb_image);
// // do nothing
}
case 1:
// inflate webview here.
WebView sourse = (WebView) vi.findViewById(R.id.webViewSourse);
WebSettings webSettings = sourse.getSettings();
webSettings.setJavaScriptEnabled(true);
// banner.loadUrl("http://www.google.com");
sourse.loadUrl(url);
}
return vi;
}
}