英文:
issue onactivityresult in recyclerview
问题
以下是您提供的代码部分的翻译:
MainActivity(主活动):
package com.example.myapuuuplication;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.sangcomz.fishbun.FishBun;
import com.sangcomz.fishbun.adapter.image.impl.GlideAdapter;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
Adapter adapter = new Adapter();
List<Modell> uris = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView rv = findViewById(R.id.rv);
rv.setAdapter(adapter);
rv.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
Button btn = findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openpicker();
}
});
}
private void openpicker() {
FishBun.with(this)
.setImageAdapter(new GlideAdapter())
.setMaxCount(3)
.setActionBarColor(Color.parseColor("#5D4037"))
.startAlbum();
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case FishBun.FISHBUN_REQUEST_CODE:
if (resultCode == RESULT_OK) {
uris = data.getParcelableArrayListExtra(FishBun.INTENT_PATH);
adapter.setUris(uris);
break;
}
}
}
}
Adapter(适配器):
package com.example.myapuuuplication;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.sangcomz.fishbun.FishBun;
import java.util.ArrayList;
import java.util.List;
public class Adapter extends RecyclerView.Adapter<Adapter.VH> {
List<Modell> uris = new ArrayList<>();
public void setUris(List<Modell> uris) {
this.uris = uris;
}
@NonNull
@Override
public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.model, parent, false);
return new VH(view);
}
@Override
public void onBindViewHolder(@NonNull VH holder, int position) {
Modell modell = uris.get(position);
VH vh = holder;
vh.img.setImageURI(modell.getImg());
}
@Override
public int getItemCount() {
return uris.size();
}
public class VH extends RecyclerView.ViewHolder {
ImageView img;
public VH(@NonNull View itemView) {
super(itemView);
img = itemView.findViewById(R.id.imgmodel);
}
}
}
Modell(模型):
package com.example.myapuuuplication;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
public class Modell implements Parcelable {
Uri img;
public Modell(Uri img) {
this.img = img;
}
protected Modell(Parcel in) {
img = in.readParcelable(Uri.class.getClassLoader());
}
public static final Creator<Modell> CREATOR = new Creator<Modell>() {
@Override
public Modell createFromParcel(Parcel in) {
return new Modell(in);
}
@Override
public Modell[] newArray(int size) {
return new Modell[size];
}
};
public Uri getImg() {
return img;
}
public void setImg(Uri img) {
this.img = img;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeParcelable(img, i);
}
}
请注意,我已经按照您的要求只返回了翻译好的部分,没有包含其他内容。如果您有任何问题或需要进一步的帮助,请随时提问。
英文:
i want to add images from gallery and add to arraylist and show them into recyclerview
but cant use onactivityresult in adapter -_-
i used Fishbun for add images from gallery but when i clicking ok the images not showing in recyclerview
im new in android and dont know how to do that
here is my MainActivity:
package com.example.myapuuuplication;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.sangcomz.fishbun.FishBun;
import com.sangcomz.fishbun.adapter.image.impl.GlideAdapter;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
Adapter adapter = new Adapter();
List<Modell> uris = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView rv = findViewById(R.id.rv);
rv.setAdapter(adapter);
rv.setLayoutManager(new LinearLayoutManager(this,RecyclerView.HORIZONTAL,false));
Button btn = findViewById(R.id.button);
btn
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openpicker();
}
});
}
private void openpicker() {
FishBun.with(this).setImageAdapter(new GlideAdapter())
.setMaxCount(3)
.setActionBarColor(Color.parseColor("#5D4037"))
.startAlbum();
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case FishBun.FISHBUN_REQUEST_CODE:
if (resultCode == RESULT_OK) {
uris = data.getParcelableArrayListExtra(FishBun.INTENT_PATH);
adapter.setUris(uris);
break;
}
}
}
}
Adapter:
package com.example.myapuuuplication;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.sangcomz.fishbun.FishBun;
import java.util.ArrayList;
import java.util.List;
import static android.app.Activity.RESULT_OK;
public class Adapter extends RecyclerView.Adapter<Adapter.VH> {
List<Modell> uris = new ArrayList<>();
public void setUris(List<Modell> uris) {
this.uris = uris;
}
@NonNull
@Override
public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.model,parent,false);
return new VH(view);
}
@Override
public void onBindViewHolder(@NonNull VH holder, int position) {
Modell modell = uris.get(position);
VH vh = holder;
vh.img.setImageURI(modell.getImg());
}
@Override
public int getItemCount() {
return uris.size();
}
public class VH extends RecyclerView.ViewHolder{
ImageView img;
public VH(@NonNull View itemView) {
super(itemView);
img = itemView.findViewById(R.id.imgmodel);
}
}
}
And Model:
package com.example.myapuuuplication;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
public class Modell implements Parcelable {
Uri img;
public Modell(Uri img) {
this.img = img;
}
protected Modell(Parcel in) {
img = in.readParcelable(Uri.class.getClassLoader());
}
public static final Creator<Modell> CREATOR = new Creator<Modell>() {
@Override
public Modell createFromParcel(Parcel in) {
return new Modell(in);
}
@Override
public Modell[] newArray(int size) {
return new Modell[size];
}
};
public Uri getImg() {
return img;
}
public void setImg(Uri img) {
this.img = img;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeParcelable(img, i);
}
}
答案1
得分: 1
你正在使用适配器设置
public void setUris(List<Modell> uris) {
this.uris = uris;
}
但是你还必须让回收视图知道数据发生了变化。
为此,你需要修改函数 setUris
,添加 notifyDataSetChanged()
。
最终代码:
public void setUris(List<Modell> uris) {
this.uris = uris;
notifyDataSetChanged();
}
英文:
You are setting the adapter with
public void setUris(List<Modell> uris) {
this.uris = uris;
}
But you must also let the recycler view know that there is a change in the data.
For that you need to modify the function setUris
to include notifyDataSetChanged()
Final code:
public void setUris(List<Modell> uris) {
this.uris = uris;
notifyDataSetChanged();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论