RecyclerView适配器列表<e>获得0

时间:2018-05-18 14:42:31

标签: android android-recyclerview drag-and-drop

我是android开发的新手。我遇到了问题,我创建了一个简单的应用程序,其中我在MainActivity中有RecyclerView。我尝试拖放位置。但是,当我从MainActivity移动到CompleteList_activity并再次来到MainActivity并尝试拖放时,我得到了ArrayOutOfBound错误,因为List值得到0 in  public void positionchange(int fromPosition,int toPosition)method。

MainActivity:

package com.example.marmeto.todolist;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    DBHelper db;

    ImageButton btn_addtodolist,btn_completelist;
    List<Model> todolist = new ArrayList<>();
    List<Model> adapterList = new ArrayList<>();
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private ToDoList_Adapter adapter;
    public static final int NEW_WORD_ACTIVITY_REQUEST_CODE = 1;
    Model mToDOItemList = new Model();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        db = new DBHelper(this);

        recyclerView = (RecyclerView)findViewById(R.id.recyclerview);
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(layoutManager);

        btn_addtodolist = (ImageButton) findViewById(R.id.btn_addtodolist);
        btn_completelist = (ImageButton)findViewById(R.id.btn_completedlist);
        btn_addtodolist.setOnClickListener(this);

        btn_completelist.setOnClickListener(this);

        getValue();
    }
    void getValue(){
        todolist.clear();

        todolist = db.getUserDetail();

        Collections.sort(todolist, new Comparator<Model>() {
            @Override public int compare(Model p1, Model p2) {
                return p1.getPosition()- p2.getPosition(); // Ascending
            }
        });
        adapter = new ToDoList_Adapter(todolist,getApplicationContext());

        ItemTouchHelper.SimpleCallback callback=new SwipItem(adapter);
        ItemTouchHelper helper=new ItemTouchHelper(callback);
        helper.attachToRecyclerView(recyclerView);

        recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }



    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onClick(View v) {


        if (v.getId() == R.id.btn_addtodolist) {
            Intent intent = new Intent(getApplicationContext(), AddToDoList_Activity.class);
            startActivity(intent);

        } else {
            Intent intent = new Intent(getApplicationContext(),CompletedActivity.class);
            startActivity(intent);

        }
    }
    }

AdapterClass:

    package com.example.marmeto.todolist;
    import android.content.Context;
    import android.support.annotation.NonNull;
    import android.support.v7.widget.RecyclerView;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
    import java.util.Collections;import java.util.List;

       public class ToDoList_Adapter extends             RecyclerView.Adapter<ToDoList_Adapter.ViewHolder> {

    List<Model> items;
    private Context context;

    public ToDoList_Adapter(List<Model> items, Context context) {
        this.items = items;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.todolist_adapter, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

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

        final Model itemlist = items.get(position);
        holder.todoText.setText(itemlist.getPosition()+" "+itemlist.getTitle());
    }


    @Override
    public int getItemCount() {
        return items.size();
    }
    public void positionchange(int fromPosition, int toPosition)
    {

        try {
        Collections.swap(items,fromPosition,toPosition);

        DBHelper db = new DBHelper(context);
        db.updateList(items);
         }catch (Exception e)
         {
             e.printStackTrace();
         }
        this.notifyItemMoved(fromPosition,toPosition);


    }

    public void deletePlanet(int pos)
    {
        //GET ID
        try {
            Model p = items.get(pos);
            int id = p.getId();

            //DELETE FROM DB
            DBHelper db = new DBHelper(context);

            if (db.delete(id)) {
                items.remove(pos);
                db.updateList(items);

            } else {
                Toast.makeText(context, "Unable To Delete", Toast.LENGTH_SHORT).show();
            }
            this.notifyItemRemoved(pos);
        }catch (Exception e){
            e.printStackTrace();
        }




    }
    class ViewHolder extends RecyclerView.ViewHolder {

        public TextView todoText;
        public Button btn_done;
        public ViewHolder(final View itemView) {
            super(itemView);

            todoText = (TextView)itemView.findViewById(R.id.todoItem_text);
            btn_done = (Button)itemView.findViewById(R.id.btn_done);
            btn_done.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    DBHelper db = new DBHelper(context);
                    try {
                        Model p = items.get(getAdapterPosition());
                        db.updateData(p.getId());
                        items.remove(getAdapterPosition());

                        db.updateList(items);
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                    notifyItemRemoved(getAdapterPosition());
                }
            });
        }
    }
}

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您致电getValue(),然后将数据填入onCreate。但是当你传递给一个新的Activity然后你回到之前的活动时onCreate方法不再被调用。

尝试将getValue()调用到onResume方法

看那里Activity lifecycle