How to Customize the Spinner widget in android? - android

I am using Spinner control in my application and my Code is
Spinner s1=new Spinner(this);
s1.setLayoutParams(new LayoutParams(100,30));
ArrayAdapter adapter=new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter.add("One");
adapter.add("Two");
adapter.add("Three");
s1.setAdapter(adapter);
This will work fine in linearLayout .when I use the code in the TableLayout, the control is not displayed.I have to generate dynamic layout so i am not using the XML part,only through Java code i am adding the contorls.
TableLayout tl=new TableLayout(this);
TableRow tr=new TableRow(this);
tr.addView(s1);
tl.addView(tr);
All the components gets added other than Spinner.
So please anyone give some solution for this.

Now add your tl to ScrollView using:
ScrollView sv = new Scrollview(this);
sv.addview(tl);
setContentview(sv);

#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("Name", "One");
map.put("Icon", R.drawable.icon);
list.add(map);
map = new HashMap<String, Object>();
map.put("Name", "Two");
map.put("Icon", R.drawable.icon);
list.add(map);
Spinner spin = (Spinner) findViewById(R.id.spin);
myAdapter adapter = new myAdapter(getApplicationContext(), list,
R.layout.list_layout, new String[] { "Name", "Icon" },
new int[] { R.id.name, R.id.icon });
spin.setAdapter(adapter);
}
private class myAdapter extends SimpleAdapter {
public myAdapter(Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.list_layout,
null);
}
HashMap<String, Object> data = (HashMap<String, Object>) getItem(position);
((TextView) convertView.findViewById(R.id.name))
.setText((String) data.get("Name"));
((ImageView) convertView.findViewById(R.id.icon))
.setImageResource(R.drawable.icon);
return convertView;
}
}

Related

How to cast view to a custom layout in the onItemClick function of AdapterView.OnItemClickListener?

I'm new to Android. I want use a SimpleAdapter with a custom layout. Do I have store the original data and then use the id to access it? the view parameter should be something that I can use. But what is the proper way to do it?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_section);
ListView listView = (ListView) findViewById(R.id.MyListView);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
for(int i=0;i<30;i++)
{
HashMap<String, String> map = new HashMap<String, String>();
map.put("ItemTitle", "This is Title.....");
map.put("ItemText", "This is text.....");
mylist.add(map);
}
SimpleAdapter mSchedule = new SimpleAdapter(this,
mylist,
R.layout.list_delegate,
new String[] {"ItemTitle", "ItemText"},
new int[] {R.id.ItemTitle,R.id.ItemText});
listView.setAdapter(mSchedule);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Log.d(TAG, "clicked: " + view);
}
});
}
//simplified activity_section.xml
<LinearLayout
android:id="#+id/MyListItem">
<TextView
android:id="#+id/ItemTitle"/>
<TextView
android:id="#+id/ItemText"/>
</LinearLayout>
Use findViewById to access ItemTitle and ItemText from clicked row of ListView as:
String itemTitle = ((TextView)
view.findViewById(R.id.ItemTitle)).getText().toString();
Do same for accessing ItemText TextView value

ListView widget android

I'm working on android application that shows number and name of persons , using ListView .But the compilers doesn't recognize "champs1" and "champs2".
public class Second extends Activity {
ListView vue;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act2);
vue=(ListView) findViewById(R.id.listV2);
String [][] rep=new String[][]{{"1","aaaaaaa" },
{"2","bbbbbbb" },
{ "3","ccccccc" } };
List<HashMap<String,String> > list= new ArrayList<HashMap<String, String>>();
HashMap<String,String> element;
for(int i=0 ;i<rep.length;i++ ){
element =new HashMap<String, String>();
element.put("chmaps1",rep[i][0]);
element.put("champs2",rep[i][1]);
list.add(element);
}
ListAdapter adapter=new SimpleAdapter(this,
list,
android.R.layout.simple_list_item_2,
new String[] {"chmaps1","champs2"},
new int[] {android.R.id.champs1, android.R.id.champs2 });
vue.setAdapter(adapter);
}
}
Error:(39, 71) error: cannot find symbol variable champs1
Error:(39, 71) error: cannot find symbol variable champs2
change android.R.id.champs1 to android.R.id.text1
change android.R.id.champs2 to android.R.id.text2
public class Second extends Activity {
ListView vue;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_searchable);
vue = (ListView) findViewById(R.id.listView1);
String[][] rep = new String[][] { { "1", "aaaaaaa" },
{ "2", "bbbbbbb" }, { "3", "ccccccc" } };
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> element;
for (int i = 0; i < rep.length; i++) {
element = new HashMap<String, String>();
element.put("chmaps1", rep[i][0]);
element.put("champs2", rep[i][1]);
list.add(element);
}
ListAdapter adapter = new SimpleAdapter(this, list,
android.R.layout.simple_list_item_2, new String[] { "chmaps1",
"champs2" }, new int[] { android.R.id.text1,
android.R.id.text2 });
vue.setAdapter(adapter);
}
}
You are refferring to:
android.R.id.champs1
instead of:
R.id.champs1
Also be careful that you ar calling variables chmaps1 sometimes and champs1 some other times.
put this code in for loop
HashMap<String, String> element = new HashMap<String, String>();
and there is no problem i think android.R.id.champs1 or R.id.champs1 both are correct .
change this one instead of this vue.setAdapter(adapter)
try like this vue.setListAdapter(adapter);

Android passing image to a ListView containing an ImageView

