Load SimpleCursorAdapter from an Array - android

Is it possible to load a SimpleCursorAdapter from an Array of data i have made?
This is how a load my Array:
String[][] arrayStixoi = new String[countCategory][countCategory];
int i = 0;
while(ccget.moveToNext()){
String stixoi = ccget.getString(ccget.getColumnIndex("stixoi"));
String syggraf=ccget.getString(ccget.getColumnIndex("syggrafeas"));
String notes=ccget.getString(ccget.getColumnIndex("syggrafeas"));
String news=ccget.getString(ccget.getColumnIndex("syggrafeas"));
arrayStixoi[i][0] = Integer.toString(i);
arrayStixoi[i][1] = stixoi;
arrayStixoi[i][2] = syggraf;
arrayStixoi[i][3] = notes;
arrayStixoi[i][4] = news;
i++;
String _id=arrayStixoi[i][0]+1;
}
Now i want that Array: arrayStixoi to load a SimpleCursorAdapter and then add this adapter to a ListView.
Is this possible? and how?
Thank you in advance for your time.

In your code snippet you have
ccget.moveToNext()
I assume ccget is an instance of Cursor. With that, you can just instantiate a SimpleCursorAdapter using its default constructor:
new SimpleCursorAdapter(
this, R.layout.your_layout,
ccget,
from,
to,
flags)
Please refer to the official documentation.

Related

Selected Spinner Item as String for a new Cursor

Right now I am almost done writing my app. All I need is a bit help on getting the selected String value from a spinner populated from my database by a simple cursor adapter. I am not sure how I can get the String from my spinner and pass it to a different cursor and use the string in a query, that will populate depending on the first choice of the spinner, and so on with other spinners.
Here is the code I am using for one of my spinners.
vType = (Cursor) DataBaseHelper.getPowersportsType();
this.startManagingCursor(vType);
SimpleCursorAdapter scaType = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item,
vType,
new String [] {DataBaseHelper.POWERSPORTS_TYPE},
new int[] {android.R.id.text1});
scaType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
vTypeSpinner = (Spinner) findViewById(R.id.typeSpinner);
vTypeSpinner.setAdapter(scaType);
And this is my query for the next spinner in my xml layout
static String MakeWhere = "POWERSPORTS_TYPE=?";
public static Cursor getPowersportsMake(){
return myDataBase.query(POWERSPORTS_TABLE,
new String [] {POWERSPORTS_ID, POWERSPORTS_MAKE},
MakeWhere,
null,
POWERSPORTS_MAKE,
null,
null);
}
Any comments or suggestions are welcomed.
to get the item selected you need to set the onItemSelectedListener
then in your onItemtSelected all you would do is
String selection = vTypeSpinner.getSelectedItem().toString();

Convert from string[] to string Android

How can I convert from String[] to String only? So I can assign orderan to items and i can put it into my listview.
Here's my code.
MyClass class= new MyClass();
String orderan =class.getName();
String[] items = orderan; **the problem here
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
list.setAdapter(adapter);
It appears that you simply want this:
String orderan = class.getName();
String[] items = {orderan};
Or even:
String[] items = {class.getName()};
ArrayAdapter wants an array, but you're trying to assign the string orderan into the array items. Instead, make a new array, and add orderran into it. Something like:
String[] items = new String[1];
items[0] = orderan;
You have to create an Array and add the string to it.
String[] items = new String[1];
items[0] = orderan;
you can do it in one line
String[] items = {orderan};

Cannot cast from String[] to ArrayList<String>

I have a resource string array that I want to put into an ArrayList, however the datatype that comes back is a String array. The string[] cannot be directly cast to ArrayList w/o receiving the following error:
Cannot cast from String[] to ArrayList
How to I convert the String[] datatype to ArrayList?
Edit
I have an adapter whose constructor takes an ArrayList and a resource string-array that I want to populate it.
If you can make do with a List<String> (and not specifically an ArrayList<String>), you can use:
List<String> list = Arrays.asList(stringArray);
Otherwise, you can do this:
ArrayList<String> list = new ArrayList<String>(Arrays.asList(stringArray));
However, the latter is less efficient (in both time and object creation count) than the other suggested solutions. Its only benefit is that it keeps the code down to one line and is easy to comprehend at a glance.
public ArrayList<String> arrayToArrayList(String[] array){
ArrayList<String> arrayList = new ArrayList<String>(array.length);
for(String string : array){
arrayList.add(string);
}
return arrayList;
}
how about this
import java.util.Collections;
String[] myStringArray = new String[] {"foo", "bar", "baz"};
List myList = new ArrayList(myStringArray.length);
Collections.addAll(myList, myStringArray);

