android回收器视图不显示项目,但它们在那里

qlckcl4x  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(287)

我正在为我的自创好友制作一个练习应用程序,在我的应用程序中配置我的个人资料,并将他们的头像设置为imageview和name,昵称设置为text view它接受object,但从不在recyclerview上显示任何内容请帮助!
我的主要活动.java

package rex.MyFriends;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.OnItemTouchListener;

import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

public class MainActivity extends AppCompatActivity {
    FloatingActionButton btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerView view = findViewById(R.id.elementsview);
        btn = (FloatingActionButton) findViewById(R.id.newfbtn);
        view.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        view.setAdapter(new Friendsadapter(this));
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getBaseContext(),FriendsAdder.class));

            }
        });
    }
}

我的friendadder活动

package rex.MyFriends;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class FriendsAdder extends AppCompatActivity {
    TextView name,nickname;
    Button add ,selectimage;
    ImageView image;
    String imgpath;
    private static final int SELECT_IMAGE = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friendsadder);
        name = findViewById(R.id.name);
        nickname = findViewById(R.id.nickname);
        add = findViewById(R.id.add);
        selectimage = findViewById(R.id.selimg);
        image = findViewById(R.id.disimg);

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(name.toString().isEmpty() || nickname.toString().isEmpty() || image.getDrawable()==null){
                    Toast.makeText(getBaseContext(),"please fill every thing!",Toast.LENGTH_SHORT).show();
                }
                else{
                    FriendList.addFriend(name.toString(),nickname.toString(),imgpath);
                    finish();
                    Toast.makeText(getBaseContext(),"Friend Added!",Toast.LENGTH_SHORT).show();
                }
            }
        });

        selectimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,"Select Your Friends Avatar!"), SELECT_IMAGE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==SELECT_IMAGE && resultCode== Activity.RESULT_OK){
            imgpath= data.getData().getPath();
            Toast.makeText(getBaseContext(),imgpath,Toast.LENGTH_LONG).show();
            image.setImageURI(data.getData());
        }
    }
}

我的适配器类

package rex.MyFriends;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.io.File;

public class Friendsadapter extends RecyclerView.Adapter<Friendsadapter.FriendsHolder> {
    Context context;

    public Friendsadapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public FriendsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.friendrow,null,false);
        return new FriendsHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull FriendsHolder holder, int position) {
        FriendList.Friend friend = (FriendList.Friend) FriendList.friendlist.get(position);
        holder.nickname.setText(friend.nickname);
        holder.name.setText(friend.name);
        File imgFile = new  File(friend.imgpath);

        if(imgFile.exists()){
            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
            holder.image.setImageBitmap(myBitmap);
        }
    }

    @Override
public int getItemCount() {
    return FriendList.friendlist.size();
}

public class FriendsHolder extends RecyclerView.ViewHolder {
    TextView name,nickname;
    ImageView image;
    public FriendsHolder(@NonNull View itemView) {
        super(itemView);
        name = itemView.findViewById(R.id.name);
        nickname = itemView.findViewById(R.id.nickname);
        image = itemView.findViewById(R.id.imageView2);
    }
}

}
我用friend对象的静态数组列表作为运行时临时数据库来添加和删除适配器类中要显示的friend的助手类

package rex.MyFriends;

import java.util.ArrayList;

public class FriendList {
    public static ArrayList<Friend> friendlist = new ArrayList<>();
    public static class Friend {
        String name;
        String nickname;
        String imgpath ;

        public Friend(String name, String nickname, String imgpath) {
            this.name = name;
            this.nickname = nickname;
            this.imgpath = imgpath;
        }
    }

    public static void addFriend(String name, String nickname, String imgpath){
        Friend friend = new Friend(name,nickname,imgpath);
        friendlist.add(friend);
    }

    public int getSize(){
        return friendlist.size();
    }

    public Friend getFriend(int position){
        return friendlist.get(position);
    }

}
eyh26e7m

eyh26e7m1#

处理recyclerview的位置 adapterName.notifyDataSetChanged (); 你得跑了。在您的例子中,数据是静态收集的。处理数据的地方是不同的活动。切换到新屏幕并添加数据后,如果希望返回时显示相关数据,则需要在onresume中更新适配器。recyclerview将显示为空,因为没有处于初始状态的数据。

@Override
public void onResume() {
    super.onResume();
    adapter.notifyDataSetChanged();
}

相关问题