Monday, April 14, 2014

Spinner on how to achieve key bindings


Recently turned to the development of learning android, gray is often necessary to replenish the basics, this is not a problem to the

spinner bound data source can be a xml file binding, the binding can also be an array of strings, but I found online is only a single string to pull up, no you can customize the ID to identify;.
This makes a lot of things are easy to get, for example, extracted from the database ID and String, not automatically bind up

One solution is to define a separate array, store ID, and then according to the position corresponding to fetch ID
 public void onItemSelected (AdapterView  arg0, View view, int position, long id) {
. Toast.makeText (getApplicationContext (), "Action Selected: position =" + position + "id =" + id, Toast.LENGTH_SHORT) show ();
mPosition = position;
}


The position can be made to the above
But personally feel that this approach is very scientific

Now would like to ask no better way?
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Rewrite bindView way, I also see large cattle speak.
Reply:
spinner can set the adapter's (setAdapter), you achieve an adapter on the line. adapter binding data used. Then spinner after setting adapter will be able to display it. As for how to achieve adapter, this is the foundation, google it, or apidemos where there are many examples, list in there, list14.java
Reply:
In order to achieve the realization of the value of taking VALUE and TEXT values ​​in the android's spinner, I've tried better way to check the information on the Internet, are said to modify the adapter, I also started to do by modifying the method of the adapter, but if there is an activity multiple spinner, you need to declare a global variable several multi-adapter. View the source code behind the adapter found, in fact, there is no need to modify the adapter, the adapter inside pass directly to the object, and then rewrite the object toString () method can be a simple solution. Without further ado, Post Code:

Declare an object, and rewrite the object toString ().

public class CItem {

private String ID = "";
private String Value = "";

public CItem () {
ID = "";
Value = "";
}

public CItem (String _ID, String _Value) {
ID = _ID;
Value = _Value;
}

@ Override
public String toString () {/ / Why rewrite toString () do? Because the case when the data in the display adapter, the adapter if the incoming string object is not directly on the use of object. ToString ()
/ / TODO Auto-generated method stub
return Value;
}

public String GetID () {
return ID;
}

public String GetValue () {
return Value;
}
}



Then declare the object instance and add to arraylist inside and set the spinner adapter

Spinner Sp = (Spinner) ............. / /

List lst = new ArrayList ();

CItem ct = new CItem ("1", "test");

lst.Add (ct);;

ArrayAdapter Adapter = new ArrayAdapter (context,
android.R.layout.simple_spinner_item, lst);

Sp.SetAdapter (Adapter);



Value:

If you take the TEXT values ​​can be directly taken: Sp.getSelectedItem.ToString () or: ((CItem) Sp.getSelectedItem) GetValue ();
.
If you go this way, you can take the value Value: ((CItem) Sp.getSelectedItem) GetID ();
.


Written in relatively simple, should be able to see to understand

Reply:
The above method from http://blog.163.com/fjshqhy_2003/blog/static/ 140268782010989384539 /
Reply:
This corresponding up. By writing bindView method, using an adapter
I hope you can, get hold of a complete application out, including xml, Tieshanglai. I have to learn. Learn

 public class simpleCursorAdapter extends SimpleCursorAdapter 
{
public simpleCursorAdapter (Context context, int layout, Cursor c, String [] from, int [] to)
{
super (context, layout, c, from, to);
/ / TODO Auto-generated constructor stub
}

/ / Rewrite bindView method
public void bindView (View view, Context context, Cursor cursor)
{
/ / Bind data to view, test
TextView name = (TextView) view.findViewById (android.R.id.text1);
TextView message = (TextView) view.findViewById (android.R.id.text2);

name.setText ("surplus" + cursor.getString (1) + "hour" + cursor.getString (2) + "minutes");
/ / Message.setText (cursor.getString (2));
/ / ItemCathe ic = new ItemCathe ();
/ / Set the tag tag line
view.setTag (cursor.getString (0));
message.setText (cursor.getString (3) + view.getTag ());

super.bindView (view, context, cursor);
}

}

Reply:
reference to the second floor dinjay reply:
spinner can set the adapter's (setAdapter), you achieve an adapter on the line. adapter binding data used. Then spinner after setting adapter will be able to display it. As for how to achieve adapter, this is the foundation, google it, or apidemos where there are many examples, list in there, list14.java
Well it using adapter
Reply:
I encountered this problem with zhangmengxiong achieved, plain and simple. Now I realize the code offer


package com.gsww.androidoa.domain;

public class BrandInfo {
private String brandId;
private String brandName;
public String getBrandId () {
return brandId;
}
public void setBrandId (String brandId) {
this.brandId = brandId;
}
public String getBrandName () {
return brandName;
}
public void setBrandName (String brandName) {
this.brandName = brandName;
}
public BrandInfo (String brandId, String brandName) {
this.brandId = brandId;
this.brandName = brandName;
}
@ Override
public String toString () {/ / This is the key
/ / Why should rewrite toString () do? Because the case when the data in the display adapter, the adapter if the incoming string object is not directly on the use of object. ToString ()
/ / TODO Auto-generated method stub
return brandName;
}
}

/ / ************************************************


List dataArray = new ArrayList ();
String brandid;
String brandname;
List list = new ArrayList ();
for (Iterator it = resultList.iterator (); it.hasNext () ;) {
Map resMap = (Map) it.next ();
brandid = (String) resMap.get ("brandid");
brandname = (String) resMap.get ("brandname");
BrandInfo brandInfo = new BrandInfo (brandid, brandname);
dataArray.add (brandInfo);
list.add (brandname);

}
ArrayAdapter adapter = new ArrayAdapter (this.context, android.R.layout.simple_spinner_item, dataArray);
adapter.setDropDownViewResource (android.R.layout.simple_spinner_dropdown_item);
brandSpinner.setAdapter (adapter);
brandSpinner.setPrompt ("Please select the brand!");


Reply:
In simple terms, is to write your own slightly
Reply:
Good things, learn
Reply:
Learn
Reply:
Thank you, landlord stickers ~ ~ ~ solved my problem ~ ~ ~
Reply:
Ah well, solve the problem

No comments:

Post a Comment