add image to list view - android

i want to add image to my list view and this is my list view activity code:
package com.example.dssdfsd;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView lv;
ArrayAdapter<String> adapter;
EditText inputSearch;
ArrayList<HashMap<String, String>> productList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String products[] = {"Dell Inspiron"};
lv = (ListView) findViewById(R.id.list_view);
inputSearch = (EditText) findViewById(R.id.inputSearch);
adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
lv.setAdapter(adapter);
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
MainActivity.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
#Override
public void afterTextChanged(Editable arg0) {
}
});
}
}
i want to add image to my list view. how can i do it?
can any one help me to add image on my list view?

You're probably going to need to create a custom ListView Adapter and a new XML Layout for the custom ListView row.
See Tutorial.
Ex:
list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow>
<ImageView
android:id="#+id/img"
android:layout_width="50dp"
android:layout_height="50dp"/>
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="50dp" />
</TableRow>
</TableLayout>
CustomList.java
public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final String[] web;
private final Integer[] imageId;
public CustomList(Activity context, String[] web, Integer[] imageId) {
super(context, R.layout.list_single, web);
this.context = context;
this.web = web;
this.imageId = imageId;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_row, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(web[position]);
imageView.setImageResource(imageId[position]);
return rowView;
}
}

Related

Create menu list without using ListActivity

I want to create a list menu with action bar, therefore I can't use ListActivity. The following code I have created but no list is shown up when I test it:
public class MainActivity extends AppCompatActivity {
ListView RecordList;
ArrayList<String> sectionArr;
ListAdapter adapter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getRecordSet(); //A procedure to get list from SQLite DB (tested ok)
adapter1 = new CardListAdapter(this, sectionArr);
RecordList = (ListView)findViewById(R.id.listSet);
RecordList.setAdapter(adapter1);
RecordList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id)
{
}
});
}...
In my custom adapter, the code is:
class CustomAdapter extends ArrayAdapter<String>{
public CustomAdapter(Context context, ArrayList<String> strArr) {
super(context, R.layout.custom_row, strArr);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
int[] colors = new int[] {0x30A1B6AE, 0x30ECE7E7};
int colorPos = position % colors.length;
if (rowView==null) {
LayoutInflater inflator = LayoutInflater.from(getContext());
rowView = inflator.inflate(R.layout.custom_row, parent, false);
rowView.setBackgroundColor(colors[colorPos]);
}
String singleMenuItem = getItem(position);
TextView menuItemText = (TextView) rowView.findViewById(R.id.idItem);
menuItemText.setText(singleMenuItem);
return rowView;
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
}
And the layout is:
<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="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"
tools:context="com.lusterview.studycards.MainActivity" >
<ListView
android:id="#+id/listSet"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="10dp"
android:cacheColorHint="#android:color/black" >
</ListView>
Please advise.
Thanks in advance.

How can I add another TextView in my Listview?

I've made a ListView by following a tutorial on the web. It works great and exactly like I want it to. Now I'm trying to create another ListView in another activity but this time I need two TextViews, not one.
Here's my Adapter:
public class CustomListTwo extends ArrayAdapter<String> {
private final Activity context;
private final String[] web;
// private final String[] hex; <-- was trying to add this one but it gave me an error :(
private final Integer[] imageId;
public CustomListTwo(Activity context,
String[] web, Integer[] imageId) {
super(context, R.layout.color_item_layout, web);
this.context = context;
this.web = web;
this.imageId = imageId;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.color_item_layout, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.item_value);
ImageView imageView = (ImageView) rowView.findViewById(R.id.item_color);
txtTitle.setText(web[position]);
imageView.setImageResource(imageId[position]);
return rowView;
}
}
I'm trying to add "String[] hex;" but it gives me an error saying that the "variable may not be initialized". My idea was to duplicate everything that was written about the web variable and replace it with the hex variable and change the values.
Here's my activity:
public class ColorRed extends Activity {
ListView list;
String[] web;
Integer[] imageId = {
R.color.red_50,
R.color.red_100,
R.color.red_200,
R.color.red_300,
R.color.red_400,
R.color.red_500,
R.color.red_600,
R.color.red_700,
R.color.red_800,
R.color.red_900,
R.color.red_A100,
R.color.red_A200,
R.color.red_A400,
R.color.red_A700,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_red);
web = getResources().getStringArray(R.array.values);
CustomListTwo adapter = new
CustomListTwo(ColorRed.this, web, imageId);
list=(ListView)findViewById(R.id.list_red);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch (position){
case 0: r50(view);
break;
case 1: r50(view);
break;
default:
r50(view);
break;
}
}
});
}
Here's my ListView item:
<?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="72dp"
android:background="?android:attr/selectableItemBackground">
<ImageView
android:background="?android:attr/selectableItemBackground"
android:layout_width="match_parent"
android:layout_height="72dp"
android:id="#+id/item_color"
android:src="#drawable/myrect" />
<TextView
android:id="#+id/item_hex"
android:layout_width="wrap_content"
android:layout_height="72dp"
android:textColor="#de000000"
android:textAllCaps="true"
android:textSize="18sp"
android:text="List Item"
android:paddingRight="16dp"
android:gravity="center_vertical"
android:layout_centerVertical="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="72dp"
android:text="New Text"
android:textColor="#de000000"
android:textSize="18sp"
android:id="#+id/item_value"
android:layout_centerVertical="true"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:paddingLeft="16dp"
android:gravity="center_vertical"/>
</RelativeLayout>
So basicly, my question is how do I add a new TextView and load it with an array from strings.xml just like I do with the current TextView? I want the TextView "hex" to be populated with text from an array which I declare in strings.xml.
EDIT: My updated adapter:
public class CustomListTwo extends ArrayAdapter<String> {
private final Activity context;
private final String[] web;
private final String[] hex;
private final Integer[] imageId;
public CustomListTwo(Activity context, String[] web, String[] hex, Integer[] imageId) {
super(context, R.layout.color_item_layout, web, hex);
this.context = context;
this.web = web;
this.hex = hex;
this.imageId = imageId;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.color_item_layout, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.item_value);
ImageView imageView = (ImageView) rowView.findViewById(R.id.item_color);
TextView txtHex = (TextView) rowView.findViewById(R.id.item_hex);
txtHex.setText(hex[position]);
txtTitle.setText(web[position]);
imageView.setImageResource(imageId[position]);
return rowView;
}
}
Updated activity:
public class ColorRed extends Activity {
ListView list;
String[] web;
String[] hex;
Integer[] imageId = {
R.color.red_50,
R.color.red_100,
R.color.red_200,
R.color.red_300,
R.color.red_400,
R.color.red_500,
R.color.red_600,
R.color.red_700,
R.color.red_800,
R.color.red_900,
R.color.red_A100,
R.color.red_A200,
R.color.red_A400,
R.color.red_A700,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_red);
web = getResources().getStringArray(R.array.values);
hex = getResources().getStringArray(R.array.hex_red);
CustomListTwo adapter = new
CustomListTwo(ColorRed.this, web, hex, imageId);
list=(ListView)findViewById(R.id.list_red);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch (position){
case 0: r50(view);
break;
case 1: r50(view);
break;
default:
r50(view);
break;
}
}
});
}
Now I get an error in this code:
super(context, R.layout.color_item_layout, web, hex);
"Cannot resolve method"
You can't declare a final variable and not initialize it anywhere.
Pass the hex array in your adapter constructor, assign it to the hex[] in the adapter and use it in getView(..).

