Reputation: 140
I am new to android and my an using list view, I want to change list item color on selection but it is not working.
I have check almost all links on stack overflow and tried them all but no luck... Please check my code ... and tell me where is the problem.
public class Main extends Activity {
private ListView listView;
private String[] menuArray;
private List<String> menuList = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
menuArray = getResources().getStringArray(R.array.Menu);
menuList = Arrays.asList(menuArray);
listView = (ListView) findViewById(R.id.listView_main_menu);
listView.setAdapter(new EfficientAdapter(this,menuList));
}
/******************************** EfficientAdapter ************************************/
public class EfficientAdapter extends BaseAdapter{
private LayoutInflater mInflater;
@SuppressWarnings("unused")
private String TAG=EfficientAdapter.class.getSimpleName();
@SuppressWarnings("unused")
private Context context;
private List<String> data= new ArrayList<String>();
public EfficientAdapter(Context context,List<String> data) {
super();
mInflater = LayoutInflater.from(context);
this.context=context;
this.data.addAll(data);
}
public EfficientAdapter(Context context) {
super();
mInflater = LayoutInflater.from(context);
this.context=context;
}
public View getView(final int position, View convertView, final ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = mInflater.inflate(R.layout.main_list, null);
holder = new ViewHolder();
holder.textLine = (TextView) convertView.findViewById(R.id.textView_mainlist_item);
convertView.setTag(holder);
}
holder =(ViewHolder) convertView.getTag();
holder.textLine.setText(getItem(position));
convertView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
return convertView;
}
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
public String getItem(int position) {
// TODO Auto-generated method stub
return data.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
class ViewHolder {
TextView textLine;
}
}
}
Here is my xml file with list view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/Gainsboro"
android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:id="@+id/listView_main_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:choiceMode="singleChoice"
android:divider="@color/DarkGray"
android:dividerHeight="1dip"
android:fadingEdge="none"
android:listSelector="@drawable/row_color"
android:scrollbars="none" >
</ListView>
</RelativeLayout>
row_color.xml
<item android:state_pressed="false" android:state_focused="false"
android:drawable="@color/Green" />
<item android:state_focused="true"
android:drawable="@color/Blue" />
<item android:state_pressed="true"
android:drawable="@color/Brown" />
<item
android:state_selected="true" android:drawable="@color/Cyan" />
main_list.xml; this xml contain textview which is inflating
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView_mainlist_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:text="TextView"
android:textColor="@color/DarkSlateGray"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/TextView01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right|center"
android:paddingRight="30dp"
android:paddingTop="6dp"
android:text=">"
android:textColor="@color/DarkSlateGray"
android:textSize="28sp"
android:textStyle="bold" />
</FrameLayout>
Just found that only this portion is working fine
<item android:state_pressed="false" android:state_focused="false"
android:drawable="@color/Green" />
Upvotes: 0
Views: 218
Reputation: 10840
Instead of
convertView = mInflater.inflate(R.layout.main_list, null);
Try
convertView = mInflater.inflate(R.layout.main_list, parent, false);
EDIT:
I see what you are trying to do now. You don't want to have android:ListSelector
on ListView, but rather on each individual FrameLayout
. So:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:listSelector="@drawable/row_color" >
You would need to apply it to each individual view in order to have them change color when pressed, etc.
Upvotes: 1
Reputation: 5258
Visit http://android-codes-examples.blogspot.in/2011/03/customized-listview-items-selection.html
Hope it works for you
Upvotes: 0