2015-09-19 158 views
0

我在Internet.Json文件中包含字段「圖像」,該字段具有圖像的URL。問題:我如何將圖像放入帶有2個文本字段的Lis​​tView?如何把文本字段,我已經知道,但如何把圖像... 我看到與AsyncTask的解決方案,但我alredy有一個類Extented AsyncTask。 MainActivity類下面,也許它可以幫助。Image to ListView

public class MainActivity extends ListActivity { 
private Context context; 
    private static String url = "https://fierce-citadel-4259.herokuapp.com/hamsters"; 
    private static final String TITLE = "title"; 
    private static final String DESCRIPTION = "description"; 
    private static final String IMAGE = "image"; 
    ArrayList<HashMap<String,String>> jsonlist = new ArrayList<HashMap<String, String>>(); 
    ListView lv; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     new ProgressTask(MainActivity.this).execute(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    private class ProgressTask extends AsyncTask<String,Void,Boolean> { 
     private ProgressDialog dialog; 
     private ListActivity activity; 
     public ProgressTask(MainActivity activity) { 
      this.activity = activity; 
      context = activity; 
      dialog = new ProgressDialog(context); 
     } 
private Context context; 
     protected void onPreExecute(){ 
      this.dialog.setMessage("Progress start"); 
      this.dialog.show(); 
     } 
     protected void onPostExecute(final Boolean success){ 
      if(dialog.isShowing()){ 
       dialog.dismiss(); 
      } 
      ListAdapter adapter = new SimpleAdapter(MainActivity.this,jsonlist, R.layout.list_item,new String[]{TITLE,DESCRIPTION,IMAGE},new int[]{R.id.title,R.id.description,R.id.image}); 
setListAdapter(adapter); 


     } 

     protected Boolean doInBackground(String... args) { 
      JSONParser jParser = new JSONParser(); 
      JSONArray json = jParser.getJSONFromUrl(url); 
      for(int i =0;i<json.length();i++){ 
       try{ 
        JSONObject c = json.getJSONObject(i); 
        String vtitle = c.getString(TITLE); 
        String vdescription = c.getString(DESCRIPTION); 
        String vimage = c.getString(IMAGE); 
HashMap<String,String > map = new HashMap<>(); 
        map.put(TITLE,vtitle); 
        map.put(DESCRIPTION,vdescription); 
        map.put(IMAGE,vimage); 
        jsonlist.add(map); 

       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
      return null; 
     } 
    } 
} 

回答

1

我建議你使用畢加索(http://square.github.io/picasso/)。這很容易。 在Android中,您可以使用ImageView來顯示圖像。這是您應該作爲參數傳遞給畢加索聲明的元素。

希望這會有所幫助!

+0

謝謝,我會盡力開始處理畢加索。 –

0

您可以下載圖片到ImageView s,但由於ImageView s與ListView一起回收,有一些特殊的考慮因素。

下面是一篇文章,這將使你的指導,代碼圖像下載你有確切的情況:

Multithreading For Performance | Android Developers Blog

你也應該看看畢加索的圖片庫。

+0

謝謝,我會讀這個。 –