如何从存储为文件的 Parse-server 加载图像并使用 Java 将其显示到 Android-Studio 中的自定义 ListView?
How to load images from Parse-server that is stored as file and display it to custom ListView in Android-Studio using Java?
我需要帮助加载作为文件存储在解析服务器上的图像并在 listView 中显示它们
我有一些图像作为文件存储在解析服务器上,我使用名称(字符串)旁边的解析查询找到它们,并将它们显示在自定义列表视图中,名称在列表中正确显示但是图片被打乱,可能是因为先加载的图片索引较低。
自定义列表适配器:
package com.xyz.abcd;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class ListAdapter extends BaseAdapter {
Context context;
private final String [] values;
private final Bitmap[] images;
public ListAdapter(Context context, String [] values, Bitmap [] images){
//super(context, R.layout.single_list_app_item, utilsArrayList);
this.context = context;
this.values = values;
this.images = images;
}
@Override
public int getCount() {
return values.length;
}
@Override
public Object getItem(int i) {
return i;
}
@Override
public long getItemId(int i) {
return i;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder viewHolder;
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.custom_list, parent, false);
viewHolder.txtName = (TextView) convertView.findViewById(R.id.aNametxt);
viewHolder.icon = (ImageView) convertView.findViewById(R.id.appIconIV);
result=convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
}
viewHolder.txtName.setText(values[position]);
try {
viewHolder.icon.setImageBitmap(images[position]);
}catch (Exception e){
e.printStackTrace();
}
return convertView;
}
private static class ViewHolder {
TextView txtName;
ImageView icon;
}
}
这是查询:
ParseQuery query = new ParseQuery("userData");
query.whereContains("Name",queryValues[0]);
query.whereEqualTo("Gender",queryValues[1]);
query.whereContains("Hair",queryValues[2]);
query.whereContains("Eyes",queryValues[3]);
query.whereContains("Height",queryValues[4]);
query.whereNotEqualTo("Username", ParseUser.getCurrentUser().getUsername());
query.addAscendingOrder("Name");
query.findInBackground(new FindCallback() {
@Override
public void done(List list, ParseException e) {
}
@Override
public void done(Object o, Throwable throwable) {
if(throwable == null){
List <ParseObject> list = (List<ParseObject>) o ;
for(ParseObject object : list){
username.add( object.getString("Username"));
name.add(object.getString("Name"));
age.add(object.getInt("Age"));
facebook.add(object.getString("Facebook"));
instagram.add(object.getString("Instagram"));
snapchat.add(object.getString("Snapchat"));
whatsapp.add(object.getString("Whatsapp"));
heat.add(object.getInt("Heat"));
sem.add(object.getString("Sem")) ;
likes.add(object.getInt("Like")) ;
ParseFile file = object.getParseFile("ProfilePicture");
file.getDataInBackground(new GetDataCallback() {
@Override
public void done(byte[] bytes, ParseException e) {
if(e == null && bytes != null){
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length); // image is downloaded here
images.add(bitmap); // added to arrayList
String[] NAMES = new String[name.size()];
NAMES = name.toArray(NAMES);
Bitmap[] IMAGES = new Bitmap[images.size()];
IMAGES = images.toArray(IMAGES); //ArrayList is converted to a Bitmap array
lAdapter = new ListAdapter(searchResults.this, NAMES, IMAGES); //names and images array is passed to adapter as arrays
ResultsList.setAdapter(lAdapter); //adapter is set
myInstance.dismiss(); //Loading bar dismisses
}else{
Log.i("Error",e.toString());
}
}
});
}
}else{
Toast.makeText(searchResults.this, "Check connection", Toast.LENGTH_SHORT).show();
myInstance.dismiss();
}
}
});
如何使列表正确显示,以便与位置上的图像一起显示。
解决方法:
使用了 glide 库,它异步加载图像并将它们加载到 UI 有序
我需要帮助加载作为文件存储在解析服务器上的图像并在 listView 中显示它们
我有一些图像作为文件存储在解析服务器上,我使用名称(字符串)旁边的解析查询找到它们,并将它们显示在自定义列表视图中,名称在列表中正确显示但是图片被打乱,可能是因为先加载的图片索引较低。
自定义列表适配器:
package com.xyz.abcd;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class ListAdapter extends BaseAdapter {
Context context;
private final String [] values;
private final Bitmap[] images;
public ListAdapter(Context context, String [] values, Bitmap [] images){
//super(context, R.layout.single_list_app_item, utilsArrayList);
this.context = context;
this.values = values;
this.images = images;
}
@Override
public int getCount() {
return values.length;
}
@Override
public Object getItem(int i) {
return i;
}
@Override
public long getItemId(int i) {
return i;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder viewHolder;
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.custom_list, parent, false);
viewHolder.txtName = (TextView) convertView.findViewById(R.id.aNametxt);
viewHolder.icon = (ImageView) convertView.findViewById(R.id.appIconIV);
result=convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
}
viewHolder.txtName.setText(values[position]);
try {
viewHolder.icon.setImageBitmap(images[position]);
}catch (Exception e){
e.printStackTrace();
}
return convertView;
}
private static class ViewHolder {
TextView txtName;
ImageView icon;
}
}
这是查询:
ParseQuery query = new ParseQuery("userData");
query.whereContains("Name",queryValues[0]);
query.whereEqualTo("Gender",queryValues[1]);
query.whereContains("Hair",queryValues[2]);
query.whereContains("Eyes",queryValues[3]);
query.whereContains("Height",queryValues[4]);
query.whereNotEqualTo("Username", ParseUser.getCurrentUser().getUsername());
query.addAscendingOrder("Name");
query.findInBackground(new FindCallback() {
@Override
public void done(List list, ParseException e) {
}
@Override
public void done(Object o, Throwable throwable) {
if(throwable == null){
List <ParseObject> list = (List<ParseObject>) o ;
for(ParseObject object : list){
username.add( object.getString("Username"));
name.add(object.getString("Name"));
age.add(object.getInt("Age"));
facebook.add(object.getString("Facebook"));
instagram.add(object.getString("Instagram"));
snapchat.add(object.getString("Snapchat"));
whatsapp.add(object.getString("Whatsapp"));
heat.add(object.getInt("Heat"));
sem.add(object.getString("Sem")) ;
likes.add(object.getInt("Like")) ;
ParseFile file = object.getParseFile("ProfilePicture");
file.getDataInBackground(new GetDataCallback() {
@Override
public void done(byte[] bytes, ParseException e) {
if(e == null && bytes != null){
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length); // image is downloaded here
images.add(bitmap); // added to arrayList
String[] NAMES = new String[name.size()];
NAMES = name.toArray(NAMES);
Bitmap[] IMAGES = new Bitmap[images.size()];
IMAGES = images.toArray(IMAGES); //ArrayList is converted to a Bitmap array
lAdapter = new ListAdapter(searchResults.this, NAMES, IMAGES); //names and images array is passed to adapter as arrays
ResultsList.setAdapter(lAdapter); //adapter is set
myInstance.dismiss(); //Loading bar dismisses
}else{
Log.i("Error",e.toString());
}
}
});
}
}else{
Toast.makeText(searchResults.this, "Check connection", Toast.LENGTH_SHORT).show();
myInstance.dismiss();
}
}
});
如何使列表正确显示,以便与位置上的图像一起显示。
解决方法: 使用了 glide 库,它异步加载图像并将它们加载到 UI 有序