我刚开始在firebase工作。我设法上传了文本和图像,但是,我无法检索要显示在回收器视图中的图像,只能检索文本。我用的是毕加索依赖。我已经包括了我的主要活动。java类,该类负责显示从问题“我的适配器”中的firebase检索的回收器视图项。java类和模型类。我相信,在我将图像URI上载到firebase存储时,我可能犯了没有存储图像URI的错误,因此适配器无法检索图像位置。我想这可能是因为我在研究这个问题时遇到了类似的情况。请协助
下面是我模型java类的代码
public class Model {
// string variable for
// storing employee name.
private String journeyName;
// string variable for storing
// employee contact number
private String journeyDescription;
// string variable for storing
// employee address.
private String journeyLocation;
//lets experiment with an id
private String journeyId;
//an url for an image
private String imageURL;
// an empty constructor is
// required when using
// Firebase Realtime Database.
public Model() {
}
protected Model(Parcel in){
journeyName = in.readString();
journeyDescription = in.readString();
journeyLocation = in.readString();
journeyId = in.readString();
imageURL = in.readString();
}
public static final Parcelable.Creator<Model> CREATOR = new Parcelable.Creator<Model>(){
@Override
public Model createFromParcel(Parcel in) {
return new Model(in);
}
@Override
public Model[] newArray(int size) {
return new Model[size];
}
};
// created getter and setter methods
// for all our variables.
public String getJourneyName() {
return journeyName;
}
public void setJourneyName(String journeyName) {
this.journeyName = journeyName;
}
public String getJourneyDescription() {
return journeyDescription;
}
public void setJourneyDescription(String journeyDescription) {
this.journeyDescription = journeyDescription;
}
public String getJourneyLocation() {
return journeyLocation;
}
public void setJourneyLocation(String journeyLocation) {
this.journeyLocation = journeyLocation;
}
public String getJourneyId() {
return journeyId;
}
public void setJourneyId(String journeyId) {
this.journeyId = journeyId;
}
public String getImageURL() {
return imageURL;
}
public void setImageURL(String imageURL) {
this.imageURL = imageURL;
}
public Model(String journeyName, String journeyDescription, String journeyLocation, String journeyId, String url) {
this.journeyName = journeyName;
this.journeyDescription = journeyDescription;
this.journeyLocation = journeyLocation;
this.journeyId = journeyId;
this.imageURL = url;
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(journeyName);
dest.writeString(journeyDescription);
dest.writeString(journeyLocation);
dest.writeString(journeyId);
dest.writeString(imageURL);
}
}
下面是我的适配器代码
public class JourneyRVAdapter extends RecyclerView.Adapter<JourneyRVAdapter.ViewHolder>{
//creating variables for our list, context interface and position
private ArrayList<Model> modelArrayList;
private Context context;
private JourneyClickInterface journeyClickInterface;
int lastPos = -1;
//creating a constructor
public JourneyRVAdapter(ArrayList<Model> modelArrayList, Context context, JourneyClickInterface journeyClickInterface) {
this.modelArrayList = modelArrayList;
this.context = context;
this.journeyClickInterface = journeyClickInterface;
}
@NonNull
@Override
public JourneyRVAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//inflating our layout file below on line
View view = LayoutInflater.from(context).inflate(R.layout.row, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
//setting data to our recycler view item on below line.
Model model = modelArrayList.get(position);
holder.titleTV.setText(model.getJourneyName());
holder.DescTV.setText(model.getJourneyDescription());
holder.LocationTV.setText(model.getJourneyLocation());
Picasso.get().load(model.getImageURL()).placeholder(R.mipmap.ic_launcher)
.fit()
.centerCrop()
.into(holder.imageIV);
// adding animation to recycler view item on below line.
setAnimation(holder.itemView, position);
holder.rowLY.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
journeyClickInterface.onJourneyClick(position);
}
});
}
private void setAnimation(View itemView, int position) {
///animations implement own logic!!!
if (position > lastPos) {
// on below line we are setting animation.
Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left);
itemView.setAnimation(animation);
lastPos = position;
}
}
@Override
public int getItemCount() {
return modelArrayList.size();
// return (modelArrayList == null) ? 0 : modelArrayList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
//creating variable for our imageview and text views below
private ImageView imageIV;
private TextView titleTV, DescTV, LocationTV;
private LinearLayout rowLY;
public ViewHolder(@NonNull View itemView) {
super(itemView);
//initializing all our variables on below line
imageIV = itemView.findViewById(R.id.rImageView);
titleTV = itemView.findViewById(R.id.rTitleView);
DescTV = itemView.findViewById(R.id.rDescriptionTv);
LocationTV = itemView.findViewById(R.id.rLocationTv);
rowLY = itemView.findViewById(R.id.cardRow);
}
}
// creating an interface for on click
public interface JourneyClickInterface {
void onJourneyClick(int position);
}
}
主要活动的代码这是我将显示回收者视图项目的地方
public class MainActivity extends AppCompatActivity {
private FloatingActionButton FloatingActionButton;
// creating variables for fab, firebase database,
// progress bar, list, adapter,firebase auth,
// recycler view and relative layout.
private FirebaseDatabase firebaseDatabase;
StorageReference storageReference;
private DatabaseReference databaseReference;
private RecyclerView journeyRV;
private FirebaseAuth mAuth;
//private ProgressBar loadingPB;
private ArrayList<Model> modelArrayList;
private JourneyRVAdapter journeyRVAdapter;
private RelativeLayout homeRL;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton = findViewById(R.id.floatingActionButton);
//initialize
journeyRV = findViewById(R.id.idRVJourneys);
//homeRL = findViewById(R.id.)
//loadingPB
firebaseDatabase = FirebaseDatabase.getInstance();
mAuth = FirebaseAuth.getInstance();
modelArrayList = new ArrayList<>();
//on below line we are getting database reference.
databaseReference = firebaseDatabase.getReference("Journeys");
//reference for our storage db
storageReference = FirebaseStorage.getInstance().getReference("images1");
// on below line adding a click listener for our floating action button.
FloatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, AddActivity.class));
}
});
// on below line initializing our adapter class.
journeyRVAdapter = new JourneyRVAdapter(modelArrayList, this, this::onJourneyClick);
//setting layout manager to recycler view on below line
journeyRV.setLayoutManager(new LinearLayoutManager(this));
// setting adapter to recycler view on below line.
journeyRV.setAdapter(journeyRVAdapter);
//on below line calling a method to fetch courses from database
getJourneys();
}
private void getJourneys() {
//on below line we are clearing our list
modelArrayList.clear();
//on below line we are calli g add child event listener methods to read the data
databaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
//hide progress bar
//then add snap shot to our array list on below line
modelArrayList.add(snapshot.getValue(Model.class));
//notify our adapter that data has changed
journeyRVAdapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
//this method is called when new child is added
// we are notifying our adapter and making progress bar
// visibility as gone.
//loadingPB.setVisibility(View.GONE);
journeyRVAdapter.notifyDataSetChanged();
}
@Override
public void onChildRemoved(@NonNull DataSnapshot snapshot) {
// notifying our adapter when child is removed.
journeyRVAdapter.notifyDataSetChanged();
//loadingPB.setVisibility(View.GONE);
}
@Override
public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
// notifying our adapter when child is moved.
//loadingPB.setVisibility(View.GONE);
journeyRVAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(getApplicationContext(), " " + error, Toast.LENGTH_SHORT).show();
}
});
}
}
用于向我的firebase实时数据库和存储添加新条目/记录的代码
public class AddActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
//initializing our edt and btn
journeyNameEdt = findViewById(R.id.idEdtJourneyName);
journeyDescriptionEdt = findViewById(R.id.idEdtJourneyDescription);
journeyLocationEdt = findViewById(R.id.idEdtJourneyLocation);
// for myn image view
imgView = findViewById(R.id.image_view);
//get instance of our firebase database
firebaseDatabase = FirebaseDatabase.getInstance();
//reference for our storage db
storageReference = FirebaseStorage.getInstance().getReference("images1");
//get reference of our database
databaseReference = firebaseDatabase.getReference("Journeys");
//initializing our object
//class variable
model = new Model();
sendDatabtn = findViewById(R.id.idBtnSendData);
btnbrowse = findViewById(R.id.btnbrowse);
//browse imagery
btnbrowse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Image"), Image_Request_Code);
}
});
//adding an onClick listener for sending data
sendDatabtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(FilePathUri != null){
String journey = journeyNameEdt.getText().toString();
String description = journeyDescriptionEdt.getText().toString();
String location = journeyLocationEdt.getText().toString();
String url = FilePathUri.getPath();
journeyId = journey;
//below line is for checking weather the edittext files are empty or not
Model model = new Model(journeyId, journey, description, location,url);
addDataFirebase(journey, description, location);
UploadImage();}
else{
Toast.makeText(getApplicationContext(), "Please make selection ", Toast.LENGTH_SHORT).show();
}
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Image_Request_Code && resultCode == RESULT_OK && data != null && data.getData() != null){
FilePathUri = data.getData();
try{
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), FilePathUri);
imgView.setImageBitmap(bitmap);
}
catch (IOException e){
e.printStackTrace();
}
}
}
public String GetFileExtension (Uri uri){
ContentResolver contentResolver = getContentResolver();
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
return mimeTypeMap.getExtensionFromMimeType(contentResolver.getType(uri));
}
public void UploadImage(){
if (FilePathUri != null) {
StorageReference storageReference1 = storageReference.child(System.currentTimeMillis()+ "."+ GetFileExtension(FilePathUri));
storageReference1.putFile(FilePathUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(getApplicationContext(), "Image uploaded", Toast.LENGTH_SHORT).show();
}
});
}else{
Toast.makeText(AddActivity.this, "Please Select Image or Add Image Name", Toast.LENGTH_SHORT).show();
}
}
private void addDataFirebase (String journey, String description, String location){
// these below lines are used to set data in our object class
model.setJourneyName(journey);
model.setJourneyDescription(description);
model.setJourneyLocation(location);
//we are to use an add value event listener method
//this is called with database reference
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
//inside here we are setting our object class to our database reference
//database reference will send data to firebase
databaseReference.child(journeyId).setValue(model);
//databaseReference.setValue(model);
//if successful show toast
Toast.makeText(AddActivity.this, "data added", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
//if failed show toast
Toast.makeText(AddActivity.this, "Failed to add data"+ error, Toast.LENGTH_SHORT).show();
}
});
}
}
我将使用此方法上载照片。此方法高度安全,并且在后台工作,因此用户不能故意停止图像加载,并且会出现错误://您还可以上载多张照片,而不仅仅是一张照片
public class UploadImageService extends IntentService {
public UploadImageService () {
super("UploadImageService ");
}
protected FirebaseFirestore db; // this Code if youe using Firestore only
protected DocumentReference user_doc ; // this Code if youe using Firestore only
// if Firebase RealTime Just Remove `db` An `usee_doc`
protected ArrayList<String> Urix = new ArrayList<>();
protected FirebaseStorage storage = FirebaseStorage.getInstance();
protected StorageReference storageRef = storage.getReference().child(Objects.requireNonNull(FirebaseAuth.getInstance().getUid()));
protected String Post_Key;
@Override
protected void onHandleIntent(Intent workIntent) {
Urix = workIntent.getExtras().getStringArrayList("Uri");
Post_Key = workIntent.getExtras().getString("Post_Key");
db = FirebaseFirestore.getInstance();
for (int i = 0; i < Urix.size(); i++)
{
SendThis(i);
}
}
public void SendThis (int Currrents)
{
final String Current = Urix.get(Currrents);
File file = new File(Urix.get(Currrents));
int fin = getRandomNumber(900) * getRandomNumber(900) + getRandomNumber(10000) ;
storageRef.child(getRandomNumber(10000) + file.getName() + fin ).putFile(Uri.parse(Urix.get(Currrents)))
.addOnSuccessListener(taskSnapshot -> taskSnapshot.getStorage().getDownloadUrl().addOnSuccessListener(uri -> {
Update(uri.toString());
}))
.addOnFailureListener(e -> {
});
}
private void Update(String url)
{
//Post Key to get child path
// url is your image URL
// Now Just get your child path and add the image url
}
private int getRandomNumber(int max) {
return (new Random()).nextInt((max - 1) + 1) + 1;
}
}
//在“UploadImageActivity或Main Activity”操作中,添加以下代码以启动任务:
public void SendThis (String XS)
{
Intent intent = new Intent(CreateUploadTitle.this ,UploadImageService.class);
intent.putExtra("Uri" , YourCurrentImagePath);
intent.putExtra("Post_Key" , yourPostKeyAfterUploadData);
startService(intent);
}
//在你的主要节日里:
<service android:name=".PostSystem.Server.UploadVideoService"
android:exported="false" />
//现在要获取您的图像,只需在数据库中获取名称//如果您有CurrectFirebase规则
这是我的活动: 卡片xml 活动xml 这是我的自定义适配器 我已经看了很多关于如何让它工作的指南,但它仍然不起作用。有人知道发生了什么事吗?我的版本是25.0。1,并导入所有模块。但它并没有在布局中添加一张卡片。
当我尝试将Image URL解析到ImageView中时,回收器视图不显示,活动为空。我正在使用Picasso将图像加载到适配器类中的onBinfViewHolder方法中。这是相关代码 代表: } RepRvAdapter: } 解析JSON数据的方法: 现在,我有一行解析图像URL的代码被注释掉了。行取消注释后,活动将不再显示。如何获取要在ImageView中解析和显示的图像url?我认为这可
在其他回收器视图中有一个回收器视图。两者都需要垂直滚动。外部回收器视图滚动正常,但内部回收器视图滚动不正常。 这是代码: ViewAdapter如下: 我尝试了以下两种回收商观点,但都无法解决问题 也尝试了这个:
我进行一个API调用,得到一个响应,并将其存储在中。我有1个和1个要填充。现在,当我得到响应时,它是根据JSON(即顺序)的,但当我尝试在适配器中设置它时,它是随机的(即没有顺序) 按顺序如下: 现在,在这个适配器中,ImagePath不是按照以下顺序:
我正在尝试在我的 上实现 ,但我没有得到任何结果。 我遵循了这个教程和这个技巧,但是没有人为我工作。 我已经实现了: 但它不让我编译,因为它们是不同的< code > viewmoder ,因为我创建了两个< code > viewmoder 类,但它们< code >扩展了Recycler。ViewHolder所以我不明白... 我正在尝试这样做,因为我有一个,我希望当列表为空时,它会显示一个,
我正在制作一个应用程序,其中有回收器视图。但我无法在我的应用程序中看到回收器视图。当我检查Logcat时,它显示'e/recyclerview:No adapter attached;正在跳过布局‘。为什么会发生这种情况,我该如何修复? 我的主要活动代码: 我的适配器(PostAdapter.java)类代码: