To show only the chosen item in the ListView for Android, you can perform the following steps:
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.
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
Asked: 2022-07-22 11:00:00 +0000
Seen: 8 times
Last updated: Oct 18 '21
How can Django Admin accommodate a variety of formats and locales for its input fields?
How can an array be passed using typo3 flexform xml and itemsProcConfig?
Is it possible to invoke an asynchronous function without using the await keyword?
How can metadata be linked to a series in Polars?
What is the process of utilizing the map function to map a pandas column using a dictionary?
How can the orientation of images be corrected during uploading using Plupload?