2017-12-03 60 views
0

我正在使用以下代碼在BaseView上使用BaseAdapter顯示圖像。代碼顯示可繪製文件夾內的圖像。但我想修改代碼,使其顯示從以下數組遠程圖像:如何使用BaseAdapter在ListView中顯示遠程圖像?

String flags[] ={"http://www.website.com/images/usa.png","http://www.website.com/images/china.png","http://www.website.com/images/australia.png","http://www.website.com/images/portugle.png","http://www.website.com/images/norway.png","http://www.website.com/images/new_zealand.png"}; 

能否專家告訴我什麼部分需要提前change.Thanks。

MainActivity.java:

public class MainActivity extends Activity { 

    ListView simpleList; 
    String countryList[] = {"USA", "China", "australia", "Portugle", "Norway", "NewZealand"}; 
    int flags[] = {R.drawable.usa, R.drawable.china, R.drawable.australia, R.drawable.portugle, R.drawable.norway, R.drawable.new_zealand}; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     simpleList = (ListView) findViewById(R.id.simpleListView); 
     CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), countryList, flags); 
     simpleList.setAdapter(customAdapter); 

     simpleList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

       Toast.makeText(getApplicationContext(), "Hello " + countryList[position], Toast.LENGTH_LONG).show(); 


      } 
     }); 
    } 
} 

CustomAdapter.java:

Public class CustomAdapter extends BaseAdapter { 
    Context context; 
    String countryList[]; 
    int flags[]; 
    LayoutInflater inflter; 

    public CustomAdapter(Context applicationContext, String[] countryList, int[] flags) { 
     this.context = context; 
     this.countryList = countryList; 
     this.flags = flags; 
     inflter = (LayoutInflater.from(applicationContext)); 
    } 

    @Override 
    public int getCount() { 
     return countryList.length; 
    } 

    @Override 
    public Object getItem(int i) { 
     return null; 
    } 

    @Override 
    public long getItemId(int i) { 
     return 0; 
    } 

    @Override 
    public View getView(int i, View view, ViewGroup viewGroup) { 
     view = inflter.inflate(R.layout.activity_listview, null); 
     TextView country = (TextView) view.findViewById(R.id.textView); 
     ImageView icon = (ImageView) view.findViewById(R.id.icon); 
     country.setText(countryList[i]); 
     icon.setImageResource(flags[i]); 
     return view; 
    } 
} 

回答

1

您需要:

1)獲取這些圖像在一個單獨的線程,你可以使用抽射,改造,爲此搶劫。

2)根據1)中的任何方法的響應,您必須將您從服務中獲得的值的列表傳遞給適配器的構造函數。您需要爲該模型創建一個POJO,該結構將包含來自REST Web服務的所有元素。

3)建議爲您的listview的適配器使用一個viewholder,以避免反覆膨脹視圖。

+0

感謝您的回覆。我在android開發方面並不是很有經驗,所以你可以指點我一些關於如何編寫函數的教程,並從getView中調用它,並將它傳遞給image數組,以便在listview中顯示它們? – user1788736

+0

是的,你可以使用:https://www.simplifiedcoding.net/android-volley-tutorial-fetch-json/ – HaroldSer

+0

稍後,您可以使用jackson數據綁定將響應中的值映射到POJO(模型類)。看到這個鏈接參考:https://stackoverflow.com/questions/25545984/how-to-use-jackson-objectmapper-to-parse-json-response-to-java-objects – HaroldSer

0

最簡單的事情是用GlidePicasso

@Override 
public View getView(int i, View view, ViewGroup viewGroup) { 
    view = inflter.inflate(R.layout.activity_listview, null); 
    TextView country = (TextView) view.findViewById(R.id.textView); 
    ImageView icon = (ImageView) view.findViewById(R.id.icon); 
    country.setText(countryList[i]); 

    // Assuming flags is now the list of Strings of image urls 
    GlideApp.with(view.getContext()).load(flags[i]).into(icon); 

    return view; 
} 
+0

感謝您的帖子。你能告訴我如何將GlideApp添加到android studio 2.2嗎?我以前從未使用過它。 – user1788736

+0

https://bumptech.github.io/glide/doc/download-setup.html – dominicoder

相關問題