好吧,我正在关注android hive。我尝试使用自己的代码。但是在CustomAdapter中,我遇到了一块导致不显示图像的岩石。那里有文件,这就是json我知道我弄乱了CustomAdapter,请做个建议。谢谢
结构:
AppController的
LruBitmapCache
MainActicity
MovieCustomAdapter
//我只会上传MovieCustomAdapter和MainActivity.java,因为其余的LruBitmap和AppCOntroller是相同的。
MovieCustomAdapter.java
public class MovieCustomAdapter extends BaseAdapter {
static class ViewHolder{
private NetworkImageView networkImageView;
private TextView title_1,review_2,genre_3,year_4;
}
private static LayoutInflater layoutInflater=null;
private Context context;
String title[],review[],year[];
String image[];
ViewHolder viewHolder;
ImageLoader imageLoader= AppController.getInstance().getImageLoader();
public MovieCustomAdapter(Context context,String image[],String title[],String review[],String year[]){
this.context=context;
this.image=image;
this.title=title;
this.review=review;
//this.genre=genre;
this.year=year;
}
@Override
public int getCount() {
return title.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v=convertView;
if(v==null){
layoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=layoutInflater.inflate(R.layout.movie_custom_layout,parent,false);
viewHolder=new ViewHolder();
viewHolder.networkImageView=(NetworkImageView)v.findViewById(R.id.thumbnail);
viewHolder.title_1=(TextView)v.findViewById(R.id.title);
viewHolder.review_2=(TextView)v.findViewById(R.id.rating);
viewHolder.genre_3=(TextView)v.findViewById(R.id.genre);
viewHolder.year_4=(TextView)v.findViewById(R.id.releaseYear);
v.setTag(viewHolder);
}else {
viewHolder=(ViewHolder)v.getTag();
}
viewHolder.title_1.setText(title[position]);
viewHolder.review_2.setText(review[position]);
viewHolder.year_4.setText(year[position]);
//HERE..................viewHolder.networkImageView whatever !
return v;
}
}
//这是我怀疑显示Images的地方。这是我使用自定义适配器的方式。之前我使用我的本地图像以这种方式显示,但是因为这些图像正在接收并将显示在列表视图,所以这里有困惑。在上面提到的教程中,他们采用了另一种方法,但请尽量坚持下去。
MainActivity.java
public class MainActivity extends AppCompatActivity {
private MovieCustomAdapter movieCustomAdapter;
private ListView lv1;
String title_k[],review_k[],year_k[];
String image_k[];
ProgressDialog progressDialog;
private static String url="http://api.androidhive.info/json/movies.json";
ArrayList<HashMap<String,String>> hashMapArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
lv1=(ListView)findViewById(R.id.listView);
progressDialog=new ProgressDialog(this);
progressDialog.setTitle("Loading");
progressDialog.setMessage("getting Images");
progressDialog.setIcon(R.mipmap.ic_launcher);
progressDialog.setCancelable(false);
showDialog();
JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response_jsonArray) {
Log.d("Rcv",response_jsonArray.toString());
hashMapArrayList=new ArrayList<HashMap<String, String>>();
for (int i=0;i<response_jsonArray.length();i++){
try {
JSONObject jsonObject=response_jsonArray.getJSONObject(i);
HashMap<String,String> hashMap=new HashMap<>();
hashMap.put("image_key",jsonObject.getString("image"));
hashMap.put("title_key",jsonObject.getString("title"));
hashMap.put("review_key",jsonObject.getString("rating"));
hashMap.put("year_key",jsonObject.getString("releaseYear"));
hashMapArrayList.add(hashMap);
} catch (JSONException e) {
e.printStackTrace();
}
}
image_k=new String[hashMapArrayList.size()];
title_k=new String[hashMapArrayList.size()];
review_k=new String[hashMapArrayList.size()];
year_k=new String[hashMapArrayList.size()];
for(int i=0;i<hashMapArrayList.size();i++) {
image_k[i]=hashMapArrayList.get(i).get("image_key");
title_k[i]=hashMapArrayList.get(i).get("title_key");
review_k[i]=hashMapArrayList.get(i).get("review_key");
year_k[i]=hashMapArrayList.get(i).get("year_key");
movieCustomAdapter=new MovieCustomAdapter(MainActivity.this,image_k,title_k,review_k,year_k);
lv1.setAdapter(movieCustomAdapter);
Log.d("Log","myACustom");
}
movieCustomAdapter.notifyDataSetChanged();
hideDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(getApplicationContext(), "server error"+volleyError.getMessage(), Toast.LENGTH_SHORT).show();
hideDialog();
}
});
AppController.getInstance().addToRequestQueue(jsonArrayRequest);
}
private void showDialog(){
if(!progressDialog.isShowing()){
progressDialog.show();
}
}
private void hideDialog(){
if(progressDialog.isShowing()){
progressDialog.dismiss();
}
}
}
深受赞赏