Reputation: 99
I am trying to parse remote images and text in listview. the code is shown below:
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;
}
I have also xmlparser class. There are different images to be parsed in listview. When i run app images repeats in listview means 1st image displays in all rows of listview than disappear than 2nd appears in all rows and disappear and continuesly images are displaying and disappearing and last image displays in all rows of listview and doesn't disappears. I think i am doing some thing wrong in getView() method in MyCustomadapter class. I can't figure it out that how should solve that problem? I want 1st image in 1st row and 2nd in 2nd row etc. How to correct it? Anybody can help? Thanks in advance.
Upvotes: 0
Views: 532
Reputation: 9439
It looks to be like you're creating a new AsyncTask (BackgroundLostListView) for every "item" in the XML. As each of these AsyncTasks finish, you are setting a new ListAdapter (MyCustomAdapter) for each one and having one Bitmap associated with that Adapter. You want to have one Adapter for the whole ListView that has a complete data set (for all the rows, including either the Bitmaps themselves or perhaps URLs to download them in the background).
Also note, your AsyncTask seems to only be using one of the three parameters, but you've set them all to Strings. For any parameter you aren't using, you should use "Void" instead.
Upvotes: 1