我试图解决以下我遇到的问题 - 无法从视图转换为relativelayout。简而言之,我正在尝试为从JSON数据生成的每个listview数组项目获取背景图像。我已经在JSON中为背景图像设置了url。 JSON函数已经配置,因此我已经能够从在线源将数据填充到应用程序中。
我认为问题在于这些内容
View itemView = inflater.inflate(R.layout.listview_item, parent, false);
RelativeLayout rootRelativeLayout=itemView.findViewById(R.id.rootRelativeLayout);
new ImageDownloadTask(rootRelativeLayout,"http://dooba.ca/analytics/ed.php").execute()
完整的代码在
下面import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
public ListViewAdapter(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView list_item_name;
TextView country;
TextView list_item_price;
ImageView list_item_bac;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.listview_item, parent, false);
RelativeLayout rootRelativeLayout=itemView.findViewById(R.id.rootRelativeLayout);
new ImageDownloadTask(rootRelativeLayout,"http://dooba.ca/analytics/ed.php").execute();
//rootRelativeLayout.setBackground(null);
// Get the position
resultp = data.get(position);
// Locate the TextViews in listview_item.xml
list_item_name = (TextView) itemView.findViewById(R.id.list_item_name);
country = (TextView) itemView.findViewById(R.id.country);
list_item_price = (TextView) itemView.findViewById(R.id.list_item_price);
// Locate the ImageView in listview_item.xml
list_item_bac = (ImageView) itemView.findViewById(R.id.list_item_bac);
// Capture position and set results to the TextViews
list_item_name.setText(resultp.get(MainActivity.LIST_ITEM_NAME));
country.setText(resultp.get(MainActivity.COUNTRY));
list_item_price.setText(resultp.get(MainActivity.LIST_ITEM_PRICE));
// Capture position and set results to the ImageView
// Passes flag images URL into ImageLoader.class
imageLoader.DisplayImage(resultp.get(MainActivity.LIST_ITEM_BAC), list_item_bac);
// Capture ListView item click
itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// Get the position
resultp = data.get(position);
Intent intent = new Intent(context, SingleItemView.class);
intent.putExtra("list_item_name", resultp.get(MainActivity.LIST_ITEM_NAME));
intent.putExtra("country", resultp.get(MainActivity.COUNTRY));
intent.putExtra("list_item_price",resultp.get(MainActivity.LIST_ITEM_PRICE));
intent.putExtra("list_item_bac", resultp.get(MainActivity.LIST_ITEM_BAC));
// Start SingleItemView Class
context.startActivity(intent);
}
});
return itemView;
}
AsyncTask<View,View,View> mytask= new AsyncTask<View,View,View>() {
@Override
protected View doInBackground(View... params) {
Bitmap img = imageLoader.loadImageSync(list_item_bac);
return null;
}
};
class ImageDownloadTask extends AsyncTask<View, View, View>
{
RelativeLayout mrelativelayout;
String downloadUrl;
Bitmap img;
public ImageDownloadTask(RelativeLayout layout,String url)
{
mrelativelayout=layout;
downloadUrl=url;
}
@Override
protected View doInBackground(View... params) {
// TODO Auto-generated method stub
img = imageLoader.loadImageSync(downloadUrl);
return null;
}
protected void onPostExecute(Void result) {
Drawable d = new BitmapDrawable(context.getResources(), img);
mrelativelayout.setBackground(d);
}
}
}
提前感谢您的支持
答案 0 :(得分:1)
更改为:
RelativeLayout rootRelativeLayout=(RelativeLayout)itemView.findViewById(R.id.rootRelativeLayout);