Friday, 12 August 2011

android: removing listview selector

first this problem solution achieve we can create custom listview...

suppose your listview xml file like...

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
         
        <ListView
        android:layout_height="wrap_content"
        android:id="@android:id/list"
        android:layout_width="match_parent"></ListView>
    </LinearLayout>

and custom view xml file...


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/black"
        >
            <TextView
            android:text="TextView"
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:textSize="30dip">
            </TextView>
   
    </LinearLayout>


activity class...

public class ListHeaderActivity extends ListActivity {

    ListView lstVw;
    MyArrayAdapter myArrayAdapter = null;
    ArrayList<String> arrList = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        arrList.add("android");
        arrList.add("iphone");
        arrList.add("rim");
        lstVw = getListView();
        myArrayAdapter = new MyArrayAdapter(ListHeaderActivity.this,
                R.layout.header, arrList);
        lstVw.setAdapter(myArrayAdapter);

    }

}

adapter class....

public class MyArrayAdapter extends ArrayAdapter<String> {

    ViewHolder holder;
    private int rawLayout;

    public MyArrayAdapter(Context context, int resourceId,
            ArrayList<String> objects) {
        super(context, resourceId, objects);
        rawLayout = resourceId;
    }

    @Override
    public int getCount() {
        return super.getCount();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(rawLayout, parent, false);
            holder = new ViewHolder();
            holder.title1 = (TextView) convertView.findViewById(R.id.textView1);

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.title1.setText("your text item");

        return convertView;
    }

    private class ViewHolder {
        TextView title1;
    }
}


No comments:

Post a Comment