How to add an icon to a listview in a fragment

I made a listview in a fragment. Now I need to add a simple icon to the listview items. The icon must be the same on every item. This will be a simple arrow (imageview).
Is this possible with the listview I made? And if yes, how do I do it?
So like this format:
My text here --space--space-- >
The code for the fragment:
public class BiblioFragment extends Fragment {
final String[] items = new String[] { "Astma en alcohol", "Astma en huisdieren", "Astma en lichaamsgewicht",
"Astma en ouder worden", "Astmamedicatie", "Bekende mensen met astma", "Longfunctieonderzoek", "Reizen en vakantie",
"Sociaal leven", "Weetjes over astma", "Enzovoort", "Enzovoort", "Enzovoort" };
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_biblio, container, false);
ListView list = (ListView)view.findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Log.v("TAG", "CLICKED row number: " + arg2);
Intent myIntent = new Intent(getActivity(), BiblioDetail.class);
myIntent.putExtra("welkerij", arg2);
startActivity(myIntent);
}
});
return view;
}
}
My XML for that fragment:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff">
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</LinearLayout>
In onCreateView
ListView list = (ListView)view.findViewById(R.id.listView1);
CustomAdapter cus = new CustomAdapter(getActivity(),items);
list.setAdapter(cus);
Use a custom adapter
public class CustomAdapter extends BaseAdapter {
String items[];
LayoutInflater mInflater;
public CustomAdapter(Context context, String[] items) {
mInflater = LayoutInflater.from(context);
this.items = items;
}
#Override
public int getCount() {
return items.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView ==null)
{
convertView = mInflater.inflate(R.layout.list_item,parent,false);
holder = new ViewHolder();
holder.tv = (TextView) convertView.findViewById(R.id.textView1);
holder.iv = (ImageView) convertView.findViewById(R.id.imageView1);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.tv.setText(items[position])
// use holder.iv to set whatever image you want according to the position
return convertView;
}
static class ViewHolder
{
ImageView iv;
TextView tv;
}
}
list_item.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" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="42dp"
android:layout_marginTop="32dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/imageView1"
android:layout_marginLeft="64dp"
android:layout_marginTop="14dp"
android:layout_toRightOf="#+id/imageView1"
android:text="TextView" />
</RelativeLayout>
Snap
Edit:
If you feel this is over kill you can use a compound textview with image on the left and text on the right.
In the above I have used list_item.xml which is a custom layout. I inflate that layout in getView of custom adapter. I set the text to textview. I have set the default launcher icon to imageview. You can change it though.
I have also used a ViewHolder
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
Also check
How ListView's recycling mechanism works
Create ListAdapter
public class ListAdapter extends ArrayAdapter<Item> {
public ListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
// TODO Auto-generated constructor stub
}
private List<Item> items;
public ListAdapter(Context context, int resource, List<Item> items) {
super(context, resource, items);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.itemlistrow, null);
}
Item p = items.get(position);
if (p != null) {
ImageView ICon = ( ImageView) v.findViewById(R.id.description);
}
}
return v;
}
In your Activity
ListView yourListView = (ListView) findViewById(R.id.itemListView);
// get data from the table by the ListAdapter
ListAdapter customAdapter = new ListAdapter(this, R.layout.itemlistrow, List<yourItem>);
yourListView .setAdapter(customAdapter);

