Ask Your Question
4

How can I show only the chosen item in the listview for Android?

asked 2022-07-22 11:00:00 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-10-18 15:00:00 +0000

djk gravatar image

To show only the chosen item in the ListView for Android, you can perform the following steps:

  1. Add an OnItemClickListener to the ListView.
  2. In the OnItemClickListener, store the selected item in a variable.
  3. Create a custom adapter that extends BaseAdapter and override the getView() method.
  4. In the getView() method, check if the current position is equal to the selected position.
  5. If the current position is equal to the selected position, set the item as visible and hide all other items. Otherwise, hide the item.

Here is some sample code to achieve this:

public class MyListActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    ListView listView;
    MyAdapter adapter;
    int selectedPosition = -1;

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

        listView = findViewById(R.id.listview);
        adapter = new MyAdapter(this);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        selectedPosition = position;
        adapter.notifyDataSetChanged();
    }

    private class MyAdapter extends BaseAdapter {

        private Context context;

        public MyAdapter(Context context) {
            this.context = context;
        }

        @Override
        public int getCount() {
            return 10;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View itemView = getLayoutInflater().inflate(R.layout.list_item, parent, false);

            if (position == selectedPosition) {
                itemView.setVisibility(View.VISIBLE);
            } else {
                itemView.setVisibility(View.GONE);
            }

            return itemView;
        }
    }
}

In this example, we have a listview with 10 items and a custom adapter that inflates a list item layout. In the onItemClick listener, we store the selected position in the selectedPosition variable and call adapter.notifyDataSetChanged() to update the listview. In the adapter's getView() method, we check if the current position is equal to the selected position and show or hide the item accordingly.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-07-22 11:00:00 +0000

Seen: 8 times

Last updated: Oct 18 '21