栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

安卓listview实练(使用API接口)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

安卓listview实练(使用API接口)

建立adapter包,内含NewsAdapter类

建立bean包,内含NewsBean和RowsBean两个实体类

建立util工具包,内含JsonParse和OkhttpUtil类都需要实现Serializable类实现序列化

layout布局文件

  (1)activity_mian.xml





  (2)item.xml




    

    

        

        

            

            
        
    

1、清单文件添加网络权限

    build.gradle中的dependencies

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.github.bumptech.glide:glide:4.11.0'
    implementation 'com.google.code.gson:gson:2.8.6'
    implementation 'com.squareup.okhttp3:okhttp:4.4.1'

    compileOnly 'org.projectlombok:lombok:1.18.24'
    annotationProcessor 'org.projectlombok:lombok:1.18.24'
    testCompileOnly 'org.projectlombok:lombok:1.18.24'
    testAnnotationProcessor 'org.projectlombok:lombok:1.18.24'
}

2、NewsBean类包含四个私有属性和其set、get和toString方法

    private int total;
    private int code;
    private String msg;
    private List rows;

RowsBean类包含四个私有属性和其set、get方法

    private String createTime;
    private String cover;
    private String title;
    private int readNum;

3、JsonParse类

public class JsonParse {
    public static JsonParse instance;

    public JsonParse() {
    }

    public static JsonParse getInstance() {
        if (instance == null) {
            instance = new JsonParse();
        }
        return instance;
    }

    public NewsBean getNewsList(String json) {
        Gson gson = new Gson();
        Type listType = new TypeToken() {
        }.getType();
        NewsBean newsBeanJson = gson.fromJson(json, listType);
        return newsBeanJson;
    }

}

     OkhttpUtil类

public class OkhttpUtil {
    public static final String URL = "http://124.93.196.45:10001/prod-api/press/press/list";

    public static void getRequest(String url, Callback callback) {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(url).get().build();
        okHttpClient.newCall(request).enqueue(callback);
    }

}

4、NewsAdapter适配器

public class NewsAdapter extends BaseAdapter {

    private List rowsList = new ArrayList();
    private Context context;
    private LayoutInflater layoutInflater;

    public void setData(List rowsList) {
        for (RowsBean rowsBean : rowsList) {
            Log.i("rowsList",rowsBean.toString());
        }
        this.rowsList = rowsList;
        notifyDataSetChanged();
    }

    public NewsAdapter(Context context) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return rowsList.size();
    }

    @Override
    public RowsBean getItem(int position) {
        return rowsList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((RowsBean) getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(RowsBean rowsBean, ViewHolder holder) {

        Glide.with(context).load("http://124.93.196.45:10001/"+rowsBean.getCover()).error(R.mipmap.ic_launcher).into(((ViewHolder) holder).img);
        holder.tvTitle.setText(rowsBean.getTitle());
        holder.tvCreateTime.setText(rowsBean.getCreateTime());
        holder.tvReadNum.setText(rowsBean.getReadNum()+"");
    }

    protected class ViewHolder {
        private ImageView img;
        private TextView tvTitle;
        private TextView tvCreateTime;
        private TextView tvReadNum;

        public ViewHolder(View view) {
            img = (ImageView) view.findViewById(R.id.img);
            tvTitle = (TextView) view.findViewById(R.id.tv_title);
            tvCreateTime = (TextView) view.findViewById(R.id.tv_createTime);
            tvReadNum = (TextView) view.findViewById(R.id.tv_readNum);
        }
    }
}

5、MainActivity

public class MainActivity extends AppCompatActivity {

    private NewsAdapter adapter;
    private ListView list_view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        list_view.setAdapter(adapter);
    }

    private void initView() {
        list_view = (ListView) findViewById(R.id.listView);
        adapter=new NewsAdapter(this);
        OkhttpUtil.getRequest(OkhttpUtil.URL, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                Log.d("Failure msg", "onFailure:" + e.getMessage());
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                final String res = response.body().string();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        NewsBean newsBeanJson = JsonParse.getInstance().getNewsList(res);
                        List rowsList = newsBeanJson.getRows();
                        if (rowsList != null) {
                            if (rowsList.size() > 0) {
                                adapter.setData(rowsList);
                            }
                        }
                    }
                });
            }
        });
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/969514.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号