Converting values of SQL Database into a String Array

I need to create a drop down list (spinner) in eclipse for Android whereby the drop down options can be created by the user.
To do this I've created a SQLiteDatabase, which I want to convert into a string array to be put into an array adapter, which can then be fed into the spinner.
Database details are:
DATABASE_NAME = "carddb"
DATABASE_TABLE = "cardtable"
Then the column I wish to read the values from is:
KEY_CARDDETAILS = "_carddetails";
Here is my code so far, adapted from another question on this site (Link: how to get the database value to a String array in android(sqlite Database))
myDB = cardtable.this.openOrCreateDatabase("carddb", MODE_PRIVATE, null);
Cursor cardcursor = cardDB.rawQuery("SELECT * FROM cardtable");
String[] cardstringarray = new String[cardcursor.getCount()];
cardcursor.moveToFirst();
int counter = 0;
while(cardcursor.moveToNext()){
String carddetailss = cardcursor.getString(cardcursor.getColumnIndex("NAME"));
cardstringarray[counter] = carddetailss;
counter++;
}
I'm getting an error under "myDB" and "cardtable", both errors saying they cannot be resolved into a variable!
PLEASE HELP!
Use this code
String[] displayName={};
ArrayList<String> List=new ArrayList<String>();
myDB = cardtable.this.openOrCreateDatabase("carddb", MODE_PRIVATE, null); Cursor cardcursor = cardDB.rawQuery("SELECT * FROM cardtable");
String[] cardstringarray = new String[cardcursor.getCount()];
cardcursor.moveToFirst();
int counter = 0;
while(cardcursor.moveToNext()){
String carddetailss = cardcursor.getString(cardcursor.getColumnIndex("NAME"));
List.add(carddetailss);
counter++;
}
if(List != null){
listEmail=(ListView)findViewById(R.id.listEmail);
listEmail.setVisibility(view.VISIBLE);
displayName=(String[])List.toArray(new String[0]);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, displayName);
// Assign adapter to ListView
listEmail.setAdapter(adapter);
}
This is work for you. Use spinner instead of listView.

Android use NumberFormat for an item of a ListView

I have a String in an Editext that I convert to a float like this :
float montantFac = Float.valueOf(etMontantFac.getText().toString());
I save it in a database and later, I get it back from the DB and I assign it again to this EditText.
etMontantFac.setText(facture.getString(facture.getColumnIndexOrThrow("montant_fa")));
My problem is that it displays some numbers with exponent.
For example, 1000000 is displayed like this : 1e+06
How can I do to ever display the values with numers only (no letter or + or anything else) ?
EDIT :
#Pratik : Thanks, that works fine!
I also need to do the same for an item of a ListView but I don't find how to use your solution in this case. Can you help me ?
Here is my ListView :
private void fillData(){
Cursor cuFactures = db.recupListeFacture(triFactures, argumentWhereVhc, choixVhc, argumentWhereGar, choixGar);
startManagingCursor(cuFactures);
ListAdapter adapter = new SimpleCursorAdapter(this,
R.layout.facture_ligne,
cuFactures,
new String[] {"libelle_fa", "nom_vhc", "montant_fa", "nom_garage", "date_fa"},
new int[] {R.id.listeNomFac, R.id.listeNomVhcFac, R.id.listeMontantFac, R.id.listeNomGarFac, R.id.listeDateFac});
setListAdapter(adapter);
The concerned field is always "montant_fa"
Thank you in advance!
check this link for format
http://download.oracle.com/javase/tutorial/i18n/format/decimalFormat.html
try this way
float val = facture.getFloat(facture.getColumnIndexOrThrow("montant_fa"));
NumberFormat nf = NumberFormat.getNumberInstance(loc);
DecimalFormat df = (DecimalFormat)nf;
df.applyPattern(pattern);
String output = df.format(value);
etMontantFac.setText(output);
I know this is old, but here is for the format data between sqlite and listview in case someone trying to do the same thing. I had similar problem and solved it with that.
here is my example
adapter = new SimpleCursorAdapter(....);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
#Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (columnIndex == cursor.getColumnIndex("columnname")) {
TextView tv = (TextView) view;
NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
tv.setText(nf.format(cursor.getInt(columnIndex)));
// must return true to be applied
return true;
}
return false;
}
});

Categories

Resources