在RecyclerView中出现了onActivityResult的问题。

huangapple go评论118阅读模式
英文:

issue onactivityresult in recyclerview

问题

以下是您提供的代码部分的翻译:

MainActivity(主活动):

  1. package com.example.myapuuuplication;
  2. import androidx.annotation.Nullable;
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import androidx.recyclerview.widget.LinearLayoutManager;
  5. import androidx.recyclerview.widget.RecyclerView;
  6. import android.content.Intent;
  7. import android.graphics.Color;
  8. import android.net.Uri;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import com.sangcomz.fishbun.FishBun;
  13. import com.sangcomz.fishbun.adapter.image.impl.GlideAdapter;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. public class MainActivity extends AppCompatActivity {
  17. Adapter adapter = new Adapter();
  18. List<Modell> uris = new ArrayList<>();
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. RecyclerView rv = findViewById(R.id.rv);
  24. rv.setAdapter(adapter);
  25. rv.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
  26. Button btn = findViewById(R.id.button);
  27. btn.setOnClickListener(new View.OnClickListener() {
  28. @Override
  29. public void onClick(View view) {
  30. openpicker();
  31. }
  32. });
  33. }
  34. private void openpicker() {
  35. FishBun.with(this)
  36. .setImageAdapter(new GlideAdapter())
  37. .setMaxCount(3)
  38. .setActionBarColor(Color.parseColor("#5D4037"))
  39. .startAlbum();
  40. }
  41. @Override
  42. public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  43. super.onActivityResult(requestCode, resultCode, data);
  44. switch (requestCode) {
  45. case FishBun.FISHBUN_REQUEST_CODE:
  46. if (resultCode == RESULT_OK) {
  47. uris = data.getParcelableArrayListExtra(FishBun.INTENT_PATH);
  48. adapter.setUris(uris);
  49. break;
  50. }
  51. }
  52. }
  53. }

Adapter(适配器):

  1. package com.example.myapuuuplication;
  2. import android.content.Intent;
  3. import android.net.Uri;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.ImageView;
  8. import androidx.annotation.NonNull;
  9. import androidx.recyclerview.widget.RecyclerView;
  10. import com.sangcomz.fishbun.FishBun;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. public class Adapter extends RecyclerView.Adapter<Adapter.VH> {
  14. List<Modell> uris = new ArrayList<>();
  15. public void setUris(List<Modell> uris) {
  16. this.uris = uris;
  17. }
  18. @NonNull
  19. @Override
  20. public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  21. View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.model, parent, false);
  22. return new VH(view);
  23. }
  24. @Override
  25. public void onBindViewHolder(@NonNull VH holder, int position) {
  26. Modell modell = uris.get(position);
  27. VH vh = holder;
  28. vh.img.setImageURI(modell.getImg());
  29. }
  30. @Override
  31. public int getItemCount() {
  32. return uris.size();
  33. }
  34. public class VH extends RecyclerView.ViewHolder {
  35. ImageView img;
  36. public VH(@NonNull View itemView) {
  37. super(itemView);
  38. img = itemView.findViewById(R.id.imgmodel);
  39. }
  40. }
  41. }

Modell(模型):

  1. package com.example.myapuuuplication;
  2. import android.net.Uri;
  3. import android.os.Parcel;
  4. import android.os.Parcelable;
  5. public class Modell implements Parcelable {
  6. Uri img;
  7. public Modell(Uri img) {
  8. this.img = img;
  9. }
  10. protected Modell(Parcel in) {
  11. img = in.readParcelable(Uri.class.getClassLoader());
  12. }
  13. public static final Creator<Modell> CREATOR = new Creator<Modell>() {
  14. @Override
  15. public Modell createFromParcel(Parcel in) {
  16. return new Modell(in);
  17. }
  18. @Override
  19. public Modell[] newArray(int size) {
  20. return new Modell[size];
  21. }
  22. };
  23. public Uri getImg() {
  24. return img;
  25. }
  26. public void setImg(Uri img) {
  27. this.img = img;
  28. }
  29. @Override
  30. public int describeContents() {
  31. return 0;
  32. }
  33. @Override
  34. public void writeToParcel(Parcel parcel, int i) {
  35. parcel.writeParcelable(img, i);
  36. }
  37. }

请注意,我已经按照您的要求只返回了翻译好的部分,没有包含其他内容。如果您有任何问题或需要进一步的帮助,请随时提问。

