Android MatrixCursor - android

I am trying to get a basic matrix cursor running, but for some reason the list remains empty. Would appreciate if someone could spot what may be wrong with the code?
String []columns = new String[] {"_id","Title","Desc"} ;
MatrixCursor mc = new MatrixCursor(columns);
startManagingCursor(mc);
for(int i=0; i< 200;i++)
{
mc.addRow(new Object[]{i, "test"+i ,"..."});
}
ListView lv = (ListView)findViewById(R.id.alist);
int [] r = new int[1];
r[0] = R.id.atext; // the id of the textview in test_list_item layout
String [] s= new String[1];
s[0] = mc.getColumnName(1); // the column to be used "Title"
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,android.R.layout.test_list_item,mc,s,r);
lv.setAdapter(adapter);

The reason the layout was not read was due to the incorrect layout being called:
SimpleCursorAdapter(this,android.R.layout.list_item,mc,s,r);
Instead of:
SimpleCursorAdapter(this,R.layout.my_list_item,mc,s,r);

Related

get value from Dynamically add multiple spinner in android?

How to get value to dynamically add multiple spinner where spinner id is same.i used 'String spin = parent.getSelectedItem().toString();' but i get all time last spinner value.Plz help me?
you can create array list of spinner
ArrayList<Spinner> listSpinner = new ArrayList<>();
listSpinner.add(spinner1);
listSpinner.add(spinner2);
listSpinner.add(spinner3);
for(int i=0;i<listSpinner.size();i++){
String p1 = listSpinner.get(i).getSelectedItem();
}
get different value for all spinner out side for loop and store it in arraylist.
ArrayList<Spinner> listSpinner = new ArrayList<>();
ArrayList<ArrayList<String>> s‌​tatus_list = new ArrayList<>();
status_data.add("" + snapshot.getValue());// getting data
s‌​tatus_list.add(status_data);
for (int i = 0; i < 10; i++) {
View v = LayoutInflater.from(AssetCodingDetails.this).inflate(R.layou‌​t.custom_asset_codin‌​g_label, null);
Spinner spinner_status = (Spinner) v.findViewById(R.id.spinner_status);
ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), R.layout.spinner_item, s‌​tatus_list.get(i));
spinner_status.setAdapter(adapter);
listSpinner.add(spinner_status);
}
for(int i=0;i<listSpinner.size();i++){
String p1 = listSpinner.get(i).getSelectedItem().toString();
}

Can I populate a listView with a nested array using Simpleadapter and hashmap?

