I am trying to populate a listview in which the rows contain a checkbox, four textviews, and two spinners. The values in the spinners on each row need to be different as they directly relate to the actual item on that row. Just to make it that little bit more complicated the spinners and the two text fields beside them are not visible (setVisibility(View.GONE)) unless the checkbox is checked. I just want the list item click-able (not the checkbox), and the spinners need to be click-able once they are shown.
All of my code is working correctly except that when the spinners are populated then I lose the ability to click the list item on that row. I can still choose values from the spinners on that row but touching anywhere else on the row does nothing at all. There are some rows that don't return data to the spinners and they continue to operate correctly so I believe it is something to do with the adaptors connected to the spinners.
Also I know I will have to account for the list view recycling but I haven't got that far yet.
I hope someone can point out what is wrong here before I go completely insane.
Here is the listview xml
<ListView android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TextView android:id="#+id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/empty"/>
</LinearLayout>
And here is the row xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<CheckBox android:id="#+id/pl_selected"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:layout_weight="1"/>
<TextView android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"/>
<TextView android:id="#+id/name2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/pl_tv1"
android:text="#string/pl_tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:visibility="gone"/>
<Spinner android:id="#+id/pl_spin1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="gone"
android:focusableInTouchMode="false"
android:focusable="false"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/pl_tv2"
android:text="#string/pl_tv2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:visibility="gone"/>
<Spinner android:id="#+id/pl_spin2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="gone"
android:focusableInTouchMode="false"
android:focusable="false"/>
</LinearLayout>
</LinearLayout>
And here is the activity with most of the irrelevant stuff taken out:
import android.widget.SimpleCursorAdapter;
import mdhsoft.band.Tab.DbAdapter;
import mdhsoft.band.Tab.R;
import android.os.Bundle;
import android.app.ListActivity;
import android.database.Cursor;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
public class PLActivity extends ListActivity {
private static final int DONE_ID = Menu.FIRST;
private DbAdapter mDbHelper;
private int mPlId;
private CheckBox mCheckBox;
private Spinner mSpin1;
private Spinner mSpin2;
private TextView mtv1;
private TextView mtv2;
private int mItemId;
private SimpleCursorAdapter mAllItems;
private Cursor mSCursor;
private Cursor mcurSpin1;
private Cursor mcurSpin2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pl_list);
Bundle extras = getIntent().getExtras();
mPlId = extras.getInt(DbAdapter.KEY_PLID);
mDbHelper = new DbAdapter(this);
mDbHelper.open();
fillData();
registerForContextMenu(getListView());
}
private void fillData() {
mSCursor = mDbHelper.fetchAllPl(mPlId);
startManagingCursor(mSCursor);
String[] from = new String[]
{"pl_selected", DbAdapter.KEY_SNAME, DbAdapter.KEY_SNAME2};
int[] to = new int[]{R.id.pl_selected, R.id.name, R.id.name2};
mAllItems = new SimpleCursorAdapter(this, R.layout.pl_row, mSCursor, from, to);
mAllItems.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if(columnIndex == 3) {
CheckBox cb = (CheckBox) view;
cb.setChecked(cursor.getInt(3) > 0);
ViewGroup row = (ViewGroup)view.getParent();
if (cursor.getInt(3) > 0) {
mcurSpin1 = (Spinner) row.findViewById(R.id.pl_spin1);
mcurSpin2 = (Spinner) row.findViewById(R.id.pl_spin2);
mtv1 = (TextView) row.findViewById(R.id.pl_tv1);
mtv2 = (TextView) row.findViewById(R.id.pl_tv2);
mcurSpin1.setVisibility(View.VISIBLE);
mtv1.setVisibility(View.VISIBLE);
mcurSpin2.setVisibility(View.VISIBLE);
mtv2.setVisibility(View.VISIBLE);
PopulateSpinner1();
PopulateSpinner2();
}
return true;
}
return false;
}
});
setListAdapter(mAllItems);
}
private void PopulateSpinner1(){
mcurSpin1 = mDbHelper.fetchSByItemId(mItemId);
startManagingCursor(mcurSpin1);
String[] from = new String[]{DbAdapter.KEY_SNAME};
int[] to = new int[]{android.R.id.text1};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item, mcurSpin1, from, to );
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
mSpin1.setAdapter(adapter);
}
private void PopulateSpinner2(){
mcurSpin2 = mDbHelper.fetchMByItemId(mItemId);
startManagingCursor(mcurSpin2);
String[] from = new String[]{DbAdapter.KEY_MNAME};
int[] to = new int[]{android.R.id.text1};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item, mcurSpin2, from, to );
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
mSpin2.setAdapter(adapter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
ViewGroup row = (ViewGroup)v;
mSCursor.moveToPosition(position);
mItemId = mSCursor.getInt(mSCursor.getColumnIndex(DbAdapter.KEY_SID));
mCheckBox = (CheckBox) row.findViewById(R.id.pl_selected);
mcurSpin1 = (Spinner) row.findViewById(R.id.pl_spin1);
mcurSpin2 = (Spinner) row.findViewById(R.id.pl_spin1);
mtv1 = (TextView) row.findViewById(R.id.pl_tv1);
mtv2 = (TextView) row.findViewById(R.id.pl_tv2);
if (mCheckBox.isChecked()) {
mCheckBox.setChecked(false);
mcurSpin1.setVisibility(View.GONE);
mtv1.setVisibility(View.GONE);
mcurSpin2.setVisibility(View.GONE);
mtv2.setVisibility(View.GONE);
}
else {
mCheckBox.setChecked(true);
mcurSpin1.setVisibility(View.VISIBLE);
mtv1.setVisibility(View.VISIBLE);
mcurSpin2.setVisibility(View.VISIBLE);
PopulateSpinner1();
PopulateSpinner2();
}
}
}
Thanks in advance for any help you can give.
You can try adding android:descendantFocussability=blocksDescendants to the top most linear layout. This might be helpful.
Related
I am creating a student details application in android.I need to display student's photo at the top and display name,roll number,address etc as a table.I added a imageview at the top and listview just below the image view.Can i add the image inside the list view ( Now image is not scrollable ).
activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/def"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"/>
<ListView
android:id="#id/list"
android:layout_above="#+id/ad_view"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
Activity.java
list = (ListView) findViewById(R.id.list);
ArrayList<HashMap<String, String>> productsList;
productsList = new ArrayList<HashMap<String, String>>();
/*
adding data from database to productsList
*/
ListAdapter adapter =
new SimpleAdapter(this, productsList, R.layout.full, new String[]{"left", "right"},
new int[]{R.id.left, R.id.right});
list.setAdapter(adapter);
full.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:background="#drawable/background_border"
android:padding="15dp"
>
<TextView
android:id="#+id/left"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="cnsdsdsf:"
android:layout_weight="1"
android:gravity="center"
/>
<TextView
android:id="#+id/right"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_weight="1"
android:gravity="center"
/>
</LinearLayout>
you can use
ImageView imageView = new ImageView(this);
listView.addHeaderView(imageView);
to add a imageView on the head of listview
You can treat the top row or any other row as special for the image. in getView() inflate with separate layout for that image according to position.
You can also modify your item list layout file (full.xml) and create a custom listview adapter. This method will give you more freedom in terms of design. You can play around with this full.xml
full.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="64dp"
android:paddingRight="32dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
tools:background="#ffaa00">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/studentPicture"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="#drawable/student_picture"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/left"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/studentPicture"
android:layout_toEndOf="#+id/studentPicture"
android:textColor="#ffffffff"
android:textSize="20sp"
android:paddingLeft="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/right"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/left"
android:layout_toEndOf="#+id/left"
android:textColor="#ffffffff"
android:textSize="20sp"
android:paddingLeft="10dp" />
</RelativeLayout>
Then you create a adapter that will use this list item layout file:
StudentsAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class StudentsAdapter extends BaseAdapter{
private Context mContext;
private String[] mNames;
private String[] mOtherInfo;
public StudentsAdapter (Context context, String[] mNames, String[] mOtherInfo) {
mContext = context;
mNames = students;
mOtherInfo = mOtherInfo;
}
#Override
public int getCount() {
return mNames.length;
}
#Override
public Object getItem(int position) {
return mNames[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
//brand new
convertView = LayoutInflater.from(mContext).inflate(R.layout.full, null);
holder = new ViewHolder();
holder.studentImageView = (ImageView) convertView.findViewById(R.id.studentPicture);
holder.left= (TextView) convertView.findViewById(R.id.left);
holder.right= (TextView) convertView.findViewById(R.id.right);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
//here you set the picture, name, etc.
holder.studentImageView.setImageResource(/* your method to find specific image view */);
holder.left.setText(mNames[position]);
holder.right.setText(mOtherInfo[position]);
return convertView;
}
private static class ViewHolder {
ImageView studentImageView;
TextView left;
TextView right;
}
}
Your activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/list"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#android:id/empty"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:text="No data to display"
android:textColor="#ffffffff"/>
</RelativeLayout>
And now implement this adapter in your Activity.java
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.Arrays;
public class Activity extends Activity {
//you will need to set this data
private String[] mNames;
private String[] mOtherInfo;
ListView mListView;
TextView mEmptyTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
mListView = (ListView) findViewById(android.R.id.list);
mEmptyTextView = (TextView) findViewById(android.R.id.empty);
DayAdapter adapter = new DayAdapter(this, mNames, mOtherInfo);
mListView.setAdapter(adapter);
mListView.setEmptyView(mEmptyTextView);
}
}
Basically the android list view activity is transparent well i have managed to make it solid black using custom layout but how do i make it like the third image where the corner colour will be random .
thanks in advance
MainActivity.java
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ArrayAdapter;
public class MainActivity extends Activity {
// Declare
private LinearLayout slidingPanel;
private boolean isExpanded;
private DisplayMetrics metrics;
private RelativeLayout headerPanel;
private int panelWidth;
private int panelWidth1;
private ImageView menuViewButton,menuRightButton;
FrameLayout.LayoutParams menuPanelParameters;
FrameLayout.LayoutParams slidingPanelParameters;
LinearLayout.LayoutParams headerPanelParameters;
LinearLayout.LayoutParams listViewParameters;
//Example
////////
String[] Example = new String[]
{ "Android Introduction","Android Setup/Installation","Android Hello World",
"Android Layouts/Viewgroups","Android Activity & Lifecycle","Intents in Android"};
////////
//Example
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layer_stack);
// Initialize
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
panelWidth = (int) ((metrics.widthPixels) * -0.65);//Right panel width
panelWidth1 = (int) ((metrics.widthPixels) * 0.65);//left panel width
headerPanel = (RelativeLayout) findViewById(R.id.header);
headerPanelParameters = (LinearLayout.LayoutParams) headerPanel
.getLayoutParams();
headerPanelParameters.width = metrics.widthPixels;
headerPanel.setLayoutParams(headerPanelParameters);
slidingPanel = (LinearLayout) findViewById(R.id.slidingPanel);
slidingPanelParameters = (FrameLayout.LayoutParams) slidingPanel
.getLayoutParams();
slidingPanelParameters.width = metrics.widthPixels;
slidingPanel.setLayoutParams(slidingPanelParameters);
///////
ArrayAdapter<String> ExampleArrayAdapter = new CustomAdapter(this, Example);
//changing code//practice
/*new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
Example);*/
ListView ExampleListView = (ListView)findViewById(R.id.listView);
ExampleListView.setAdapter(ExampleArrayAdapter);
///////
// Slide the Panel
menuRightButton = (ImageView) findViewById(R.id.menuViewButton);
menuRightButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (!isExpanded) {
isExpanded = true;
// Expand
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.menuPanel,
new LeftMenuFragment());
fragmentTransaction.commit();
new ExpandAnimation(slidingPanel, panelWidth1,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.65f, 0, 0.0f, 0, 0.0f);
} else {
isExpanded = false;
// Collapse
new CollapseAnimation(slidingPanel, panelWidth1,
TranslateAnimation.RELATIVE_TO_SELF, 0.65f,
TranslateAnimation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f,
0, 0.0f);
}
}
});
}
}
custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal"
android:layout_marginTop="60dp"
android:background="#drawable/blue_bg">
<ImageView
android:id="#+id/imageView1"
android:layout_width="70dp"
android:layout_height="60dp"
android:src="#drawable/ic_launcher"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:gravity="left" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="19sp"
android:textColor="#ffffff"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="20dp"
android:text="Tweet body text here"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginTop="5dp"
android:ellipsize="end"
android:lines="3"
android:textColor="#ffffff"
android:textSize="14sp" />
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Small Text"
android:layout_marginTop="5dp"
android:textColor="#ffffff"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
CustomAdapter.java
public class CustomAdapter extends ArrayAdapter {
private LayoutInflater inflater;
public CustomAdapter (Activity activity, String[] items){
super(activity, R.layout.custom_layout, items);
inflater = activity.getWindow().getLayoutInflater();
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
return inflater.inflate(R.layout.custom_layout, parent, false);
}
}
I have managed to make it show random colour with updating the get view function with the following code but everytime i scroll the colour changes, how do i make it stable so that once the colour is assigned it won't change?
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
View rowView = inflater.inflate(R.layout.custom_layout, parent, false);
rowView.setBackgroundColor(color);
You have to use a custom adapter with two views. You can change the color of left view in getView method of adapter.
Finally, i have achieved the following answer to my question But i'm still facing the problem where on every scroll the colors are changing , where i want them to be constant once assigned, if anyone can help it will be great. thank you all. By just modifying xml and custom adapter code i have made it my xml code as
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_marginTop="70dp"
android:layout_height="70dp"
android:background="#drawable/blue_bg">
<LinearLayout
android:layout_marginLeft="20dp"
android:layout_width="345dp"
android:layout_height="70dp"
android:background="#drawable/white_bg">
<ImageView
android:id="#+id/imageView1"
android:layout_width="70dp"
android:layout_height="60dp"
android:src="#drawable/ic_launcher"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:gravity="left" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:layout_marginTop="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="19sp"
android:textColor="#000000"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="15dp"
android:text="Tweet body text here"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginTop="5dp"
android:ellipsize="end"
android:lines="3"
android:textColor="#000000"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Modifying the java code with the following code
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(257), rnd.nextInt(258));
View rowView = inflater.inflate(R.layout.custom_layout, parent, false);
rowView.setBackgroundColor(color);
Create your layout file as follows.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_weight="0.3" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_weight="0.7" />
</LinearLayout>
Your activity class onCreate() method should look like something like this.
ListView listView = (ListView) findViewById(R.id.listView1);
String[] Example = new String[] { "Android Introduction",
"Android Setup/Installation", "Android Hello World",
"Android Layouts/Viewgroups", "Android Activity & Lifecycle",
"Intents in Android" };
ArrayList<String> list = (ArrayList<String>) Arrays.asList(Example);
CustomAdapter adapter = new CustomAdapter(this, list);
listView.setAdapter(adapter);
Create a custom adapter class as follows.
public class CustomAdapter extends BaseAdapter {
Context context;
ArrayList<String> listOfContents;
LayoutInflater inflater;
public CustomAdapter(Context context, ArrayList<String> list) {
this.context = context;
listOfContents = list;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return listOfContents.size();
}
#Override
public Object getItem(int position) {
return listOfContents.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.list_row, null);
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256),
rnd.nextInt(256));
TextView clorText = (TextView) convertView.findViewById(R.id.textView1);
TextView clorText2 = (TextView) convertView
.findViewById(R.id.textView2);
clorText.setBackgroundColor(color);
clorText2.setBackgroundColor(Color.BLACK);
return convertView;
}
}
This code is completely untested. You should get a rough idea about using Listview and Custom adapter and generating random color.
The colour keeps changing because the list items are recycled, now when android calls the getView() method for the second time for a particular list item on scrolling, you regenerate the colour value and it changes.
Since, you are able to generate random colours, all you need to do is to store the colour once the item is generated, and then reuse that colour.
What you could do is to create an array list which stores the colour value and then inside the getView() method check if the colour for a particular position exists in the arraylist. If yes, then use that colour else generate a random colour and also add it to the arraylist of colours.
E.g. (Untested code but the approach should be similar)
Inside getView() :
int color = 0;
if(colorsList.size > position) {
//This means that the color has already been generated for this position
color = colorsList.get(position);
} else {
//Color hasnt been generated for this position.
color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256),
rnd.nextInt(256));
colorsList.add(color);
}
clorText.setBackgroundColor(color);
My activity contains a ListView that is populated from an SQLite Database. However I find that the items on the ListView are not clickable. They are not clickable at all in the emulator or on the device. I know there are a lot of similar questions and I tried every possible one of them but to no avail. Please help me make the items clickable.
My Activity Code :
package com.tintin.scheduler_3;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
public class List_Course extends ListActivity {
DatabaseHelper db;
SimpleCursorAdapter dataAdapter;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.course_list);
Button add = (Button) findViewById(R.id.add);
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
startActivity(new Intent(List_Course.this, Add_Course.class));
}
});
Log.v("Button", "On Click Done");
db = new DatabaseHelper(List_Course.this);
Log.v("Button", "On Click Done2");
displayList();
Log.v("Button", "On Click Done3");
db.close();
}
public void onResume(){
Cursor newCursor = db.getAllCourses();
dataAdapter.changeCursor(newCursor);
super.onResume();
db.close();
}
public void displayList(){
Cursor cursor = db.getAllCourses();
String from [] = new String[] {db.colName,db.colDisplay};
int to[] = new int[] {R.id.textView1, R.id.textView2};
dataAdapter = new SimpleCursorAdapter(this, R.layout.listitem, cursor, from, to, 0){
public View getView(int position, View convertView, ViewGroup parent){
final View row = super.getView(position, convertView, parent);
if(position % 2 == 0)
row.setBackgroundColor(Color.parseColor("#D8D8D8"));
else
row.setBackgroundColor(Color.parseColor("#D0D0D0"));
return row;
}
};
ListView lv = getListView();
lv.setAdapter(dataAdapter);
registerForContextMenu(lv);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Log.v("Click","Anything "+arg2+" "+arg3);
}
});
cursor.close();
db.close();
}
}
The Layout of this Activity :- (NOTE: I found that without the TextView the list populated from bottom up even though android:stackfromBottom was not set. So I put in the TextView and put the ListView below that)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#D0D0D0" >
<Button
android:id="#+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Add a Course" />
<TextView
android:id="#+id/tv"
android:layout_width="match_parent"
android:layout_height="1dp"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false" >
</TextView>
<ListView
android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv"
android:layout_above="#+id/add"
android:layout_alignParentLeft="true"
android:divider="#FFFFFF"
android:clickable="true"
android:dividerHeight="2dp" >
</ListView>
</RelativeLayout>
And the listitem.xml that defines how rows are to be shown in the listview :-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
EDIT:: - I noticed that the items on my ListView are actually clickable. The problem however is that on clicking the list items the orange selection color is not shown. So unless the log is checked, it is not apparent which row was clicked on.However if I remove the part where I am overriding the getView method of the SimpleCursorAdapter while defining dataAdapter, on clicking the items there is an orange selection color that appears in the background of the clicked row.
In other words I am not getting any color for the focused row in list view.
Please run it in one of AVDs to understand what I am trying to say here. I would appreciate if anyone can help me get that color while also overriding the getView method.
I am almost 100% sure that your problem is that some of the views rendered inside the ListView's items is avoiding the ItemClick event. In order to solve this, start by removing the follwing attributes from the TextView:
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textAppearance="?android:attr/textAppearanceSmall"
Try to override onListItemClick() instead of setting OnItemClickListener. Subclasses of ListActivity should override this method.
http://developer.android.com/reference/android/app/ListActivity.html
I think all problems are in your xml's code.
Change your listitem.xml as..
?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
And from code behind use below code.Ofcourse use this in your onCreate() method
ls1 = (ListView) findViewById(R.id.yourListviewName);
ls1.setOnItemClickListener(new OnItemClickListener()
{
#Override public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
Toast.makeText(getApplicationContext(), "" + position, Toast.LENGTH_SHORT).show();
}
});
Alternatively see similar issues here..
android-how-to-programmatically-click-select-tap-a-listview-item/
how-to-click-a-listview-item-in-android
I'm trying to set a Listview under some other Widgets (Buttons, editText, etc). I don't want to use another activity for the listview. After reading some I found How can I implement a ListView without ListActivity? (use only Activity) and I tried to do it, ending up with:
Here is my main.xml:
<LinearLayout android:id="#+id/relativeLayout1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical">
<TextView ...[some code].../>
<EditText ...[some code].../>
<ImageButton ...[some code].../>
<Chronometer ..[some code]..../>
<ListView android:id="#+id/listView1" android:layout_height="wrap_content"
android:layout_width="fill_parent"></ListView>
here is my onCreate:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (ImageButton) findViewById(R.id.button1);
mChronometer = (Chronometer) findViewById(R.id.chronometer1);
editText1 = (EditText) findViewById(R.id.editText1);
ListView lv = (ListView) findViewById(R.id.listView1);
String[] listword = new String[] {"Hello","World","Foo","Bar"};
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, listword));
}
and here is list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
When I debug in my physical device, the application simply stays all black. If I comment out the lines of onCreate() that involve the list, the application works (obviously without the listview).
Any ideas what might be wrong?
I actually have an app with a listview below some TextViews and above two buttons, let me grab my code!
Here's my activity with listview below the textviews and above the buttons (with quite a bit removed for brevity):
public class BNYDirectoryResults extends Activity{
public static String[] stuff;
ListView list;
BNYAdapter adapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.resultscreen);
TextView headDisplay = (TextView)findViewById(R.id.results);
TextView headCount = (TextView)findViewById(R.id.resultsTotal);
TextView headPages = (TextView)findViewById(R.id.pages);
//Set up the results list, see BNYAdapter
list = (ListView)findViewById(R.id.list);
adapter = new BNYAdapter (this, BNYDirectory.ReturnResults);
list.setAdapter(adapter);
//Sets up header information (pages, total results)
//Just some stuff to change the TextViews
//Passes EmployeeID and Phone Number to AND opens the details page
list.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
String EID = BNYDirectory.ReturnResults[position][0];
String phoneNumber = BNYDirectory.ReturnResults[position][2];
BNYDirectoryTransaction.doDetails(EID, phoneNumber);
Intent i = new Intent(view.getContext(), BNYDirectoryDetails.class);
startActivity(i);
}
});
}
}
Here's the XML file for it:
<TextView
android:id="#+id/results"
android:text="Name"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" android:textStyle="bold"/>
<TextView
android:id="#+id/resultsTotal"
android:text="Phone"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" android:textStyle="bold"/>
<TextView
android:id="#+id/pages"
android:text="AIM"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/name"
android:text="Name"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="#+id/phone"
android:text="Phone"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="#+id/aim"
android:text="AIM"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="#+id/dept"
android:text="Dept"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/prevButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Previous"
android:layout_weight="1"/>
<Button
android:id="#+id/nextButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Next"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
and here's the custom adapter for the list:
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class BNYAdapter extends BaseAdapter {
private Activity activity;
private String[][] results;
private static LayoutInflater inflater=null;
public BNYAdapter(Activity a, String[][]info) {
activity = a;
results = info;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return results.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public TextView name;
public TextView phone;
public TextView aim;
public TextView dept;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.item, null);
holder=new ViewHolder();
holder.name=(TextView)vi.findViewById(R.id.nameItem);
holder.phone=(TextView)vi.findViewById(R.id.phoneItem);
holder.aim=(TextView)vi.findViewById(R.id.aimItem);
holder.dept=(TextView)vi.findViewById(R.id.deptItem);
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
holder.name.setText(BNYDirectory.ReturnResults[position][1]);
holder.phone.setText(BNYDirectory.ReturnResults[position][2]);
holder.aim.setText(BNYDirectory.ReturnResults[position][3]);
holder.dept.setText(BNYDirectory.ReturnResults[position][4]);
return vi;
}
}
and all together that makes a page like
You can use your own layout with a ListActivity. Make your activity extend ListActivity, create your own layout and make sure that the ListView has android:id="#android:id/list" (this is how the ListActivity links to the ListView in your own layout), then in onCreate set your layout with setContentView.
Here is the my code for the Dialog With ListView and here i also maintain the state when the user click on list it background will be fill with green as you can see in my image below
But problem that i have is below
1>I want to increase my row height .How can do?
2>and i also want to put image beside in all row is this possible?
Here is my code for all this stuff.
package com.android.listselector;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class ListSelector extends Activity {
private SelectedAdapter selectedAdapter;
private ArrayList<String> list;
private Context mContext = ListSelector.this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.selected_example);
String[] items = { "lorem", "ipsum", "dolor", "sit", "amet",
"consectetuer", "adipiscing", "elit", "morbi", "vel", "ligula",
"vitae", "arcu", "aliquet", "mollis", "etiam", "vel", "erat",
"placerat", "ante", "porttitor", "sodales", "pellentesque",
"augue", "purus" };
// populate the model - a simple a list
list = new ArrayList<String>();
for (int i = 0; i < items.length; i++) {
list.add(items[i]);
}
// create our SelectedAdapter
selectedAdapter = new SelectedAdapter(this, 0, list);
selectedAdapter.setNotifyOnChange(true);
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.selected_example);
dialog.setTitle("Custom Dialog");
ListView listview = (ListView) dialog.findViewById(R.id.listExample);
listview.setAdapter(selectedAdapter);
dialog.show();
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
// user clicked a list item, make it "selected"
selectedAdapter.setSelectedPosition(position);
}
});
}
// move up event handler
// move down event handler
// Move selected item "up" in the ViewList.
private void moveUp() {
int selectedPos = selectedAdapter.getSelectedPosition();
if (selectedPos > 0) {
String str = list.remove(selectedPos);
list.add(selectedPos - 1, str);
// set selected position in the adapter
selectedAdapter.setSelectedPosition(selectedPos - 1);
}
}
// Move selected item "down" in the ViewList.
private void moveDown() {
int selectedPos = selectedAdapter.getSelectedPosition();
if (selectedPos < list.size() - 1) {
String str = list.remove(selectedPos);
list.add(selectedPos + 1, str);
// set selected position in the adapter
selectedAdapter.setSelectedPosition(selectedPos + 1);
}
}
public class SelectedAdapter extends ArrayAdapter<String> {
// used to keep selected position in ListView
private int selectedPos = -1; // init value for not-selected
public SelectedAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
}
public void setSelectedPosition(int pos) {
selectedPos = pos;
// inform the view of this change
notifyDataSetChanged();
}
public int getSelectedPosition() {
return selectedPos;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
// only inflate the view if it's null
if (v == null) {
LayoutInflater vi = (LayoutInflater) this.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.selected_row, null);
}
// get text view
TextView label = (TextView) v.findViewById(R.id.txtExample);
// change the row color based on selected state
if (selectedPos == position) {
label.setBackgroundColor(Color.GREEN);
} else {
label.setBackgroundColor(Color.WHITE);
}
label.setText(this.getItem(position).toString());
/*
* // to use something other than .toString() MyClass myobj =
* (MyClass)this.getItem(position);
* label.setText(myobj.myReturnsString());
*/
return (v);
}
}
}
and here are the layout used in my code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="#+id/listExample"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#CCCCCC"
android:choiceMode="singleChoice"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/btnMoveUp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/btnMoveDown"
/>
</LinearLayout>
here is other one
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:id="#+id/txtExample" android:textSize="18sp" android:textColor="#000000"
android:background="#FF0000">
</TextView>
Use the following code to get your expectation. image view is placed to the below of text view. If you want to see image view right side of taxt view then instead of using android:orientation="vertical" in linearlayout use android:orientation="horizontal"
selected_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/linearLayout"
android:layout_marginTop="5dip"
android:layout_marginBottom="5dip">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:id="#+id/txtExample" android:textSize="18sp" android:textColor="#000000"
android:background="#FF0000">
</TextView>
<ImageView
android:id="#+id/accountIcon"
android:layout_width="wrap_content"![enter image description here][1]
android:layout_height="wrap_content"
android:background="#drawable/btn_public_message_focus"
android:layout_marginRight="6dip" />
</LinearLayout>
Donot forget to vote if my response is useful for you.
Thanks
Deepak
You should customize the listview to increase the row height.
possible to put image only using the customized listview in dialog.
check this
If you want to make the ListView with different settings, you have to create the Custom ListView and also CustomAdapter for that listview.
http://saigeethamn.blogspot.com/2010/04/custom-listview-android-developer.html
http://www.androidpeople.com/android-custom-listview-tutorial-part-2
You have to make seprate layout for this to add image in the cell
Use this layout for your List row.....
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:orientation="vertical"
android:gravity="center_vertical">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:id="#+id/txtExample" android:textSize="18sp" android:textColor="#000000"
android:background="#FF0000">
</TextView>
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/icon"
/>
</LinearLayout>
Yes i get solution by here all my answer
first i have to modify selected_row xml file by this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="100dip">
<TextView android:layout_width="fill_parent"
android:layout_height="40px" android:id="#+id/txtExample"
android:textSize="18sp" android:textColor="#000000"
android:gravity="center_vertical"
/>
<ImageView android:layout_width="wrap_content"
android:layout_height="40px" android:id="#+id/imgbeside"
android:scaleType="center" android:layout_centerVertical="true"
android:layout_alignParentRight="true" android:src="#drawable/selector"
android:layout_marginRight="10dip" />
and also in my java file i put the following thing below this line
TextView label = (TextView) v.findViewById(R.id.txtExample);
ImageView mImageview = (ImageView) v.findViewById(R.id.imgbeside);
this will work for me.Ok Thanks For all reponse.