I use the following function to resize the text on a spinner when an item is selected:
mySpinner.post(new Runnable()
{
public void run()
{
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
TextView tempView = ((TextView) parent.getChildAt(0));
((TextView) parent.getChildAt(0)).setTextSize(TypedValue.COMPLEX_UNIT_PX, SetTextSize(tempView.getText().toString(), tempView.getWidth() - tempView.getPaddingLeft() - tempView.getPaddingRight(), (int) fieldHeight - tempView.getPaddingTop() - tempView.getPaddingBottom()));
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
// Do nothing
}
});
}
});
The problem is, before it resizes the text to the correct size, the text is drawn in the incorrect size. What I need to do is call my function SetTextSize as soon as the item is selected. I cannot find anyway to do this. setOnItemSelectedListener won't do because text is displayed in the incorrect size before the event runs.
I have changed:
mySpinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, CHOICES));
To:
mySpinner.setAdapter(new MyArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, CHOICES));
and added the following class:
public class MyArrayAdapter extends ArrayAdapter
{
public MyArrayAdapter(Context context, int textViewResourceId, Object[] objects)
{
super(context, textViewResourceId, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
((TextView) convertView).setTextSize(TypedValue.COMPLEX_UNIT_PX, SetTextSize(((TextView) convertView).getText().toString(), convertView.getWidth() - convertView.getPaddingLeft() - convertView.getPaddingRight(), (int) fieldHeight - convertView.getPaddingTop() - convertView.getPaddingBottom()));
return convertView;
}
}
Unfortunately, now my program crashes immediately when it starts.
I have created the following class:
class MySpinnerAdapter extends ArrayAdapter<String>
{
MySpinnerAdapter()
{
super(MainActivity.this, R.layout.row, R.id.label, CHOICES);
}
public View getView(int position, View convertView, ViewGroup parent)
{
View row = super.getView(position, convertView, parent);
TextView label = (TextView)row.findViewById(R.id.label);
if (label != null)
label.setTextSize(TypedValue.COMPLEX_UNIT_PX, SetTextSize(label.getText().toString(), label.getWidth() - label.getPaddingLeft() - label.getPaddingRight(), (int) fieldHeight - label.getPaddingTop() - label.getPaddingBottom()));
return(row);
}
}
I now have 2 problems. Firstly, this line causes my program to crash:
label.setTextSize(TypedValue.COMPLEX_UNIT_PX, SetTextSize(label.getText().toString(), label.getWidth() - label.getPaddingLeft() - label.getPaddingRight(), (int) fieldHeight - label.getPaddingTop() - label.getPaddingBottom()));
Secondly, the styles that are in "row.xml" are applied to my spinner. If I don't specify any styles, then my program crashes. I simply do not want the xml file to alter the way my spinner looks because I do not know at compile time. I would like the spinner to use the default values that it was using before I created the XML file, and anything else I can then change in code.
Change your this code
mySpinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, CHOICES));
to
mySpinner.setAdapter(new ArrayAdapter<String>(this, R.layout.spinner_layout, CHOICES));
where the code for spinner_layout.xml is
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerItemStyle"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:gravity="center_vertical"
android:textColor="#color/green"
android:paddingLeft="4dp"
android:textSize="18sp"
android:ellipsize="marquee"
android:singleLine="true" />
change layout_height and text size according to your need
I finally have the text on my spinner resizing perfectly and I think the code is efficient. Thanks everyone who tried to help. :)
class MySpinnerAdapter extends ArrayAdapter<String>
{
MySpinnerAdapter(int dropDownResource, String[] choices)
{
super(MainActivity.this, dropDownResource, choices);
}
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if (row == null)
row = super.getView(position, convertView, parent);
if (parent.getWidth() > 0)
{
TextView label = (TextView) row.findViewById(android.R.id.text1);
label.setTextSize(TypedValue.COMPLEX_UNIT_PX, SetTextSize(label.getText().toString(), parent.getWidth() - label.getPaddingLeft() - label.getPaddingRight(), (int) fieldHeight - label.getPaddingTop() - label.getPaddingBottom()));
label.setPadding(label.getPaddingLeft(), 0, 0, 0);
}
return(row);
}
}
Related
listContent.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
for(int i=0;i<showLists.size();i++){
//
TextView v=(TextView)listContent.getChildAt(i).findViewById(R.id.txtDes);
v.setTextColor(Color.BLACK);
}
TextView v=(TextView)listContent.getChildAt(position).findViewById(R.id.txtDes);
v.setTextColor(Color.RED);
Toast.makeText(context,"POS "+showLists.get(position).getDes(),Toast.LENGTH_SHORT).show();
}
});
I have been the problem with get position item of listview. Android just has shown about 12 row on devide's creen, when I click another item on listview (my listview have 30 item ) which shown the error.
And this is error:
"Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference" .
Thanks for read.
listContent.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
for (int i = 0; i < showLists.size(); i++) {
TextView v = (TextView) view.findViewById(R.id.txtDes);
v.setTextColor(Color.BLACK);
}
TextView v = (TextView) view.findViewById(R.id.txtDes);
v.setTextColor(Color.RED);
Toast.makeText(context, "POS " + showLists.get(position).getDes(), Toast.LENGTH_SHORT).show();
}
});
Your question is indeed about Null Pointer Exception, but is harder to identify why this is happening. The problem can be found here:
for(int i=0;i<showLists.size();i++){ <-- this line actually causes the crash
//
the crash is in the next line, at the findViewById
TextView v=(TextView)listContent.getChildAt(i).findViewById(R.id.txtDes);
v.setTextColor(Color.BLACK);
}
Your for has the wrong upper limit because of a mechanism known as recycling, and because of this mechanism your list view will never have the same number of rows as the amount of data that needs to be displayed (read about recycling to understand this). Given this fact, we know for sure that the number of views found in list view (listContent.getChildCount()) will be smaller than showLists.size() and thus making the call listContent.getChildAt(i) to return a NULL value when the index equals listContent.getChildCount() creating the crash.
Now you might be tempted to change showLists.size() with listContent.getChildCount() and see that the app doesn't crash anymore, but if you click a row, then other rows are coloured also as you scroll the list (the recycling is again the problem). To really fix the problem you should save the index of the selected row and call notifyDatasetChanged, so when getView is called in adapter you simply check the current position to be displayed with the selected position. In case of equality you change the color of text to red, otherwise to black. Below, you will find some parts of an example:
int currentPosition = -1;
// Just a basic adapter. The getView method is the key here
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1) {
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (position == currentPosition) {
((TextView) view).setTextColor(Color.RED);
} else {
((TextView) view).setTextColor(Color.BLACK);
}
return view;
}
};
// and here is the onItemClick
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
currentPosition = position;
adapter.notifyDataSetChanged();
}
});
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewRow=convertView;
if(viewRow==null){
viewRow=inflater.inflate(layout,parent,false);
viewHolder viewHolder=new viewHolder();
viewHolder.imgIcon = (ImageView) viewRow.findViewById(R.id.imgIcon);
viewHolder.txtDes = (TextView) viewRow.findViewById(R.id.txtDes);
viewRow.setTag(viewHolder);
}
viewHolder holder= (viewHolder) viewRow.getTag();
holder.imgIcon.setImageResource(listMoiNhat.get(position).getIcon());
holder.txtDes.setText(listMoiNhat.get(position).getDes());
if(position==currentpos){
holder.txtDes.setTextColor(Color.RED);
}
else {
holder.txtDes.setTextColor(Color.RED);
}
return viewRow;
}
//and here is create customListMoiNhat object
final customListMoiNhat customListMoiNhat=new customListMoiNhat(context,R.layout.moinhat_customlistview,showLists,currentpos);
customListMoiNhat.notifyDataSetChanged();
listContent.setAdapter(customListMoiNhat);
listContent.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
currentpos=position;
customListMoiNhat.notifyDataSetChanged();
}
});
I am facing some problem for rendering ListView from a dynamic layout. I don't know why the getView is called only with position 0 and several times!
I searched over internet and stackoverflow but cannot find a suitable answer.
I am actually trying to do a demo of this: http://www.framentos.com/en/android-tutorial/2012/07/16/listview-in-android-using-custom-listadapter-and-viewcache/
Notably, my main layout file is surrounded by scrollbar.
main activity layout file:
<ListView
android:id="#+id/city_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/questionsList"
android:paddingTop="20sp" >
</ListView>
My layout file for list view:
<?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="80dip" >
<ImageView
android:id="#+id/ImageCity"
android:layout_width="90sp"
android:layout_height="90sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toRightOf="#id/ImageCity"
android:orientation="vertical"
android:paddingLeft="10sp">
<TextView
android:id="#+id/cityName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25sp" />
<TextView
android:id="#+id/cityLinkWiki"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="web"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>
Adapter class:
import com.incidentreport.app.classes.objects.City;
public class CityListAdapter extends ArrayAdapter{
private int resource;
private LayoutInflater inflater;
private Context context;
public CityListAdapter ( Context ctx, int resourceId, List objects) {
super( ctx, resourceId, objects );
resource = resourceId;
inflater = LayoutInflater.from( ctx );
context=ctx;
}
#Override
public View getView ( int position, View convertView, ViewGroup parent ) {
Log.v("adapter", "pos: " + position + "#" + resource);
/* create a new view of my layout and inflate it in the row */
convertView = ( RelativeLayout ) inflater.inflate( resource, null );
/* Extract the city's object to show */
City city = (City)getItem( position );
/* Take the TextView from layout and set the city's name */
TextView txtName = (TextView) convertView.findViewById(R.id.cityName);
txtName.setText(city.getName());
/* Take the TextView from layout and set the city's wiki link */
TextView txtWiki = (TextView) convertView.findViewById(R.id.cityLinkWiki);
txtWiki.setText(city.getUrlWiki());
/* Take the ImageView from layout and set the city's image */
ImageView imageCity = (ImageView) convertView.findViewById(R.id.ImageCity);
return convertView;
}
}
main activity code snipps:
List listCity= new ArrayList();
listCity.add(new City("London","http://en.wikipedia.org/wiki/London","london"));
listCity.add(new City("Rome","http://en.wikipedia.org/wiki/Rome","rome"));
listCity.add(new City("Paris","http://en.wikipedia.org/wiki/Paris","paris"));
ListView listViewCity = ( ListView ) findViewById( R.id.city_list);
listViewCity.setAdapter( new CityListAdapter(this, R.layout.layout_city, listCity ) );
Okay, I figured out the issue by expanding ListView as much possible. Meaning to say, giving a dynamic full height so that all item becomes visible.
I followed the below solution:
Calculate the size of a list view or how to tell it to fully expand
Use a ViewHolder pattern for better performance.
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
static class ViewHolder
{
TextView txtName,txtWiki;
ImageView imageCity;
}
Change getView to
#Override
public View getView ( int position, View convertView, ViewGroup parent ) {
ViewHolder holder;
if(convertView == null)
{
convertView = ( RelativeLayout ) inflater.inflate( resource, parent, false );
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.cityName);
holder.txtWiki = (TextView) convertView.findViewById(R.id.cityLinkWiki);
holder.imageCity = (ImageView) convertView.findViewById(R.id.ImageCity);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
City city = (City)getItem( position );
holder.txtName.setText(city.getName());
holder.txtWiki.setText(city.getUrlWiki());
return convertView;
}
ListView recyclues view's. You may also want to read
How ListView's recycling mechanism works
Try this way,hope this will help you to solve your problem.
public class CityListAdapter extends BaseAdapter{
private Context context;
private List objects;
public CityListAdapter ( Context context, int resourceId, List objects) {
this.context=context;
this.objects=objects;
}
#Override
public int getCount() {
return objects.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public Object getItem(int position) {
return objects.get(position);
}
#Override
public View getView ( int position, View convertView, ViewGroup parent ) {
ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.layout_city,null);
holder.txtName = (TextView) convertView.findViewById(R.id.cityName);
holder.txtWiki = (TextView) convertView.findViewById(R.id.cityLinkWiki);
holder.imageCity = (ImageView) convertView.findViewById(R.id.ImageCity);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.txtName.setText(((City)getItem(position)).getName());
holder.txtWiki.setText(((City)getItem(position)).getUrlWiki());
return convertView;
}
class ViewHolder{
TextView txtName;
TextView txtWiki;
ImageView imageCity;
}
}
I ran into this same issue. My list view is mostly working, but there's a certain sequence of actions which will make certain items disappear. Clicking on them afterwards causes a NullPointerException.
Here's the steps to reproduce the bug:
Drag an item to the top.
Drag another item up or down.
The item at the top will disappear.
Drag another item up or down.
The top item will reappear.
Behavior continues if you go to step 2
After debugging, I found that my StableArrayAdapter.getView() method was being called twice, only for the blank item at the top of the list.
To fix it, per masum7's answer, I set the layout_height for my DynamicListView to "wrap_content".
Try to get layout inflater as
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
This may work
I have an ArrayAdapter for a list view that has multiple buttons in it. For one toggle button, I want to have a default state based on a condition, and let users toggle the button as well.
However, when users click button on row 1, the button for row 3 actually gets selected. I'm not sure why this is happening. Below is snippet of relevant code from my getView method with comments.
layout of my toggle button
<ToggleButton android:id="#+id/color_toggle"
android:layout_width="50px"
android:layout_height="50px"
android:focusable="false"
android:textOn="" android:textOff="" android:layout_alignParentLeft="true"
android:layout_marginRight="10dp"
/>
class Color {
int id;
int something;
}
List<Color> colorsList;
class ColorHolder {
TextView colorNameText;
ToggleButton toggleButton;
}
public View getView(final int position, final View convertView, final ViewGroup parent) {
View rowView = convertView;
Color c = colorsList.get(position);
if (null == rowView) {
rowView = this.inflater.inflate(R.layout.list_item_color, parent, false);
holder = new ColorHolder();
holder.colorNameText = (TextView) rowView.findViewById(R.id.color_name);
holder.toggleButton = (ToggleButton) rowView.findViewById(R.id.color_toggle);
rowView.setTag(holder);
}
else {
holder = (ColorHolder)rowView.getTag();
}
holder.toggleButton.setTag(c.getId());
final ColorHolder thisRowHolder = holder;
holder.toggleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (thisRowHolder.toggleButton.isChecked()) {
thisRowHolder.toggleButton.setBackgroundDrawable(//normal button);
thisRowHolder.toggleButton.setChecked(false);
for (int i = 0; i < colorList.size(); i++) {
if (colorList.get(i) == (Integer)v.getTag()) {
colorList.get(i).setSomething(0);
break;
}
}
adapter.notifyDataSetChanged();
}
else {
thisRowHolder.toggleButton.setBackgroundDrawable(//enabled button);
thisRowHolder.toggleButton.setChecked(true);
for (int i = 0; i < colorList.size(); i++) {
if (colorList.get(i) == (Integer)v.getTag()) {
colorList.get(i).setSomething(1);
break;
}
}
adapter.notifyDataSetChanged();
}
}
});
if (c.getSomething()>0) {
holder.toggleButton.setBackgroundDrawable(//enabled button);
holder.toggleButton.setChecked(true);
}
else {
holder.toggleButton.setBackgroundDrawable(//normal button);
holder.toggleButton.setChecked(false);
}
return rowView;
}
Question
What am I doing wrong? why are other buttons in third row toggling even though i'm toggling buttons in row one.
I read that this happens because the listView recycles, is there no way to fix it? Some strategies i've tried, to no avail, based on similar questions: 1) put onClickListener in the if clause. 2) instead of setting int in setTag instead set the holder and use that holder in onClickListener
update
I've updated all the code in the question with suggestions I received.
Hope This Helps.
Activity Code
public class DemoActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ColorInfo[] clr= new ColorInfo[20];
for(int i=0;i<20;i++){
clr[i] = new ColorInfo();
}
((ListView)findViewById(R.id.list)).setAdapter(new MyAdapter(this, 0, clr));
}
private static class MyAdapter extends ArrayAdapter<ColorInfo> implements OnClickListener{
LayoutInflater inflater;
public MyAdapter(Context context, int textViewResourceId,
ColorInfo[] objects) {
super(context, textViewResourceId, objects);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = inflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.tgl = (ToggleButton) convertView.findViewById(R.id.toggle);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
holder.tgl.setTag(position);
holder.tgl.setOnClickListener(this);
holder.tgl.setChecked(getItem(position).isChecked);
return convertView;
}
private static class ViewHolder{
ToggleButton tgl;
}
public void onClick(View v) {
int pos = (Integer) v.getTag();
ColorInfo cinfo = getItem(pos);
cinfo.isChecked = !cinfo.isChecked;
}
}
private static class ColorInfo{
boolean isChecked=false;
}
}
main.xml
<?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/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
row.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="match_parent"
android:orientation="vertical" >
<ToggleButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/toggle"
/>
</LinearLayout>
Your problem is listview recycling views
You have to store state of toggle button for each row of listview.
Eg.Create class which stores information about each row,suppose ColorInfo which contains color and isChecked boolean.
so instead of
Color c = colorsList.get(position);
it will be
ColorInfo colorInfo = colorsList.get(position);
and in getview
togglebutton.setCheck(colorInfo.isCheck)
and in onClick listener of toggle buttons you change state of object of ColorInfo for that position to toggleChecked true or false and notifyDatasetChanged,this will solve your problem.
You are using a member variable for your ViewHolder, rather than a final local variable. So your OnClickListener is referencing whatever the latest holder instance is, which will correspond with the most recently created or recycled list item.
Do this instead:
//Lock in this reference for the OnClickListener
final ColorHolder thisRowHolder = holder;
holder.favButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (thisRowHolder.toggleButton.isChecked()) {
thisRowHolder.toggleButton.setBackgroundDrawable(getResources().getDrawable(...);
thisRowHolder.toggleButton.setChecked(false);
}
else {
thisRowHolder.toggleButton.setBackgroundDrawable(getResources().getDrawable(...));
thisRowHolder.toggleButton.setChecked(true);
}
}
});
...
Edit:
Also noticed this. In these two lines:
holder.colorNameText = (TextView) itemView.findViewById(R.id.color_name);
holder.toggleButton = (ToggleButton) itemView.findViewById(R.id.color_toggle);
You are finding the views in some member variable itemView, but you need to be finding them in rowView so you are getting the instances for this specific row. All your view holders are looking at the same ToggleButton instance, which may not even be on screen.
Edit 2:
One more thing you're missing. You need to store the state of the toggle buttons and reapply them. So in your OnClickListener, when you call setChecked() you must also update the backing data in colorsList. Looks like you already cached a reference to the proper list element in your ToggleButton's ID, so should be easy. Then move this block of code out of your if/else block and put it afterwards, so the toggle button is always updated to the latest data:
if (c.getSomething()>0) {
holder.toggleButton.setBackgroundDrawable(getResource().getDrawable(...)));
holder.setChecked(false);
}
else {
holder.toggleButton.setBackgroundDrawable(getResource().getDrawable(...)));
holder.setChecked(true);
}
I want to add a second on click to a ListViewItem.
I already created the View (ImageView) and i set the on Click. The function gets called.
But: How can i get the Informations of this ListViewItem? It would be enough to get the Position in the ListView?
The ImageView:
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:onClick="favorite"
android:src="#drawable/star" />
The code for my on click function:
public void favorite(View view){
ImageView iView = (ImageView) view;
iView.setImageResource(R.drawable.star_checked);
ViewParent v = iView.getParent();
}
Use an anonymous inner class:
ImageView iView = (ImageView) findViewById(R.id.imageView1);
iView.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
//Your code
}
});
myListView.setOnItemClickListener(new OnItemClickListener() { ![enter image description here][2]
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String value = myListView.getAdapter().getItem(position).toString(); // String value of the clicked item
//Code
}
});
If you want to make bigger applications you should think about better naming your components, 'imageView1' is not very handy. Name you components like 'imageview_main' or 'imageview_customerdetails'.
Subclass OnItemClickListener rather than OnClickListener.
Edit:
Okay I think I understand what you are trying to do now. I would subclass your ListAdapter and override the getView(int position, ...) method like so:
private OnClickListener mImgClickListener = new OnClickListener() {
#Override
public void onClick(View view) {
int position = (Integer) view.getTag();
// do stuff with position knowledge!
}
});
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView != null) {
v = convertView;
} else {
v = newView(int position);
}
v.findViewById(R.id.imageView1).setTag(position);
return v;
}
private View newView(int position) {
View v;
// inflate your view here
View imageView = v.findViewById(R.id.imageView1);
imageView.setOnClickListener(mImgClickListener);
return v;
}
Of course, even better would be to implement the ViewHolder pattern in the getView method to eliminate expensive calls to findViewById.
I believe the View hierarchy will take care of giving priority to the click on the ImageView (rather than the list item as a whole), but I could be wrong.
I have a spinner and I have added a custom style to spinner. Problem is when I select an item it doesnt show up on the spinner but when I use android spinner style it shows what I selected on the spinner. Is there any more coding to add to make it work? Otherwise everthing of the spinner works. I have written the app when an item is selected in spinner to show a text. These things work. But it doesnt show what I selected.
Here is my code
MyAdapter dataAdapter3 = new MyAdapter(this, R.layout.spinner, list3);
spinner1.setAdapter(dataAdapter3);
list 3 referes a list
List<String> list3 = new ArrayList<String>();
Here is the class for custom spinner style
public class MyAdapter extends ArrayAdapter<String>
{
private List<String> listString = new ArrayList<String>();
public MyAdapter(Context context, int textViewResourceId, List<String> objects) {
super(context, textViewResourceId, objects);
this.listString = objects;
}
#Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.spinner, parent, false);
TextView label=(TextView)row.findViewById(R.id.textView1);
label.setText(listString.get(position));
return row;
}
}
please can anybody tell whether I have done any mistake here?
This is how spinner is shows when I selected item
I tried several days on this problem. Actually code is pretty ok. problem was in the spinner.xml file. I had added a large padding to textview. Therefore, though spinner works it is not visible the selected item on spinner. Point is I forget xml file. :D
Modify the padding in the layout spinner.xml file to a lower value,
for ex:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:textColor="#000000"
android:textSize="14sp" />