When clicking on a row Cannot find the index or position in listview using arrayadapter

I have created a listview using following tutorial link http://www.ezzylearning.com/tutorial.aspx?tid=1763429
Outcome of this is, List of Activity as below image
Now, I want to navigate to different activity screen by clicking on individual row. I do have idea of navigating to next screen, However I'm not able to find the correct position of each row. I don't have much knowledge of android so please bare with my question if it is too silly.
although the code is available on the above link I'm attaching the .xml files and .java. here as well.
main.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:background="#FFFFFF">
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
listview_item_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<ImageView android:id="#+id/imgIcon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
<TextView android:id="#+id/txtTitle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:textStyle="bold"
android:textSize="22dp"
android:textColor="#000000"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
</LinearLayout>
Weather.java
public class Weather {
public int icon;
public String title;
public Weather(){
super();
}
public Weather(int icon, String title) {
super();
this.icon = icon;
this.title = title;
}
}
WeatherAdapter.java
public class WeatherAdapter extends ArrayAdapter<Weather>{
Context context;
int layoutResourceId;
Weather data[] = null;
public WeatherAdapter(Context context, int layoutResourceId, Weather[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
WeatherHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new WeatherHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
row.setTag(holder);
}
else
{
holder = (WeatherHolder)row.getTag();
}
Weather weather = data[position];
holder.txtTitle.setText(weather.title);
holder.imgIcon.setImageResource(weather.icon);
return row;
}
static class WeatherHolder
{
ImageView imgIcon;
TextView txtTitle;
}
}
MainActivity.java
public class MainActivity extends Activity {
private ListView listView1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Weather weather_data[] = new Weather[]
{
new Weather(R.drawable.weather_cloudy, "Cloudy"),
new Weather(R.drawable.weather_showers, "Showers"),
new Weather(R.drawable.weather_snow, "Snow"),
new Weather(R.drawable.weather_storm, "Storm"),
new Weather(R.drawable.weather_sunny, "Sunny")
};
WeatherAdapter adapter = new WeatherAdapter(this,
R.layout.listview_item_row, weather_data);
listView1 = (ListView)findViewById(R.id.listView1);
View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
}
any help will be appreciated
Thanks
You need to attach OnItemClickListener
listView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView <?> adapterView, View view, int position, long id) {
// position is index of the row that was clicked
}
});
where is listView1.setOnItemClickListener ? have you set setOnItemClickListener to listView1?.and in below code onItemClick have position.
listView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
}
});
You should do something like this
listView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //do something
}
});
use setOnItemSelectedListener with list http://developer.android.com/reference/android/widget/AdapterView.OnItemSelectedListener.html
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
listView1.setOnItemSelectedListener(
new OnItemSelectedListener() {
#Override
public void onItemSelected(
AdapterView<?> parent, View view, int pos, long id
) {
//where pos is your index
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {}
}
);
Try this way...
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
Toast.makeText(context, position+"", Toast.LENGTH_LONG).show();
Intent go_detail_Activity_Intent= new Intent(context,Target_Activity.class);
go_detail_Activity_Intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(go_detail_Activity_Intent);
}
});
I think it help you.

