2013-09-05 51 views
1

Json android,我有一個加載圖像和文本,完美的作品,但圖像與文本不匹配,圖像重複骨和文本,我錯了, m a newbieJson android圖像和文字重複

public class CustomAdapter extends BaseAdapter { 

    LayoutInflater inflator; 
    ArrayList<JSONFields> result; 

    public CustomAdapter(Context context, ArrayList<JSONFields> result) { 
     // TODO Auto-generated constructor stub 

     inflator = LayoutInflater.from(context); 
     this.result = result; 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return result.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 

     Holder holder; 
     DownloadImageTask task = null; 
     if (convertView == null) { 
      holder = new Holder(); 
      convertView = inflator.inflate(R.layout.helper, null); 
      holder.image = (ImageView) convertView 
        .findViewById(R.id.imageView1); 
      holder.textview1 = (TextView) convertView 
        .findViewById(R.id.textView1); 
      holder.descrip = (TextView) convertView 
        .findViewById(R.id.descripcion); 


      task = new DownloadImageTask(holder.image); 
      convertView.setTag(holder); 

     } else { 
      holder = (Holder) convertView.getTag(); 
     } 

     JSONFields fields = new JSONFields(); 

     fields = result.get(position); 
     if (task != null) 
      task.execute(fields.getUrl()); 


     holder.textview1.setText(fields.getName()); 
     holder.descrip.setText(fields.getDescripcion()); 



     return convertView; 

    } 

    public class Holder { 
     ImageView image; 
     TextView textview1; 
     TextView descrip; 
    } 

    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 
     ImageView bmImage; 

     public DownloadImageTask(ImageView bmImage) { 
      this.bmImage = bmImage; 


     } 

     protected Bitmap doInBackground(String... urls) { 
      String urldisplay = urls[0]; 
      Bitmap mIcon11 = null; 
      try { 
       InputStream in = new java.net.URL(urldisplay).openStream(); 
       mIcon11 = BitmapFactory.decodeStream(in); 
       in.close(); 
      } catch (Exception e) { 
       Log.e("Error", e.getMessage()); 
       e.printStackTrace(); 
      } 
      return mIcon11; 
     } 

     protected void onPostExecute(Bitmap result) { 
      bmImage.setImageBitmap(result); 
     } 
    } 

} 



import com.ditec.json.data.JSONFields; 

public class MainActivity extends Activity { 

    public static final String URL = "http://169.254.65.190:5000/fbsharing/7BkWVF6r"; 
    ListView listview; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     listview = (ListView) findViewById(R.id.listView1); 

     GetJSON json = new GetJSON(); 
     json.execute(URL); 

    } 

    public class GetJSON extends AsyncTask<String, Void, ArrayList<JSONFields>> { 

     @Override 
     protected ArrayList<JSONFields> doInBackground(String... params) { 
      // TODO Auto-generated method stub 

      try { 
       String url = params[0]; 
       HttpClient client = new DefaultHttpClient(); 
       HttpGet get = new HttpGet(url); 
       HttpResponse response; 
       response = client.execute(get); 
       InputStream content = response.getEntity().getContent(); 
       BufferedReader reader; 
       reader = new BufferedReader(new InputStreamReader(content, "iso-8859-1"), 8); 
       String line; 
       String json = ""; 
       while ((line = reader.readLine()) != null) { 
        json += line; 
       } 

       JSONArray array = new JSONArray(json); 
       ArrayList<JSONFields> alData = new ArrayList<JSONFields>(); 
       for (int i = 0; i < array.length(); i++) { 

        JSONFields data = new JSONFields(); 
        JSONObject obj = array.getJSONObject(i); 

        data.setUrl(obj.getString("url")); 
        data.setName(obj.getString("name")); 
        data.setDescription(obj.getString("descripcion")); 
        alData.add(data); 
       } 
       System.out.println("Data returned sucessfully"); 
       return alData; 

      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(ArrayList<JSONFields> result) { 
      // TODO Auto-generated method stub 
      super.onPostExecute(result); 

      CustomAdapter adapter = new CustomAdapter(MainActivity.this, result); 

      listview.setAdapter(adapter); 

     } 
    } 

} 



public class JSONFields { 

    private String name; 
    private String url; 
    private String descripcion; 

    public String getName() { 
     return name; 
    } 
    public String getUrl() { 
     return url; 
    } 
    public String getDescripcion() { 
     return descripcion; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public void setUrl(String url) { 
     this.url = url; 
    } 
    public void setDescription(String descripcion) { 
     this.descripcion = descripcion; 
    } 

} 

回答

0

看看這個帖子,它解決了圖像與您的文本不匹配的確切問題(由於視圖在圖像加載時被重用/回收)。 Listview with images, but when scroll the List, the images apear in wrong rows 一個簡單的解決方法是在imageview上使用setTag和getTag來設置一個字符串(例如url),用於標識應該在imageview中顯示的圖像,然後檢查標記是否仍然相同當你不加載圖像時,捕捉imageview(convertview)已被回收的情況。