I'm creating a ListView that contains two TextViews and an ImageView.
I'm passing data to my SimpleAdapter using a Hashmap, so I convert the ids of the images to a string and then later in the SimpleAdapter I convert them back to integers to set the image resource.
However that doesn't seem to work.
The relevant code is:
The oncreate of my activity
clubImages = new Integer[] {
R.drawable.pic1, R.drawable.pic2,
R.drawable.pic3, R.drawable.pic4,
R.drawable.pic5
};
ListView lv = (ListView)findViewById(android.R.id.list);
// create the grid item mapping
String[] from = new String[] {"image"};
int[] to = new int[] {R.id.club_image};
// prepare the list of all records
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < eventTitles.length; i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("image", getString(clubImages[i]));
fillMaps.add(map);
}
// fill in the grid_item layout
SimpleAdapter adapter = new MySimpleAdapter(this, fillMaps, R.layout.eventview_row, from, to);
lv.setAdapter(adapter);
the simpleadapter class
public class MySimpleAdapter extends SimpleAdapter {
private List<HashMap<String, String>> results;
public MySimpleAdapter(Context context, List<HashMap<String, String>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
this.results = data;
}
public View getView(int position, View view, ViewGroup parent){
Typeface type = Typeface.createFromAsset(getAssets(), "fonts/aircruiser.ttf");
View v = view;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.eventview_row, null);
}
mClubImageImageView = (ImageView) v.findViewById(R.id.club_image);
int ClubImageHelper = Integer.parseInt(results.get(position).get("image").toString());
mClubImageImageView.setImageResource(ClubImageHelper);
return v;
}
}
The error in my logcat
E/AndroidRuntime(19406): java.lang.NumberFormatException: unable to parse 'res/drawable-hdpi/pic1.png' as integer
You can't pass R.drawable.pic1 as a String and then parse it back as an Integer. This will not work, instead simply pass the initial Integer drawable ids directly in the HashMap:
List<HashMap<String, Integer>> fillMaps = new ArrayList<HashMap<String, Integer>>();
for(int i = 0; i < eventTitles.length; i++){
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("image", clubImages[i]);
fillMaps.add(map);
}
I hope I understand what you want. You could make a class to hold the data that you want to pass in to the adapter, for example:
class DataObject {
int imageId;
String firstString;
String secondString; // add here any string that you want to put in your list
public DataObject(String firstString, String secondString, int imageId) {
this.imageId = imageId;
this.firstString = firstString;
this.secondString = secondString;
}
}
Then construct the list for the adapter:
List<HashMap<String, DataObject>> fillMaps = new ArrayList<HashMap<String, DataObject>>();
for(int i = 0; i < eventTitles.length; i++){
HashMap<String, DataObject> map = new HashMap<String, DataObject>();
map.put("image", new DataObject("First String", "second String", clubImages[i]));
fillMaps.add(map);
}
In the getView() method retrieve the DataObject and use its content data as you please.
here is an good example that may help you

How to add Image to the Custom List View in Android?

I have a custom list view with image view and text view.I am loading text view data from SQLite Database and I want to add image from my drawable folder to the image view contain in list based on some condition
I am using cursor to load data to the list view.Please any one guide me how to do this
Very simple solution for that,using a simple adapter.
public class TestList extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView list = (ListView) findViewById(R.id.list);
ArrayList<Map<String, String>> arrlist = new ArrayList<Map<String, String>>();
Map<String, String> m = null;
for (int i = 0; i <=50; i++) {
m = new HashMap<String, String>();
m.put("key", String.valueOf(R.drawable.ic_launcher));
m.put("title", "Title-" + i);
m.put("subtitle", "SubTitle-" + i);
arrlist.add(m);
}
SimpleAdapter adapter = new SimpleAdapter(this, arrlist,
R.layout.sample, new String[] { "key", "title", "subtitle" },
new int[] { R.id.imageView1, R.id.title, R.id.subtitle });
list.setAdapter(adapter);
}
}
Xml layouts: main xml layout has a listview with id-list,and sample.xml is your inflating layout,it has one image,two textviews.Modify the code as your requirement.

Using SimpleAdapter with Spinner

I am new to Android development. I am trying to populate a spinner by using the SimpleAdapter. But spinner's list is showing blank element. When I click any element, its text is shown properly in Toast. Please tell me what is the problem in my code below.
public void onCreate(Bundle savedInstanceState) {
private List<Map<String, String>> data = new ArrayList<Map<String, String>>();
String[] from = new String[] { "colorsData" };
int[] to = new int[] { R.id.spinner };
String[] colors = getResources().getStringArray(R.array.colorsData);
for (int i = 0; i < colors.length; i++) {
data.add(addData(colors[i]));
}
Spinner spinner = (Spinner) findViewById(R.id.spinner);
SimpleAdapter simpleAdapter = new SimpleAdapter(this, data, android.R.layout.simple_spinner_item, from, to);
simpleAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(simpleAdapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(
parent.getContext(),
"Selected Color:- "
+ parent.getItemAtPosition(position),
Toast.LENGTH_LONG).show();
}
});
}
private Map<String, String> addData(String colorName) {
Map<String, String> mapList = new HashMap<String, String>();
mapList.put("colorsData", colorName);
return mapList;
}
I'm about 95% sure that your to array should be declared as:
int[] to = new int[] { android.R.id.text1 };
Give that a try.
EDIT (based on comments below):
It seems there was a bug in older versions of AndroidOS which caused that IllegalStateException. (I didn't see the exception in 2.2, but I did see it in 1.5 in the emulator.) The bug can be worked around by adding a ViewBinder to the SimpleAdapter. ViewBinder isn't hard to implement; here's an example:
SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() {
public boolean setViewValue(View view, Object data,
String textRepresentation) {
// We configured the SimpleAdapter to create TextViews (see
// the 'to' array), so this cast should be safe:
TextView textView = (TextView) view;
textView.setText(textRepresentation);
return true;
}
};
simpleAdapter.setViewBinder(viewBinder);
I blogged about this here.

Categories

Resources