英文:

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:

  1. package com.example.myapuuuplication;
  2. import androidx.annotation.Nullable;
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import androidx.recyclerview.widget.LinearLayoutManager;
  5. import androidx.recyclerview.widget.RecyclerView;
  6. import android.content.Intent;
  7. import android.graphics.Color;
  8. import android.net.Uri;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.ImageView;
  13. import android.widget.Toast;
  14. import com.sangcomz.fishbun.FishBun;
  15. import com.sangcomz.fishbun.adapter.image.impl.GlideAdapter;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. public class MainActivity extends AppCompatActivity {
  19. Adapter adapter = new Adapter();
  20. List&lt;Modell&gt; uris = new ArrayList&lt;&gt;();
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25. RecyclerView rv = findViewById(R.id.rv);
  26. rv.setAdapter(adapter);
  27. rv.setLayoutManager(new LinearLayoutManager(this,RecyclerView.HORIZONTAL,false));
  28. Button btn = findViewById(R.id.button);
  29. btn
  30. .setOnClickListener(new View.OnClickListener() {
  31. @Override
  32. public void onClick(View view) {
  33. openpicker();
  34. }
  35. });
  36. }
  37. private void openpicker() {
  38. FishBun.with(this).setImageAdapter(new GlideAdapter())
  39. .setMaxCount(3)
  40. .setActionBarColor(Color.parseColor(&quot;#5D4037&quot;))
  41. .startAlbum();
  42. }
  43. @Override
  44. public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  45. super.onActivityResult(requestCode, resultCode, data);
  46. switch (requestCode) {
  47. case FishBun.FISHBUN_REQUEST_CODE:
  48. if (resultCode == RESULT_OK) {
  49. uris = data.getParcelableArrayListExtra(FishBun.INTENT_PATH);
  50. adapter.setUris(uris);
  51. break;
  52. }
  53. }
  54. }
  55. }

Adapter:

  1. package com.example.myapuuuplication;
  2. import android.content.Intent;
  3. import android.net.Uri;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.ImageView;
  8. import androidx.annotation.NonNull;
  9. import androidx.recyclerview.widget.RecyclerView;
  10. import com.sangcomz.fishbun.FishBun;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import static android.app.Activity.RESULT_OK;
  14. public class Adapter extends RecyclerView.Adapter&lt;Adapter.VH&gt; {
  15. List&lt;Modell&gt; uris = new ArrayList&lt;&gt;();
  16. public void setUris(List&lt;Modell&gt; uris) {
  17. this.uris = uris;
  18. }
  19. @NonNull
  20. @Override
  21. public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  22. View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.model,parent,false);
  23. return new VH(view);
  24. }
  25. @Override
  26. public void onBindViewHolder(@NonNull VH holder, int position) {
  27. Modell modell = uris.get(position);
  28. VH vh = holder;
  29. vh.img.setImageURI(modell.getImg());
  30. }
  31. @Override
  32. public int getItemCount() {
  33. return uris.size();
  34. }
  35. public class VH extends RecyclerView.ViewHolder{
  36. ImageView img;
  37. public VH(@NonNull View itemView) {
  38. super(itemView);
  39. img = itemView.findViewById(R.id.imgmodel);
  40. }
  41. }
  42. }

And Model:

  1. package com.example.myapuuuplication;
  2. import android.net.Uri;
  3. import android.os.Parcel;
  4. import android.os.Parcelable;
  5. public class Modell implements Parcelable {
  6. Uri img;
  7. public Modell(Uri img) {
  8. this.img = img;
  9. }
  10. protected Modell(Parcel in) {
  11. img = in.readParcelable(Uri.class.getClassLoader());
  12. }
  13. public static final Creator&lt;Modell&gt; CREATOR = new Creator&lt;Modell&gt;() {
  14. @Override
  15. public Modell createFromParcel(Parcel in) {
  16. return new Modell(in);
  17. }
  18. @Override
  19. public Modell[] newArray(int size) {
  20. return new Modell[size];
  21. }
  22. };
  23. public Uri getImg() {
  24. return img;
  25. }
  26. public void setImg(Uri img) {
  27. this.img = img;
  28. }
  29. @Override
  30. public int describeContents() {
  31. return 0;
  32. }
  33. @Override
  34. public void writeToParcel(Parcel parcel, int i) {
  35. parcel.writeParcelable(img, i);
  36. }
  37. }

答案1

得分: 1

你正在使用适配器设置

  1. public void setUris(List<Modell> uris) {
  2. this.uris = uris;
  3. }

但是你还必须让回收视图知道数据发生了变化。
为此,你需要修改函数 setUris,添加 notifyDataSetChanged()

最终代码:

  1. public void setUris(List<Modell> uris) {
  2. this.uris = uris;
  3. notifyDataSetChanged();
  4. }
英文:

You are setting the adapter with

  1. public void setUris(List&lt;Modell&gt; uris) {
  2. this.uris = uris;
  3. }

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:

  1. public void setUris(List&lt;Modell&gt; uris) {
  2. this.uris = uris;
  3. notifyDataSetChanged();
  4. }

huangapple
  • 本文由 发表于 2020年8月19日 17:55:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63484445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定