我是Android新手,我想用本書的數據創建一個列表。我的列表需要一張圖片。我必須在我的ListView中放入一些來自url的圖像。我從我的數據庫中檢索String urlImages。ListView中的ImageView與ArrayList <String>
這是我customAdapter:
public class customAdapter extends ArrayAdapter<String>{
Context mContext;
ArrayList<String> user_name;
ArrayList<String> book_title;
ArrayList<String> book_author;
ArrayList<String> book_price;
ArrayList<String> book_cover;
public customAdapter(Context context, ArrayList<String> user_name, ArrayList<String> book_title,
ArrayList<String> book_author, ArrayList<String> book_price, ArrayList<String> book_cover) {
super(context, R.layout.row_list_books, book_title);
// TODO Auto-generated constructor stub
this.mContext = context;
this.user_name = user_name;
this.book_title = book_title;
this.book_author = book_author;
this.book_price = book_price;
this.book_cover = book_cover;
}
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View rowView = inflater.inflate(R.layout.row_list_books, null, true);
TextView user = (TextView) rowView.findViewById(R.id.user_name_search);
TextView title = (TextView) rowView.findViewById(R.id.bookTitle);
TextView author = (TextView) rowView.findViewById(R.id.bookAuthor);
TextView price = (TextView) rowView.findViewById(R.id.priceBook);
ImageView bookImage = (ImageView) rowView.findViewById(R.id.coverBookSearch);
user.setText(user_name.get(position));
title.setText(book_title.get(position));
author.setText(book_author.get(position));
price.setText(book_price.get(position));
//// I don't know how I can do ////
bookImage.setImageBitmap(book_cover.get(position));
///////////////////////////////
return rowView;
}
}
這是我ListBooks活動:
Context mContext;
static final String KEY = "KEY", TAG_LOGIN = "TAG_LOGIN";
String key, getTagLogin;
String user_name;
DatabaseReference user;
DatabaseReference userBook = FirebaseDatabase.getInstance().getReference("Books").child("User's books");
ListView mListView;
ArrayList<String> mArrayUserName = new ArrayList<>();
ArrayList<String> mArrayTitle = new ArrayList<>();
ArrayList<String> mArrayAuthor = new ArrayList<>();
ArrayList<String> mArrayPrice = new ArrayList<>();
ArrayList<String> mArrayImage = new ArrayList<>();
customAdapter customAdapter;
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_books);
mContext = this;
Intent intent = getIntent();
key = intent.getStringExtra(KEY);
getTagLogin = intent.getStringExtra(TAG_LOGIN);
mListView = (ListView) findViewById(R.id.listView);
customAdapter = new customAdapter(mContext, mArrayTitle, mArrayAuthor, mArrayUserName, mArrayPrice, mArrayImage);
mListView.setAdapter(customAdapter);
userBook.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
UserBook userBook = dataSnapshot.getValue(UserBook.class);
user_name = userBook.user_name;
String book_title = userBook.book_title;
String book_author = userBook.book_author;
String book_price = userBook.book_price;
String book_cover = userBook.book_urlImage;
mArrayUserName.add(user_name);
mArrayTitle.add(book_title);
mArrayAuthor.add(book_author);
mArrayPrice.add(book_price);
mArrayImage.add(book_cover);
customAdapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
預先感謝您的幫助。
檢查此鏈接,如何從URL加載圖像http://stackoverflow.com/questions/2471935/how-to-load-an-imageview-by-url-in-android –
什麼是問題?是「如何在ImagheView?中顯示來自URL的圖片 –