There is a ListView in my App and the ListView has a selector. I want to make the first item of this ListView to be selected as default at the very startup of App, How? Can anyone give some tips? THX a lot.
You can do this by
yourListView.setSelection(0);
yourListView.getSelectedView().setSelected(true);
I hope this will help you
You can do this by
yourListView.setSelection(0);
yourListView.getSelectedView().setSelected(true);
I hope this will help you
This work for me. I hope this will help you. :)
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// do someting...
if(position == 0){
mListView.performItemClick(convertView, 0, 0);
}
}
now i found another good way to implement this.
- Firstly,you should set ListView android:choiceMode=singleChoice
Secondly,just go ahead as follow,
int defaultPositon = 0; int justIgnoreId = 0; mListView.setItemChecked(defaultPositon, true); mListView.performItemClick(mListView.getSelectedView(), defaultPositon, justIgnoreId);
//put the below code in get view function
if(position==0)
{
convertView.setSelected(true);
}
I think this will work as we have given 0 index of array to listView
ListView.setSelection(0);
Create an onItemClickListener
then use this snippet, filling in the appropriate values:
listView.performItemClick(View view, int position, long id);
See the docs for further detail.
You can call listview.setSelection(0);
. Just make sure you are calling this after poplulating your List
. If you call it before populating your List
it will not work, because it won't have any data at that time.
Below solution works for me:
Setting background/View Id on get View & using the setOnItemClickListener of the List View
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)
ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listitems, null);
}
ListItem m = items.get(position);
if (m != null) {
TextView txt = (TextView)v.findViewById(R.id.textView);
txt.setText(m.Item);
}
// set selected item
LinearLayout ActiveItem = (LinearLayout) v;
v.setId(position);
if (position == GetDeviceDetails.selectedsize)
{
ActiveItem.setBackgroundColor(0xFF7F8184);
}
return v;
}
In the Activity on Create :
listView1.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
adapter.findViewById(GetDeviceDetails.selectedsize).setBackgroundColor(0xFFFFFFF);
GetDeviceDetails.selectedsize = position;
adapter.findViewById(position).setBackgroundColor(0xFF7F8184);
Log.d("Selected Id", "" + v.getId());
Log.d("find Selected Id", "" + adapter.findViewById(0));
}
});
listView1.setSelection(0);
listView1.setItemChecked(0, true);
This works if you want to click it to get additional event handling done:
listView.getAdapter().getView(position, null, null).performClick();
You can fix it like this.
//set list adapter, then
lv.setItemChecked(1, true);//position 1
lv.setSelection(1);//position 1
Update XML layout with single choice.
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/list_item_background"
android:cacheColorHint="#FFFFFF"
android:choiceMode="singleChoice"
android:dividerHeight="1dip" >
</ListView>
and set listview background XML as list_item_background.xml,
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/bg_list_default" android:state_activated="false"/>
<item android:drawable="@drawable/bg_list_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/bg_list_pressed" android:state_activated="true"/>
THIS WORKS FOR ME.You have to make custom adapter.
First Create Modal Class
public class FilterBean {
String filter_catagory;
boolean isSelected;
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
public String getFilter_catagory() {
return filter_catagory;
}
public void setFilter_catagory(String filter_catagory) {
this.filter_catagory = filter_catagory;
}
}
In Adapter Class
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = layoutInflater.inflate(R.layout.size_name_indicator, null);
txt = (TextView) convertView.findViewById(R.id.key_textView);
txt.setText(getItem(position).getFilter_catagory());
if(getItem(position).isSelected()){
txt.setBackgroundColor(context.getResources().getColor(R.color.transparent));
}else {
txt.setBackgroundColor(context.getResources().getColor(R.color.colorWhite));
}
return convertView;
}
public void updateNameBean(String key,boolean checked)
{
for(int i=0;i<getCount();i++)
{
FilterBean filterBean = getItem(i);
if(filterBean.getFilter_catagory().equals(key))
{
filterBean.setSelected(checked);
}else {
filterBean.setSelected(false);
}
}
notifyDataSetChanged();
}
In Filter Activity or Fragment
filter_listView.setAdapter(filterNameAdapter);
filterNameAdapter.updateNameBean(filterNameAdapter.getItem(0).getFilter_catagory(),true);
filter_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String key = filterNameAdapter.getItem(position).getFilter_catagory();
filterNameAdapter.updateNameBean(filterNameAdapter.getItem(position).getFilter_catagory(),true);
}
});
I have used data binding to solve this,
using the following codebehind:
private List<Unit> _units;
private Unit _selectedUnit;
public Unit selectedUnit{
get{
if (_selectedUnit is null){
return _units.Count > 0 ? _units[0] : null;
}else{
return _selectedUnit;
}
}
set{
if (_selectedUnit != value){
_selectedUnit = value;
OnPropertyChanged("selectedUnit");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName){
var changed = PropertyChanged;
if (changed != null){
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
and XAML
<ListView ItemsSource="{Binding units}"
SelectedItem="{Binding selectedUnit, Mode=TwoWay}">
if(position == 0){
// This is the first item, you need to select this
//rowview which u inflate under getview
rowView.performClick();
}
i think this will help you . it works for me
© 2022 - 2024 — McMap. All rights reserved.