Android program development ListView+Json+ asynchronous network picture loading + scroll page example (picture can be cached the picture is good chaos)

  • 2021-01-18 06:38:44
  • OfStack

The Gson used to parse Json in the example is downloaded by Google yourself

Lord Activity:


package COM.Example.Main;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import COM.Example.Main.R;
import COM.Example.Main.stringGetJson.User;
import android.app.Activity;
import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
public class stringListActivity extends ListActivity {
private MyAdapter mMyAdapter;
private LinkedList<User> users = null;
//  The current Activity In the ListView
ListView listView = null;
int lastItem = 0;
LinearLayout loadingLayout = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.foodlistactivity);
listView = getListView();
setLoadingLayout();
new readTask().execute(null);
}
private final class MyAdapter extends ArrayAdapter<User> {
public MyAdapter(Activity activity, List<User> newsList) {
super(activity, 0, newsList);
}
private Map<Integer, View> viewMap = new HashMap<Integer, View>();
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View rowView = this.viewMap.get(position);
if (rowView == null) {
User user = users.get(position);
LayoutInflater inflater = ((Activity) this.getContext())
.getLayoutInflater();
holder = new ViewHolder();
rowView = inflater
.inflate(R.layout.foodlistactivity_item, null);
holder.mNameText = (TextView) rowView
.findViewById(R.id.foodItemUsername);
holder.mPhoto = (ImageView) rowView
.findViewById(R.id.foodItemPic);
rowView.setTag(holder);
holder.mNameText.setText(user.Name);
if (!holder.mPhoto.isDrawingCacheEnabled()) {
holder.mPhoto.setTag(user.Pic);
new downImageTask().execute(holder.mPhoto);
holder.mPhoto.setDrawingCacheEnabled(true);
}
viewMap.put(position, rowView);
} else {
holder = (ViewHolder) rowView.getTag();
}
return rowView;
}
public class ViewHolder {
public TextView mNameText;
public ImageView mPhoto;
}
}
public void setLoadingLayout() {
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
ProgressBar progressBar = new ProgressBar(this); progressBar.setPadding(0, 0, 15, 0); layout.addView(progressBar); TextView textView = new TextView(this); textView.setText(" In the load ..."); textView.setGravity(Gravity.CENTER_VERTICAL); layout.addView(textView); layout.setGravity(Gravity.CENTER); loadingLayout = new LinearLayout(this); loadingLayout.addView(layout); loadingLayout.setGravity(Gravity.CENTER); } private class scrollListener implements OnScrollListener { @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { //  when listView There is no data in, or the data exceeds 100 Bar, or not 10 Integer multiples of (i.e., insufficient data) when hiding "More" and undoing onScroll Binding of Events  if (mMyAdapter.getCount() >= 100 || mMyAdapter.getCount() % 10 > 0 || mMyAdapter.getCount() == 0) { listView.removeFooterView(loadingLayout); listView.setOnScrollListener(null); } lastItem = firstVisibleItem + visibleItemCount - 1;//  This reduction 1 Because there is FootView } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if (lastItem == mMyAdapter.getCount() && scrollState == OnScrollListener.SCROLL_STATE_IDLE) { //  Perform page turning operations  new scrollTask().execute(null); } } } public class readTask extends AsyncTask<Object, Void, Void> { @Override protected Void doInBackground(Object... arg0) { users = new stringGetJson().getJson(); return null; } @Override protected void onPostExecute(Void result) { listView.addFooterView(loadingLayout); mMyAdapter = new MyAdapter(stringListActivity.this, users); setListAdapter(mMyAdapter); listView.setOnScrollListener(new scrollListener()); } } public class scrollTask extends AsyncTask<Object, Void, Void> { @Override protected Void doInBackground(Object... arg0) { for (Iterator iterator = new stringGetJson().getJson() .iterator(); iterator.hasNext();) { User user = (User) iterator.next(); users.add(user); } return null; } @Override protected void onPostExecute(Void result) { mMyAdapter.notifyDataSetChanged(); } } }

Json data source (in this example, the json string is written directly in the code, in the real project, it needs to be read from the network, the example already uses multithreading) :


package COM.Example.Main;
import java.lang.reflect.Type;
import java.util.Iterator;
import java.util.LinkedList;
import android.database.MatrixCursor;
import android.widget.ListView;
import android.widget.TextView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class stringGetJson {
//  Set up temporary ID , will be realistic in the project id replace 
private int id = 0;
public stringGetJson() {
super();
}
// through getJson Methods from Json String fetch User The list of 
public LinkedList<User> getJson() {
String jsonData = "[{\"Name\":\"xinuxForJson1\",\"Pic\":\"http://www.qqjay.com/uploads/110414/1_234626_5.jpg\"},{\"Name\":\"xinuxForJson2\",\"Pic\":\"http://www.qqjay.com/uploads/110414/1_234626_3.jpg\"},{\"Name\":\"xinuxForJson3\",\"Pic\":\"http://www.qqjay.com/uploads/110414/1_234626_4.jpg\"},{\"Name\":\"xinuxForJson4\",\"Pic\":\"http://sucai.qqjay.com/fengmian/201104/tupian1/8.jpg\"},{\"Name\":\"xinuxForJson5\",\"Pic\":\"http://sucai.qqjay.com/fengmian/201104/tupian1/10.jpg\"},{\"Name\":\"xinuxForJson6\",\"Pic\":\"http://sucai.qqjay.com/fengmian/201104/tupian1/7.jpg\"},{\"Name\":\"xinuxForJson7\",\"Pic\":\"http://sucai.qqjay.com/fengmian/201104/tupian1/11.jpg\"},{\"Name\":\"xinuxForJson8\",\"Pic\":\"http://sucai.qqjay.com/fengmian/201104/tupian1/12.jpg\"},{\"Name\":\"xinuxForJson9\",\"Pic\":\"http://sucai.qqjay.com/fengmian/201104/tupian1/9.jpg\"},{\"Name\":\"xinuxForJson10\",\"Pic\":\"http://sucai.qqjay.com/fengmian/201104/tupian/7.jpg\"}]";
Type listType = new TypeToken<LinkedList<User>>() {}.getType();
Gson gson = new Gson();
LinkedList<User> users = gson.fromJson(jsonData, listType);
return users;
}
// for getJson Data entity classes are provided 
final static class User {
public String Name;
public String Pic;
}
}

Download the image from AsyncTask


package COM.Example.Main;
import COM.Example.FunctionUtility.Download;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.widget.ImageView;
public class downImageTask extends AsyncTask<ImageView, Void, Bitmap> {
ImageView gView = null;
@Override
protected Bitmap doInBackground(ImageView... arg0) {
gView = (ImageView)arg0[0];
return Download.getBitmapFromUrl(gView.getTag().toString());
}
@Override
protected void onPostExecute(Bitmap result) {
if(result != null){
this.gView.setImageBitmap(result);
}
this.gView = null;
}
}

Network picture download class:


package COM.Example.FunctionUtility;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class Download {
public static Bitmap getBitmapFromUrl(String imgUrl) {
URL url;
Bitmap bitmap = null;
try {
url = new URL(imgUrl);
InputStream is = url.openConnection().getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bitmap = BitmapFactory.decodeStream(bis);
bis.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
}

The above is this site to share Android program development ListView+Json+ asynchronous network picture loading + rolling page example (pictures can be cached, pictures are good chaos) related knowledge, I hope to be helpful to you.


Related articles: