我正在尝试解析listview中的远程图像和文本。代码如下所示:
public class ParseImagesActivity extends ListActivity {
String myURL;
String xml;
Document doc;
NodeList nodes;
int equal=1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
xml = XmlParser.getXML("http://www.myurl.com");
doc = XmlParser.XMLfromString(xml);
nodes = doc.getElementsByTagName("item");
for ( int i = 0; i < nodes.getLength(); i++) {
Element e = (Element)nodes.item(i);
myURL = XmlParser.getValue(e, "thumb");
startParsing(myURL);
}
}
private void startParsing(String url) {
new backgroundLoadListView().execute(url);
}
public class backgroundLoadListView extends
AsyncTask<String, String, String> {
@Override
protected void onPostExecute(String unused) {
// TODO Auto-generated method stub
setListAdapter(new MyCustomAdapter(ParseImagesActivity.this,
R.layout.row, month));
}
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String url = params[0];
preLoadSrcBitmap(url);
return null;
}
}
public class MyCustomAdapter extends ArrayAdapter<String> {
Bitmap bm;
public MyCustomAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
bm = srcBitmap;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if(row==null){
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.row, parent, false);
}
TextView label=(TextView)row.findViewById(R.id.weekofday);
label.setText(month[position]);
ImageView icon=(ImageView)row.findViewById(R.id.icon);
icon.setImageBitmap(bm);
return row;
}
}
Bitmap srcBitmap;
private void preLoadSrcBitmap(String url){
BitmapFactory.Options bmOptions;
bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
srcBitmap = LoadImage(url, bmOptions);
}
String[] month = {
"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"
};
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
//super.onListItemClick(l, v, position, id);
String selection = l.getItemAtPosition(position).toString();
Toast.makeText(this, selection, Toast.LENGTH_LONG).show();
}
private Bitmap LoadImage(String URL, BitmapFactory.Options options)
{
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in, null, options);
in.close();
} catch (IOException e1) {
}
return bitmap;
}
private InputStream OpenHttpConnection(String strURL) throws IOException{
InputStream inputStream = null;
URL url = new URL(strURL);
URLConnection conn = url.openConnection();
try{
HttpURLConnection httpConn = (HttpURLConnection)conn;
httpConn.setRequestMethod("GET");
httpConn.connect();
if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = httpConn.getInputStream();
}
}
catch (Exception ex){
}
return inputStream;
}
我还有xmlparser类。在listview中有不同的图像需要解析。当我在列表视图中运行应用程序图像时重复意味着列表视图的所有行中的第一个图像显示比第二个出现在所有行中的消失并且消失并且继续图像显示和消失并且最后图像显示在列表视图的所有行中并且不会消失。 我想我在MyCustomadapter类的getView()方法中做了一些错误。我无法弄明白应该如何解决这个问题?我想要第1行的第1张图像和第2行的第2张等。如何纠正?有人可以帮忙吗? 提前谢谢。
答案 0 :(得分:1)
看起来就像是在为XML中的每个“项”创建一个新的AsyncTask(BackgroundLostListView)。当这些AsyncTasks中的每一个完成时,您将为每个AsyncTask设置一个新的ListAdapter(MyCustomAdapter)并且具有与该适配器关联的一个Bitmap。您希望为整个ListView安装一个具有完整数据集的适配器(适用于所有行,包括位图本身或可能在后台下载它们的URL)。
另请注意,您的AsyncTask似乎只使用三个参数中的一个,但您已将它们全部设置为字符串。对于您未使用的任何参数,您应该使用“Void”。