I have 2 listview. I want when the activity starts first row of the both the listview to be selected by default.I googled up i found this
onewaydata=new One
Try this, it should do the trick:
your_list_view.post(new Runnable() {
@Override
public void run() {
your_list_view.setSelection(0);
}
});
UPDATE: Please take a look at this
class YourAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
View view = convertView;
if(position == 0){
// This is the first item, you need to select this
view.setSelected(true);
}
// Do whatever you want here
return view;
}
}
In XML:
<ListView
android:id="@+id/lvDepartures"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:listSelector="@null" <!--Here is the thing you need-->
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<View
android:layout_width="1dp"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray" />
<ListView
android:id="@+id/lvArrivals"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:listSelector="@null" <!--Here is the thing you need-->
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
In your item-row-xml:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/item_row_selector"> <!--Here is the thing you need-->
<!--Your item layout here-->
</LinearLayout>
And last you need to implement your own item_row_select with the state of "selected".
Hope this helps.
**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 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);
}
});
instead of your_list_view.setSelection(0);
try your_list_view.setItemChecked(0,true);