zhu

public class MainActivity extends AppCompatActivity {

    private Gson gson;
    private List data = new ArrayList<>();
    private ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new MyAsyncTask().execute("http://api.expoon.com/AppNews/getNewsList/type/1/p/1");
    }
    class MyAsyncTask extends AsyncTask {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
        @Override
        protected void onPostExecute(String s) {
            gson = new Gson();
            Jiexi jiexi = gson.fromJson(s, Jiexi.class);
            data = jiexi.getData();
            lv = (ListView) findViewById(R.id.lv);
            lv.setAdapter(new Madapter());
            super.onPostExecute(s);
        }
        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }
        @Override
        protected String doInBackground(String... strings) {
            return new NetWork().GetString(strings[0]);
        }
    }
    private class Madapter extends BaseAdapter {
        @Override
        public int getCount() {
            return data.size();
        }
        @Override
        public Object getItem(int i) {
            return data.get(i);
        }
        @Override
        public long getItemId(int i) {
            return i;
        }
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHoder hoder;
            Jiexi.DataBean bean = data.get(i);
            if (view == null) {
                view = View.inflate(MainActivity.this, R.layout.item_lv, null);
                hoder = new ViewHoder();
                hoder.img = (ImageView) view.findViewById(R.id.img);
                hoder.tv = (TextView) view.findViewById(R.id.tv);
                hoder.tv1 = (TextView) view.findViewById(R.id.tv1);
                view.setTag(hoder);
            } else {
                hoder = (ViewHoder) view.getTag();
            }
            new MAsyncTask(hoder.img).execute(bean.getPic_url());
            hoder.tv.setText(bean.getNews_title());
            hoder.tv1.setText(bean.getNews_summary());

            return view;
        }
        class ViewHoder {
            ImageView img;
            TextView tv;
            TextView tv1;
        }
        private class MAsyncTask extends AsyncTask {
            private final ImageView img;

            public MAsyncTask(ImageView img) {
                this.img = img;

            }
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }
            @Override
            protected void onPostExecute(Bitmap bitmap) {
                super.onPostExecute(bitmap);
                img.setImageBitmap(bitmap);
            }
            @Override
            protected void onProgressUpdate(Void... values) {
                super.onProgressUpdate(values);
            }
            @Override
            protected Bitmap doInBackground(String... strings) {
                return new NetWork().getBit(strings[0]);
            }
        }
    }
}

你可能感兴趣的:(zhu)