在此示例中,我对imageview有问题 我确实从Gooogle Firebase查看数据 一切都很好,但我无法在图像视图中显示图像, 我已经测试了链接的图片,但没有发现问题 我已在此示例中使用了两类主要活动和模型 我们也有recycleview和card视图 看我的代码:
MainActivity.java
public class MainActivity extends AppCompatActivity {
private EditText editText, etd;
private Button button;
private RecyclerView recyclerView;
private LinearLayoutManager linearLayoutManager;
private FirebaseRecyclerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//mDialog = new ProgressDialog(this);
editText = findViewById(R.id.et);
etd = findViewById(R.id.etd);
button = findViewById(R.id.btn);
recyclerView = findViewById(R.id.list);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Blog_").push();
Map<String, Object> map = new HashMap<>();
// map.put("id",databaseReference.getKey());
map.put("title", editText.getText().toString());
map.put("desc", etd.getText().toString());
databaseReference.setValue(map);
}
});
linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);
fetch();
}
private void fetch(){
Query query = FirebaseDatabase.getInstance().getReference().child("Blog_");
FirebaseRecyclerOptions<Model> options =
new FirebaseRecyclerOptions.Builder<Model>()
.setQuery(query, new SnapshotParser<Model>() {
@NonNull
@Override
public Model parseSnapshot(@NonNull DataSnapshot snapshot) {
return new Model(
snapshot.child("title").getValue().toString(),
snapshot.child("desc").getValue().toString(),
snapshot.child("image").getValue().toString());
}
})
.build();
adapter = new FirebaseRecyclerAdapter<Model, ViewHolder>(options) {
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new ViewHolder(view);
}
@Override
protected void onBindViewHolder(ViewHolder holder, final int position, Model model) {
holder.setTxtTitle(model.getTitle());
holder.setTxtDesc(model.getDesc());
holder.setImg(MainActivity.this ,model.getImage().toString());
Log.i("Trace1: ",model.getImage().toString());
Log.i("Trace2: ",model.getDesc().toString());
holder.root.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, String.valueOf(position), Toast.LENGTH_SHORT).show();
}
});
}
};
recyclerView.setAdapter(adapter);
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public LinearLayout root;
public TextView txtTitle;
public TextView txtDesc;
public ImageView Img;
public ViewHolder(View itemView) {
super(itemView);
root = itemView.findViewById(R.id.list_root);
txtTitle = itemView.findViewById(R.id.list_title);
txtDesc = itemView.findViewById(R.id.list_desc);
Img= itemView.findViewById(R.id.img);
Img.setVisibility(View.INVISIBLE);
}
public void setTxtTitle(String string) {
txtTitle.setText(string);
}
public void setTxtDesc(String string) {
txtDesc.setText(string);
}
public void setImg(final Context cxc, final String image){
Picasso.get().load("https://firebasestorage.googleapis.com/v0/b/fir-app-79785.appspot.com/o/Blog_simple%2Fimage%3A16565?alt=media&token=c0a5bdd5-eaa8-46b8-80f8-d1b8100585d5").resize(400,400).centerCrop().into(Img);
}
}
}
model.class
public class Model {
public String title,desc,image;
public Model(){
}
public Model(String title, String desc ,String image) {
this.title = title;
this.desc = desc;
this.image = image;
}
public void setTitle(String title) {
this.title = title;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getTitle() {
return title;
}
public String getDesc() {
return desc;
}
public void setImage(String image) {
this.image = image;
}
public String getImage() {
return image;
}
}
XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:layout_gravity="center" />
<EditText
android:id="@+id/etd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:layout_gravity="center" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp">
<LinearLayout
android:id="@+id/list_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:background="?attr/selectableItemBackground"
android:padding="16dp">
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="400dp"
android:elevation="10dp" />
<TextView
android:id="@+id/list_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:textColor="@android:color/black"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/list_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="Description" />
</LinearLayout>
</android.support.v7.widget.CardView>