Bmob后端云+ImageLoader框架实现图文列表,oppo android面试

dependencies {

implementation fileTree(include: [’*.jar’], dir: ‘libs’)

implementation ‘com.android.support:appcompat-v7:28.0.0’

implementation ‘com.android.support.constraint:constraint-layout:1.1.3’

testImplementation ‘junit:junit:4.12’

androidTestImplementation ‘com.android.support.test🏃1.0.2’

androidTestImplementation ‘com.android.support.test.espresso:espresso-core:3.0.2’

implementation ‘cn.bmob.android:bmob-sdk:3.6.9’

implementation ‘io.reactivex.rxjava2:rxjava:2.2.2’

implementation ‘io.reactivex.rxjava2:rxandroid:2.1.0’

implementation ‘com.squareup.okio:okio:2.1.0’

implementation ‘com.google.code.gson:gson:2.8.5’

implementation ‘com.squareup.okhttp3:okhttp:3.12.0’

implementation ‘com.nostra13.universalimageloader:universal-image-loader:1.9.5’

}

配置AndroidMani

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

fest.xml

在你的应用程序的AndroidManifest.xml文件中添加相应的权限:

配置ContentProvider

同样在的AndroidManifest.xml文件中添加相应的权限:

<provider

android:name=“cn.bmob.v3.util.BmobContentProvider”

android:authorities=“com.example.list_dome.BmobContentProvider”>

初始化BmobSDK

在应用的onCreate方法中初始化BmobSDK

Bmob.initialize(this, “4943e6a9dd93e0df1aee0fc6d54239d9”);

新建list表

对以上都配置完成以后,请到应用的数据库中新建list表,此表中有两列分别为

(列名:name 属性:String 描述:每列的文字信息)

(列名:icon 属性:File 描述:每列的)

在这里插入图片描述

3.2在项目中新建list类

新建的list类应与在Bmob云后端建立list表一 一对应

public class list extends BmobObject {

private String name;

private BmobFile icon;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public BmobFile getIcon() {

return icon;

}

public String getIconUrl(){

return icon.getFileUrl();

}

public void setIcon(BmobFile icon) {

this.icon = icon;

}

}

其中类中的BmobFile对应表中的File,代码块中的getIconUrl()方法用来获取图片的url。

3.3在项目中导入ImageLoader框架

第一种方法:导入imageloader的jar包

点击跳转到下载地址

第二种方法:在Android Studio中导入依赖使用快捷键Alert+ctrl+shift+s,然后点击Dependencies再点击右面的“+”号选择“Library dependency”,把com.nostra13.universalimageloader:universal-image-loader:1.9.5复制在输入框中。

缓存图片配置(初始化ImageLoader)

imageLoader.init(ImageLoaderConfiguration.createDefault(MainActivity.this));

3.4编写ListView适配器

以下代码是对ListView适配器进行的编写,如果想要了解详细的ListView适配器请点击链接[

点击跳转到ListView适配器详细编写博客]( )

实例化ImageLoader对象

// 创建ImageLoader对象

private ImageLoader imageLoader = ImageLoader.getInstance();

主页面的布局文件

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:app=“http://schemas.android.com/apk/res-auto”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:paddingLeft=“16dp”

android:paddingRight=“16dp”

android:paddingBottom=“16dp”

android:paddingTop=“16dp”>

<ListView

android:id="@+id/main_list_view"

android:layout_width=“match_parent”

android:layout_height=“match_parent”>

列表每项布局文件

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“match_parent”

android:layout_height=“100dp”>

<RelativeLayout

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:layout_margin=“10dp”>

<ImageView

android:id="@+id/title_pic"

android:layout_width=“80dp”

android:layout_height=“60dp”

android:layout_centerVertical=“true”

android:layout_alignParentLeft=“true”

android:src="@mipmap/ic_launcher"/>

<TextView

android:id="@+id/title_content"

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:textSize=“16sp”

android:layout_marginLeft=“10dp”

android:layout_toRightOf="@id/title_pic"

/>

适配器编写

//适配器用来进行视图与数据的适配作用

public class NewListAdapter extends BaseAdapter {

private List lists= new ArrayList<>();

public NewListAdapter(List list) {

this.lists=list;

}

@Override

public int getCount() {

return lists.size();

}

@Override

public Object getItem(int position) {

return lists.get(position);

}

@Override

public long getItemId(int position) {

return position;

}

@Override

public View getView(final int position, View convertView, ViewGroup parent) {

ViewHolder viewHolder = new ViewHolder();

if (convertView == null) {

LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

convertView = layoutInflater.inflate(R.layout.item_news_list, null);

viewHolder.titleContent = (TextView) convertView.findViewById(R.id.title_content);

viewHolder.icon = (ImageView) convertView.findViewById(R.id.title_pic);

convertView.setTag(viewHolder);

}else {

viewHolder = (ViewHolder) convertView.getTag();

}

// 创建DisplayImageOptions对象并进行相关选项配置

DisplayImageOptions options = new DisplayImageOptions.Builder()

.showImageOnLoading(R.drawable.ic_launcher_background)// 设置图片下载期间显示的图片

.showImageForEmptyUri(R.drawable.ic_launcher_background)// 设置图片Uri为空或是错误的时候显示的图片

.showImageOnFail(R.drawable.ic_launcher_background)// 设置图片加载或解码过程中发生错误显示的图片

.cacheInMemory(true)// 设置下载的图片是否缓存在内存中

.cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中

.displayer(new RoundedBitmapDisplayer(20))// 设置成圆角图片

.build();// 创建DisplayImageOptions对象

// 使用ImageLoader加载图片

imageLoader.displayImage(lists.get(position).getIcon().getFileUrl(),viewHolder.icon);

viewHolder.titleContent.setText(lists.get(position).getName());

return convertView;

}

public class ViewHolder{

TextView titleContent;

ImageView icon;

}

}

其中displayImage(参数1,参数2)方法用来加载图片,参数1为想要加载图片的url(图片的url通过getFileUrl()方法获取),参数2为ImageView控件。

重写onDestroy()方法回收缓存在内存中的图片

@Override

protected void onDestroy() {

// 回收该页面缓存在内存中的图片

imageLoader.clearMemoryCache();

super.onDestroy();

}

3.5批量的从Bmob云后端的数据库获取数据

//查询所有数据

public void queryPage(){

BmobQuery query = new BmobQuery<>();

//查询存在“objectId”字段的数据。

query.addWhereExists(“objectId”);

//获取查询数据

query.findObjects(new FindListener() {

@Override

public void done(List list, BmobException e) {

if (e == null) {

try {

listView.setAdapter(new NewListAdapter(list));

Toast.makeText(MainActivity.this, “更新列为” + list.size()+“条”, Toast.LENGTH_LONG).show();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值