简体   繁体   中英

android Recycler View not showing Items but they are there

I am making a practice app for my self creating friends profile my self in my app and having their avatar as imageview and name,nickname as text view it accepts object but it never shows anything on recyclerview Please Help!

My main activity.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));

            }
        });
    }
}

my FriendAdder activity

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());
        }
    }
}

my adapter class

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);
    }
}

}

A helper Class I made with static Array list of Friend object acting as A runtime temporary database to add and remove Friend used in adapter class to display

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);
    }

}

Where the RecyclerView is handled adapterName.notifyDataSetChanged (); you need to run. In your case, the data is collected statically. The place where the data is processed is a different Activity. After switching to the new screen and adding data, you will need to update your adapter in onResume if you want the relevant data to appear when you come back. Recyclerview will appear blank since there is no data in its initial state.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM