I've been trying to bind data to a drawerlayout over the past few days with no luck. I think my issue is the binding of the data and making sure that I have the correct parameters in the right spot. The code is below. For the purposes of this code, I have an array of items that I load from Resources.Array.NavigationItems. Once I have this in a string array, I iterate through the list, create a dictionary with an item, which is the text to be displayed, and an image, which is the image that I want to display. At this time, the image is just load from my resources's drawables. I then create two arrays. These two arrays should map to the necessary from (data in the javalist) and to (the id of the controls to display. When I run the code below, I get the error shown below the code. I am assuming that I have something wrong with my dictionary definitions. Suggestions are appreciated. Quite honestly, I'm not sure that I am creating the Dictionary or the List properly with the correct objects; my skills in translating from java to .net are probably lacking in this area.
_navigationItems = Resources.GetStringArray(Resource.Array.NavigationItems);
var mList = new List<IDictionary<string, object>>();
for(int i=0;i<_navigationItems.Length;i++){
var hm = new Dictionary<String,Object>();
hm.Add("item", _navigationItems[i]);
hm.Add("image", Resource.Drawable.aniswath);
mList.Add(hm);
}
var fromH = new string[]{ "item", "image" };
var toH = new int[] { Resource.Id.title, Resource.Id.icon };
_drawerList.Adapter = new SimpleAdapter (this, mList,
Resource.Layout.custom_drawer_item, fromH, toH);
Here are the contents of my Resource/Layout/custom_drawer_item.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="horizontal">
<ImageView
android:id="#+id/icon"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:src="#drawable/aniswath" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="#id/icon"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textStyle="bold"
android:gravity="center_vertical"
android:paddingRight="40dp" />
</LinearLayout>
Error message determined from monitor:
04-11 21:40:50.022: E/AndroidRuntime(2515): java.lang.ClassCastException: mono.android.runtime.JavaObject cannot be cast to java.util.Map
04-11 21:40:50.022: E/AndroidRuntime(2515): at android.widget.SimpleAdapter.bindView(SimpleAdapter.java:147)
04-11 21:40:50.022: E/AndroidRuntime(2515): at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:126)
Related
So in my code I have 6 arrays (the 5 last could be empty) of objects. Each objects has a name and the array could have many of each object (every array are sorted so every item are grouped).
First, I have this xml file:
<LinearLayout 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:background="#drawable/fond4"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:background="#color/white"
android:gravity="center"
android:text="#string/commentOpti"
android:textSize="20sp" />
<Button
android:id="#+id/choisirTroupe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:onClick="soumission"
android:text="#string/lancer" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:background="#color/white"
android:gravity="center"
android:text="#string/casernes"
android:textSize="20sp" />
<!-- Caserne 1 -->
<LinearLayout
android:id="#+id/caserne1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#color/white"
android:orientation="horizontal" >
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:background="#drawable/caserne" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="#string/deuxPoints"
android:textSize="25sp" />
</LinearLayout>
//Then 5 other like this 1st linearLayou
//The ids are incremented each time like caserne2 for the 2nd etc...
</ScrollView>
It look like this: https://gyazo.com/17a1276dfff5521d540fb6dc953df424
What I want to do is, for each object in each array (one array: one linear layout), to add a textView with the number of Item and an imageView which represent the object.
Next, you will find how I sort everything, that's not really important I think, but if you want to understand everything you'll have to give a look :p
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_optimisation);
LinearLayout caserne1 = (LinearLayout) findViewById(R.id.caserne1);
//...Doing this for the 5 others...
Intent intent = getIntent();
//Instanciation of my object
OptiClashOfClans o = new OptiClashOfClans();
//In fact, the number of linearLayout i'll have to work with
o.setNbCasernes(this.nbCaserne);
if (this.nbBarbares > 0)
o.ajouterFormation(1, this.nbBarbares);
//...Adding each object in on array...
o.setAFormer(o.triTroupe());
//And finally here i'll put each object in the good array
o.orgaCaserne(o);
Now we go for my problem, this portion of code is just next to the one I put previously, here is how I pick up the number of the same object in each array
for (int cptCaserne = 0; cptCaserne < this.nbCaserne; cptCaserne++) {
// Here I pick up the array of object I'll work with
Caserne casTmp = o.getCaserneNum(cptCaserne);
// Here I pick every object which are in the previous array
ArrayList<Troupe> enFormTmp = new ArrayList<Troupe>();
enFormTmp = casTmp.getEnFormation();
// To count where I am in the array of object
int cptAFormer = 0;
//To know if the next object is different from the one I'm working with
Troupe troupTmp = enFormTmp.get(cptAFormer);
int cptTroupTmp = 0;
//For the object t in the array of object
for (Troupe t : enFormTmp) {
cptTroupTmp = 0;
//While the object is the same from the one we're working with
while (!troupTmp.equals(t)) {
//Incrementing the previous counter
cptTroupTmp++;
cptAFormer++;
}
And finally here is how I don't really know how to do:
ImageView iView = new ImageView(Optimisation.this);
//To know which background i'll add to the image view
//I'll do this for each different object
if (t.getNom().equals("Barbare"))
iView.setImageResource(R.drawable.barbare);
//...
//The text view with the number of object I found
TextView tView = new TextView(Optimisation.this);
tView.setText("" + cptTroupTmp);
//And now it's here where I want to add the different views
switch (cptCaserne) {
case 0:
caserne1.addView(tView);
caserne1.addView(iView);
case 1:
caserne2.addView(tView);
caserne1.addView(iView);
case 2:
caserne3.addView(tView);
caserne1.addView(iView);
case 3:
caserne4.addView(tView);
caserne1.addView(iView);
}
troupTmp = enFormTmp.get(cptAFormer);
}
}
With this code I have an insane black screen when I'm going on this activity.
Here is what I want to do with in red the textView with the number of objet and in green the imageView of the object...
https://gyazo.com/31b16982ce30b66391bafdd2cd4d86fc
I've found some stuff to do with LayoutInflater but I haven't been successfull with these... Thanks a lot if you could help me.
PS: sorry for the mistakes, I think there are a lot, but in long post like this one, my school english isn't very effective ^^
Thanks again :)
Correct me if I'm wrong. But from what I understood you just need to accomplish what is being shown here https://gyazo.com/31b16982ce30b66391bafdd2cd4d86fc?
Base on your code you just have to add an additional LinearLayout having an horizontal orientation on each LinearLayout before adding the TextView and Image View.
Here's an example using your code:
LinearLayout innerLayout = new LinearLayout(Optimisation.this)
innerLayout.setOrientation(LinearLayout.HORIZONTAL);
ImageView iView = new ImageView(Optimisation.this);
//To know which background i'll add to the image view
//I'll do this for each different object
if (t.getNom().equals("Barbare"))
iView.setImageResource(R.drawable.barbare);
//...
//The text view with the number of object I found
TextView tView = new TextView(Optimisation.this);
tView.setText("" + cptTroupTmp);
innerLayout.addView(tView);
innerLayout.addView(iView);
//And now it's here where I want to add the different views
switch (cptCaserne) {
case 0:
caserne1.addView(innerLayout);
case 1:
caserne2.addView(innerLayout);
case 2:
caserne3.addView(innerLayout);
case 3:
caserne4.addView(innerLayout);
}
Feel free to adjust it if the Layout that I'm adding the innerLayout in is wrong. But basically, that's the idea.
I'm new to android and struggling in changing the height of the dropdown list's height of a spinner.
I'm using the following code to inflate my spinner.
code
String[] fil_array = { "Starts with", "Contains" };
ArrayAdapter<String> m_FilterAdap = new ArrayAdapter<String>(
Mse_Customer.this, android.R.layout.simple_spinner_item, fil_array);
m_Filter.setAdapter(m_FilterAdap);
Please can anyone guide me how to achieve it.
Please have a look over the attached image for more clarity.
The second image working correctly with the same type of code.Really confused why it is happening.
don't use android.R.layout make your own custom layout
layout_spinner_item.xml
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8.0dip"
android:text="Text"
android:textSize="20.0sp" />
String[] fil_array = { "Starts with", "Contains" };
ArrayAdapter<String> m_FilterAdap = new ArrayAdapter<String>(
Mse_Customer.this, R.layout.layout_spinner_item, fil_array);
m_Filter.setAdapter(m_FilterAdap);
Create custom_spinner_item.xml
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp" />
Then in your code, replace:
ArrayAdapter<String> m_FilterAdap = new ArrayAdapter<String>(Mse_Customer.this, android.R.layout.simple_spinner_item, fil_array);
with
ArrayAdapter<String> m_FilterAdap = new ArrayAdapter<String>(Mse_Customer.this, R.layout.custom_spinner_item, fil_array);
create custom drop_down_spinner_item in xml and set:
layout_height="wrap_content" and minHeight="40dp";
My Android game project needs a "New Game" activity. The game has a couple of options that should be asked each time a new game is started: the board size and the game seed. I would like to do this in a form resembling the standard "Settings" activity, but with an added "Start" button at the bottom. Given that these things will be chosen every new game, they don't want to be hidden away in the app's Settings activity. So what's the best way to go about this?
Things I've investigated:
Using a custom PreferenceActivity, since the behaviour is almost the same. Since the PreferenceActivity doesn't use layout XML files I can't see a way of adding a button to it.
A Linear layout that includes a ListView and a Button. I've started implementing this, but I can't even find a way to add items to the ListView - the ListView guide in the documentation certainly isn't aimed at a beginner like me! And then the list items need to be interactive to allow options to be selected: this looks like a lot of work to duplicate what PreferenceActivity almost does on its own.
Is there an option that I'm missing? I've spent a couple of hours trying to find an answer in the documentation and in previous questions, but with no progress. If anyone can point me in the right direction I'd be grateful.
Here is some code for you. Hope it will be useful.
You should go by the second way and use LinearLayout (this is 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/lvSimple"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<Button // describe your start button here/>
</LinearLayout>
Layout for ListView item:
<?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="horizontal">
<ImageView
android:id="#+id/ivImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher">
</ImageView>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<CheckBox
android:id="#+id/cbChecked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox">
</CheckBox>
<TextView
android:id="#+id/tvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="20dp"
android:text="TextView">
</TextView>
</LinearLayout>
</LinearLayout>
Your MainActivity.java:
public class MainActivity extends Activity {
// имена атрибутов для Map
final String ATTRIBUTE_NAME_TEXT = "text";
final String ATTRIBUTE_NAME_CHECKED = "checked";
final String ATTRIBUTE_NAME_IMAGE = "image";
ListView lvSimple;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// data array
String[] texts = { "sometext 1", "sometext 2", "sometext 3",
"sometext 4", "sometext 5" };
boolean[] checked = { true, false, false, true, false };
int img = R.drawable.ic_launcher;
// our data source
ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>(
texts.length);
Map<String, Object> m;
for (int i = 0; i < texts.length; i++) {
m = new HashMap<String, Object>();
m.put(ATTRIBUTE_NAME_TEXT, texts[i]);
m.put(ATTRIBUTE_NAME_CHECKED, checked[i]);
m.put(ATTRIBUTE_NAME_IMAGE, img);
data.add(m);
}
String[] from = { ATTRIBUTE_NAME_TEXT, ATTRIBUTE_NAME_CHECKED,
ATTRIBUTE_NAME_IMAGE };
// array of IDs of Views
int[] to = { R.id.tvText, R.id.cbChecked, R.id.ivImg };
// create adapter
SimpleAdapter sAdapter = new SimpleAdapter(this, data, R.layout.item,
from, to);
// create ListView and set the adapter
lvSimple = (ListView) findViewById(R.id.lvSimple);
lvSimple.setAdapter(sAdapter);
}
}
I have already created a dynamic list view that passes text to a SimpleAdapter, however I would like to pass information to make an image appear from the drawable folder. Here is the XML layout for each row:
<ImageView
android:id="#+id/sport_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:padding="2dp"
android:textSize="20dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right" >
<TextView
android:id="#+id/item_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="13dp" />
</LinearLayout>
</LinearLayout>
and the bit of code creating the list:
//fill in the list items from the XML document
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
map.put("id", XMLfunctions.getValue(e, "fixture_id"));
map.put("sport", XMLfunctions.getValue(e, "sport"));
map.put("teams", XMLfunctions.getValue(e, "team_2") + " v " + XMLfunctions.getValue(e, "team_2"));
mylist.add(map);
}
//Make a new listadapter
ListAdapter adapter = new SimpleAdapter(this, mylist, R.layout.fixture, new String[] { "sport", "teams" }, new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
Now at the moment when I pass the string sport it just displays the text such as 'football' in the item_title text view. But as I know what the sport is I would like to be able to display an icon in the sport_icon imageview. An example of the sport icon location would be #drawable/icon_football.
Many thanks.
The answer here was creating a BaseAdapter with the ListView as suggested by Frank below. If anyone else is looking to do the same thing but has no idea where to start I highly recommend watching http://www.youtube.com/watch?v=pVs4qKmenQM. This video is well put together and explains everything you need to know.
:)
You are going to have to extend something like BaseAdapter and check what your position is in getView() and then accordingly set what image you would like to use.
SimpleAdapter only works nice if ALL the items are have the exact same format and it's very simple.
Here is an ok example.
http://thinkandroid.wordpress.com/2010/01/13/custom-baseadapters/
But I would really recommend watching the Romain and Adam's Google IO Video
http://www.youtube.com/watch?v=wDBM6wVEO70
I've now spent a full day on trying to get a custom row to load. There seem to be plenty of examples here on StackOverflow and other places on how to bind a checkbox to a row of data within an Android listview, but they all seem to be incomplete.
Here's what I have (row.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="40dip"
android:orientation="horizontal" android:background="#drawable/row_bk"
android:layout_gravity="center_vertical">
<TextView android:id="#+id/ItemID"
android:layout_weight="0" android:visibility="gone"
android:layout_width="0dp" android:gravity="center"
android:layout_height="wrap_content" />
<TextView android:id="#+id/Title"
android:layout_width="0dp" android:textColor="#666"
android:layout_weight="1" android:layout_height="40dip"
android:padding="5dp" android:gravity="center_vertical"
android:textSize="17dip" />
<CheckBox android:id="#+id/chkCheck" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:focusable="false" android:focusableInTouchMode="false" android:clickable="true"></CheckBox>
</LinearLayout>
In code, I have a SimpleCursorAdapter, populating the ListView.
Cursor oLoop = db
.rawQuery(
"select _id, title, is_checked from tbl",null);
startManagingCursor(oLoop);
String[] from = new String[] { "_id", "title", "is_checked" };
int[] to = new int[] { R.id.ItemID, R.id.Title, R.id.chkCheck };
SimpleCursorAdapter oList =
new SimpleCursorAdapter (this, R.layout.task_item_row, oLoop, from,
to);
setListAdapter(oList);
Past this, I'm not really sure what to do. If someone could point me at a good example, that would be great. The goal is to actually be able to toggle the checkboxes.
Thanks in advance!
Alex
Alex,
The next step is to implement ViewBinder.setViewValue()
It would look something like this:
getViewAdapter().setViewBinder(
new ViewBinder(){
public boolean setViewAdapter(View view, Cursor cursor, int columnIndex){
<JavaType> object = cursor.get<JavaType>(columnIndex);
boolean isHandled = false;
if(view.getId() == R.id.checkBox){
CheckBox cb = (CheckBox) view;
cb.setChecked(isObjectChecked(object));
// object depends on your underlying data type
// in the data base, use the debugger to find the actually implemented type.
isHandled = true;
}
return isHandled;
}
}
);
This can be a very powerful method with conditionally visible views and loading Uri's from the network, etc.