RecyclerView handles onClick from activity rather than from adapter

Asked 2 years ago, Updated 2 years ago, 42 views

RecyclerView was used to display a list of information.
After displaying the list, I would like to erase the data on the external storage by pressing the erase button, and update the data on the list to display it.
At this time, MainActivity must process the erase button.
How should I do it?

MainActivity.java

public class MainActivity extensions AppCompatActivity{

    Activity thisActivity = this;

    List<HashMap<String, String>>worldDatas=new ArrayList<>();;

    String sdPath=getExternalStoragePath();
    final String WORLD_NAME_KEY="name";
    final String WORLD_PATH_KEY="path";
    final String WORLD_SIZE_KEY="size";

    RecyclerView worldInfoContainer;

    @ Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);
        Toolbar toolbar= (Toolbar) findViewById (R.id.toolbar);
        setSupportActionBar (toolbar);

        updateWorldsList();
    }

// Update list data from terminal folder
    private void updateWorldsList(){
        worldInfoContainer= (RecyclerView) findViewById (R.id.worldInfoContainer);
        worldDatas=new ArrayList<>();
        //...

        worldInfoContainer.setLayoutManager (new LinearLayoutManager (thisActivity));
        worldInfoContainer.setHasFixedSize(true);

        worldInfoContainer.setAdapter (new WorldDataAdapter (thisActivity, worldDatas));
    }
// Clear Directory
    public void deleteDirectory (File file) {
        //...
    }

    public static String getExternalStoragePath(){
        //...
    }
}

WorldDataAdapter.java

public class WorldDataAdapter extensions RecyclerView.Adapter<WorldDataAdapter.ViewHolder>{
    private LayoutInflater mLayoutInflater;

    List<HashMap<String, String>>worldDatas=new ArrayList<>();;

    final String WORLD_NAME_KEY="name";
    final String WORLD_PATH_KEY="path";
    final String WORLD_SIZE_KEY="size";

    public WorldDataAdapter (Context context, List <HashMap<String, String > > dataList) {
        super();
        mLayoutInflater=LayoutInflater.from(context);
        worldData=dataList;
    }

    @ Override
    publicWorldDataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, intviewType){
        View v=mLayoutInflater.inflate(R.layout.template_world_info, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);

        return viewHolder;
    }

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

    @ Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        final HashMap<String, String>positionData=worldData.get(position);

        holder.worldNameText.setText(positionData.get(WORLD_NAME_KEY)));
        holder.worldPathText.setText(positionData.get(WORLD_PATH_KEY)));
        holder.worldSizeText.setText(positionData.get(WORLD_SIZE_KEY)));

        holder.deleteButton.setOnClickListener(newView.OnClickListener(){
            @ Override
            public void onClick (View view) {
                // I want to do it with MainActivity
            }
        });
    }

    static classViewHolder extensionsRecyclerView.ViewHolder{
        TextView worldNameText;
        TextView worldPathText;
        TextView worldSizeText;

        ImageButton deleteButton;
        public ViewHolder (final View v) {
            super(v);

            worldNameText=(TextView) v.findViewById (R.id.worldNameText);
            worldPathText=(TextView) v.findViewById (R.id.worldPathText);
            worldSizeText=(TextView) v.findViewById (R.id.worldSizeText);

            deleteButton=(ImageButton) v.findViewById (R.id.deleteButton);
        }
    }
}

android java

2022-09-30 16:19

1 Answers

I put the class definition into MainActivity and it worked.


2022-09-30 16:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.