简体   繁体   中英

Android, Can I get list data to persist when using a recyclerview

I am trying to use a RecyclerView to display a list of data in .MainActivity. When I used ListView with an adapter, I could access the source data ArrayList using the position clicked in order to pass the list element to an edit activity. Using a RecyclerView, the ArrayList to populate the RecyclerView is Null when the onListItemClicked attempts to access it to get the list element and add it to the Intent. I assume this is because the mechanism to handle the click is done outside of the MainActivity class, unlike it was with ListView, and so the variable no longer exists once onCreate is finished, but I'm not sure.

I don't want to read the list data from the views (do I?). Unless there is a way to make this list persistent the only thing I can think of is to either have the main activity read a file to build the list and then read it again to get the list element to pass to the edit activity, or to have the main activity pass the element position/index and have the edit activity access the list file to retrieve the element. But both of these seem rather file access intensive and would require the edit activity to manage the list as well as the main activity.

public class MainActivity extends AppCompatActivity
        implements PlanAdapter.ListItemClickListener
{
    // View elements
    private RecyclerView planDisplay;

    // Adapter for building the RecyclerView
    private PlanAdapter mListAdapter;

    // Layout manager for the RecyclerView
    RecyclerView.LayoutManager layoutManager;

    // Plan list
    private static ArrayList<PlanElement> thisPlan;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        planDisplay = findViewById(R.id.rv_plan_display);

        ArrayList<PlanElement> thisPlan = buildThisPlan();

        layoutManager =
                new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        planDisplay.setLayoutManager(layoutManager);
        planDisplay.setHasFixedSize(true);

        mListAdapter = new PlanAdapter(thisPlan, this);
        planDisplay.setAdapter(mListAdapter);

    }

    @Override
    public void onListItemClick(int clickedItemIndex)
    {
        Intent i = new Intent(MainActivity.this, EditPlanItemActivity.class);

        i.putExtra("elementNumber", clickedItemIndex + 1);

// Where the error occurs
        i.putExtra("PlanElement", thisPlan.get(clickedItemIndex));

        startActivity(i);
    }

}

public class PlanAdapter extends RecyclerView.Adapter<PlanAdapter.AdapterViewHolder>
{
    private ArrayList<PlanElement> thisPlan;

    final private ListItemClickListener mOnClickListener;

    // Click Listener Interface
    public interface ListItemClickListener
    {
        void onListItemClick(int clickedItemIndex);
    }

    public PlanAdapter(ArrayList<PlanElement> aPlan, ListItemClickListener listener)
    {
        thisPlan = aPlan;
        mOnClickListener = listener;
    }

    @NonNull
    @Override
    public PlanAdapter.AdapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
    {
        Context context = parent.getContext();
        int layoutListItem = R.layout.list_element;

        LayoutInflater inflater = LayoutInflater.from(context);

        View view = inflater.inflate(layoutListItem, parent, false);

        return new AdapterViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AdapterViewHolder holder, int position)
    {
        PlanElement thisElement = thisPlan.get(position);


        holder.textItem1Display.setText(String.format("%d", thisElement.getItem1()));
    holder.textItem2Display.setText(String.format("%d", thisElement.getItem2()));

    }

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

    public class AdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
    {
        public TextView textItem1Display;
        public TextView textItem2Display;

        public AdapterViewHolder(@NonNull View itemView)
        {
            super(itemView);

            textItem1Display = itemView.findViewById(R.id.textItem1);
            textItem2Display = itemView.findViewById(R.id.textItem2);

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view)
        {
            int clickedPosition = getAdapterPosition();
            mOnClickListener.onListItemClick(clickedPosition);
        }
    }
}

Ok, added my adapter. But the adapter works fine. The problem occurs when the the onClick method tries to access a null ArrayList.

Looks like you've instantiated a new thisPlan list in your MainActivity. In your OnCreate method, instead of creating a new ArrayList, use the one you've already created.

Instead of this:

protected void onCreate(Bundle savedInstanceState)
    {
        ...
        ArrayList<PlanElement> thisPlan = buildThisPlan(); //created a new instace
        ...
    }

Do this:

protected void onCreate(Bundle savedInstanceState)
    {
        ...
        thisPlan = buildThisPlan(); // use the one you already created
        ...
    }

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