I'm trying to populate a ListView using a HashMap and a SimpleAdapter from a nested array with 12 columns and 12 rows
So here is my code
try {
String [][] array2 = new String[cursor.getCount()][cursor.getColumnCount()];
cursor.moveToFirst();
for (int i =0; i<cursor.getCount();i++){
for(int j = 0; j < cursor.getColumnNames().length; j++) {
String uname = cursor.getString(j);
array2[i][j]=uname;
}
cursor.moveToNext();
}
} finally {
cursor.close();
}
and then the simple adapter with hashmap
int[] to = new int[]{R.id.item2, R.id.item3, R.id.item4, R.id.item5, R.id.item6, R.id.item7, R.id.item8 ,R.id.item9, R.id.item10, R.id.item11, R.id.item12};
List<HashMap<String,String>> fillMaps = (List<HashMap<String, String>>) new ArrayList<HashMap<String, String>>();
for(int i = 0; i<10;i++){
HashMap<String,ArrayList> map = new HashMap<String, ArrayList>();
}
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, array2, to);
when I try this I get an error
SimpleAdapter(android.content.Context, java.util.List<? extends java.util.Map<java.lang.String,?>>, int, java.lang.String[], int[])' in android.widget.SimpleAdapter cannot be applied to
(com.example.thevenom1215.prueba.MainActivity, java.util.List<java.util.HashMap<java.lang.String, java.lang.String>>, int, java.util.ArrayList<java.lang.String>, int[])
Or I thought that I could convert the nested array to a simple array is that possible? If so how can I accomplish that? I tried this
String from [] = new String from[252]:
from[0]= array[0][1]+"|"+array[0][2]+"|"+array[0][3]+......+array[0][12];
but it doesn't work.
In the array2, which is array2[12][21], every row has 12 columns, which are information of a person, for example (name, age)
Cesar Joel Gurrola, xx
The first row of the array is: [Cesar, Joel, Gurrola, xx]
I need that in an array because further in my code I need String by String and not a whole String "Cesar, Joel, Gurrola, xx"
Sql query
sql="select b.CVE_CONTR, r.NO_RECIBO , a.NOM_SOLICIT ,r.NO_PARCIAL ,r.SDO_TOTAL, r.STS_PAGO ,a.AP_PATSOLICIT,a.AP_MATSOLICIT, " +
"f.DESCRIPCION, b.NO_PARCIALID , b.PAGO_PARCIAL, b.TOT_APAG from MFRF_M_SOLICITANTES a, MFRF_M_CONTPREV_H b, MFRF_M_CONTPREV_D_RECGEN_2 r," +
"C_PAQUETE f , C_PARCIALIDADES g, MFRF_C_COLONIAS c where b.CVE_CONTR = '"+etnumcontrato.getText().toString() + "' and r.STS_PAGO in ('1','10','11','12')" +
"and c.ID_COLONIA = a.ID_COLONIA and f.ID_PAQUETE = b.ID_PAQUETE and g.ID_PARCIALIDAD = b.ID_PARCIAL AND a.ID_SOLICIT = b.ID_SOLICITANTE ";
Overall, I wouldn't recommend this.
String [][] array2 = new String[cursor.getCount()][cursor.getColumnCount()];
An adapter displays one row-item at a time, not the whole table.
I would instead recommend a CursorAdapter since you do have a Cursor object, but I'll continue with a SimpleAdapter.
First, you need to create an XML layout, let it be named item_layout.xml. It contains all the Views that you want to bind the row from the database to. I believe these must all be TextViews for a SimpleAdapter.
String[] from = new String[] {"rowid", "fname", "mname", "lname"};
int[] to = new int[] { R.id.item1, R.id.item2, R.id.item3, R.id.item4 };
Then, loop over the rows of the cursor, and pull out the necessary information into a List of HashMaps. Note: this is one row of data each time that we are looping over the cursor results. And, as described earlier, you only have TextViews, so you can only store String values.
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
final int rows = cursor.getCount();
for (int i = 0; i < rows; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("rowid", "" + i);
map.put("fname", cursor.getString(cursor.getColumnIndexOrThrow("fname"));
map.put("mname", cursor.getString(cursor.getColumnIndexOrThrow("mname"));
map.put("lname", cursor.getString(cursor.getColumnIndexOrThrow("lname"));
fillMaps.add(map);
cursor.moveToNext();
}
Now that you have looped over all the rows of the data and are satisfied with that data, you just set the adapter.
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps,
R.layout.item_layout,
from, to);
listView.setAdapter(adapter);

Android how to trim sharedpreferences name

I'm getting all sharedpreferences created by my app and I insert them in a spinner but every sharedpreferences displayed like "example.xml" etc. I want to trim .xml extension let them just names like "example".
File prefsdir = new File(getApplicationInfo().dataDir,"shared_prefs");
if(prefsdir.exists() && prefsdir.isDirectory()){
String[] list = prefsdir.list();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, android.R.id.text1,list);
Spinner sp = (Spinner) findViewById(R.id.tum_ilaclar);
sp.setAdapter(adapter);
}
You can use something like this create a new method:
String[] getList(String[] list){
String values[] = new String[list.length];
for(int i=0; i<list.length; i++){
String value = list[i];
values[i]=value.contains(".xml")?value.replace(".xml",""):value;
}
return values;
}
Then in your code change :
String[] list = prefsdir.list();
to
String[] list = getList(prefsdir.list());
This will remove the .xml part.

Create dynamic ListPreference (Android)

I want to create a ListPreference in my settings that is populated with the different "years" that are stored in my SQLite database. I've gotten it to add all the years, however it adds them more than once if there is more than one of them. I cannot figure out how to make each appear only once (ex. 2012,2011,2010. instead of 2012,2012,2012,2011,2010,2010). Heres my code:
ListPreference listPreferenceCategory = (ListPreference) findPreference("Filter");
entries = new ArrayList<String>();
values = new ArrayList<String>();
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToWrite();
cursor = mySQLiteAdapter.queueAll6();
cursor.moveToFirst();
for(int i = 0; i < cursor.getCount();i++)
{
String year = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.YEAR));
if(Arrays.asList(entries).contains(year) == false){
entries.add(year);
values.add(year);
}
cursor.moveToNext();
}
final CharSequence[] charSequenceItems = entries.toArray(new CharSequence[entries.size()]);
final CharSequence[] charSequenceItems2 = values.toArray(new CharSequence[values.size()]);
listPreferenceCategory.setEntries(charSequenceItems);
listPreferenceCategory.setEntryValues(charSequenceItems2);
mySQLiteAdapter.close();
figured it out. I needed to use String[] array = entries.toArray(new String[entries.size()]); and then Arrays.asList(array).contains(year) == false

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.

Categories

Resources