Spinner in ListView preventing highlighting in android

I'm trying to place a spinner inside a list view item in android. The problem is this seems to prevent the row from being highlighted during a press and the onItemClickListener is not getting called by the ListView. Basically I am trying to mimic the functionality in the Google Music app. Does anyone know how to do this? This is the code I have been trying (I know it may not be the best... just throwing together a quick sample which shows the problem):
Activity:
public class ListViewTestActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView) findViewById(R.id.listView1);
DataHolder data = new DataHolder(this);
DataHolder data1 = new DataHolder(this);
DataHolder data2 = new DataHolder(this);
DataHolder data3 = new DataHolder(this);
DataHolder data4 = new DataHolder(this);
DataAdapter d = new DataAdapter(this, R.layout.rowview, new DataHolder[] { data, data1, data2, data3, data4 });
listView.setAdapter(d);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Log.e("ERROR", "look at me!");
}
});
}
}
public class DataHolder {
private int selected;
private ArrayAdapter<CharSequence> adapter;
public DataHolder(Context parent) {
adapter = ArrayAdapter.createFromResource(parent, R.array.choices, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
public ArrayAdapter<CharSequence> getAdapter() {
return adapter;
}
public String getText() {
return (String) adapter.getItem(selected);
}
public int getSelected() {
return selected;
}
public void setSelected(int selected) {
this.selected = selected;
}
}
This is the list adapter:
public class DataAdapter extends ArrayAdapter<DataHolder> {
private Activity myContext;
public DataAdapter(Activity context, int textViewResourceId, DataHolder[] objects) {
super(context, textViewResourceId, objects);
myContext = context;
}
// We keep this ViewHolder object to save time. It's quicker than findViewById() when repainting.
static class ViewHolder {
protected DataHolder data;
protected TextView text;
protected Spinner spin;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
// Check to see if this row has already been painted once.
if (convertView == null) {
// If it hasn't, set up everything:
LayoutInflater inflator = myContext.getLayoutInflater();
view = inflator.inflate(R.layout.rowview, null);
// Make a new ViewHolder for this row, and modify its data and spinner:
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.text);
viewHolder.data = new DataHolder(myContext);
viewHolder.spin = (Spinner) view.findViewById(R.id.spin);
viewHolder.spin.setAdapter(viewHolder.data.getAdapter());
// Used to handle events when the user changes the Spinner selection:
viewHolder.spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
viewHolder.data.setSelected(arg2);
viewHolder.text.setText(viewHolder.data.getText());
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
Log.d("DBGINF", "asdf");
}
});
// Update the TextView to reflect what's in the Spinner
viewHolder.text.setText(viewHolder.data.getText());
view.setTag(viewHolder);
Log.d("DBGINF", viewHolder.text.getText() + "");
} else {
view = convertView;
}
// This is what gets called every time the ListView refreshes
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(getItem(position).getText());
holder.spin.setSelection(getItem(position).getSelected());
return view;
}
}
Main layout for activity
<?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">
<ListView android:id="#+id/listView1" android:layout_height="match_parent" android:layout_width="match_parent" />
</LinearLayout>
Layout for a row:
<?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="wrap_content" android:weightSum="1" >
<TextView android:layout_width="wrap_content"
android:layout_height="match_parent" android:id="#+id/text"
android:layout_weight="0.5" android:textSize="25sp" />
<Spinner android:layout_width="0dp" android:layout_height="wrap_content"
android:id="#+id/spin" android:prompt="#string/choice_prompt"
android:layout_weight="0.5" />
</LinearLayout>
Thanks for any help!
I had the same problem with ImageButton, and what was needed was to do
mImageButton.setFocusable(false);
I can't garantee it's the same for Spinner, but my money is on it.
Best regards.

Categories

Resources