I have a question for Android listview.

Asked 2 years ago, Updated 2 years ago, 91 views

ListView listView = findViewById(R.id.listview);
    SingerAdapter adapter = new SingerAdapter();

    adapter.addItem(new SingerItem("1","2"));
    adapter.addItem(new SingerItem("1","2"));
    adapter.addItem(new SingerItem("1","2"));

    listView.setAdapter(adapter);

    Button button = findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), secondActivity.class);
            ArrayList<SingerItem> items = new ArrayList<SingerItem>();
            intent.putExtra("items", items);
            startActivity(intent);
        }
    })

I'm asking you this question because I don't know what to do with the adapter on the receiving side after sending it from the main activity.

public class secondAdapter extends BaseAdapter {
    ArrayList<SingerItem> items = (ArrayList<SingerItem>) intent.getSerializableExtra("items");

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SingerItemView view = new SingerItemView(getApplicationContext());

        SingerItem item = items.get(position);
        view.setTitle(item.getTitle());
        view.setMainText(item.getMaintext());

        return view;
    }
}

android intent

2022-09-21 17:43

1 Answers

I will explain after receiving the list through int.getSerializable() from secondActivity.

Configure ListView

final SingerAdapter adapter = new SingerAdapter(dataSet);
final ListView listView = findViewById(R.id.listView);
listView.setAdapter(adapter);

Create an Adapter object and invoke setAdapter() in ListView.

Adapter Configuration

static class SingerAdapter extensions BaseAdapter {

        final List<SingerItem> dataSet;

        SingerAdapter(List<SingerItem> dataSet) {
            this.dataSet = dataSet;
        }

        @Override
        public int getCount() {
            return dataSet.size();
        }

        @Override
        public SingerItem getItem(int position) {
            return dataSet.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, null);
                holder = new ViewHolder();
                holder.title = convertView.findViewById(R.id.title);
                holder.mainText = convertView.findViewById(R.id.mainText);
                convertView.setTag(holder);
            } } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.title.setText(getItem(position).getTitle());
            holder.mainText.setText(getItem(position).getMainText());
            return convertView;
        }

        static class ViewHolder {
            TextView title;
            TextView mainText;
        }
}

In the SingerAdapter constructor, receive the dataset to be bound to ListView as a parameter. An important part of ListView is getView(), and the view that constitutes the row of ListView is always newly created and implemented using a holder pattern for recycling instead of being used. Creating a view every time getView() is called not only affects memory, but also findViewById() is not good for performance.

You can use ListView as above. It is better to use RecyclerView, which is an improved version than ListView.


2022-09-21 17:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.