Android cursor charc to array - android

I hope i explain the correctly...
What i want to do is be able to run a query and then get the first record of that query and put the characters into an character array, then i want to get the second query and put that into the same array depending on where it is suppose to be inserted into the array.
So for example...the first record from the query will have the string 'hello', i want this to be inserted into the array at position one. Then i want record two which will be 'you' to be inserted at position 6. so when the character array is out put it will display 'hello you'
How do i go about doing this?
SQLiteDatabase db = dbs.getReadableDatabase();
String SQL = "SELECT * FROM Table";
Cursor cursor = db.rawQuery(SQL, null);
startManagingCursor(cursor);
char charac_array[];
charac_array = new char[10];
while (cursor.moveToNext()) {
???
}

That's what you probably want:
StringBuilder sb = new StringBuilder();
while (cursor.moveToNext()) {
String s = cursor.getString(...);
if (sb.length() > 0) {
sb.append(" ");
}
sb.append(s);
}
String helloYou = sb.toString();
UPDATE
To fill a char array from String
StringBuilder sb = new StringBuilder();
char[] charArray = new char[10];
while (c.moveToNext()) {
String s = getFromCursor(c);
s.getChars(0, 10, charArray, 0); // <-- this copies 10 chars from s to charArray
sb.append(charArray);
}
You'll have to take care when s is less than 10 characters though.

Related

Returning rows from a csv

Hi I have a csv that looks like this:
r1c1|r1c2|r1c3
r2c1|r2c2|r2c3
As you can see it is delimited by the character "|"
In my application, I am trying to explode this using input stream. Here is my code:
String line = "";
String cvsSplitBy = "|";
try {
File initialFile = new File(myfile.txt);
InputStream targetStream = new FileInputStream(initialFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(targetStream));
while ((line = reader.readLine()) != null) {
String[] RowData = line.split(cvsSplitBy);
String c0 = RowData[0];
String c1 = RowData[1];
String c2 = RowData[2];
Toast.makeText(mainactivity.this, c2, Toast.LENGTH_LONG).show();
}
}catch (IOException ex) {
// handle exception
}
Unfortunately, this appears to return each character in the csv as a row. The toast example above returns 1 then 2.
Any ideas how to return the proper column, anyone?
split() splits string around matches of the given regular expression, therefore use of special character (and vertical bar is one of these) requires escaping to strip its "powers".
String cvsSplitBy = "\\|"
See docs: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

how to loop through 2d array and make the result appear in table form in android

HI below is the code which gets the four columns data from the curson and put in the 2d array. basically there are two issues one is that i get the last value as nullnullnullnull means all for columns are fetched as null.
the seconds is that i want to print the array in multitextline or if any other widget availabe so that i get four fields in a row. like
id rule_body rule_con boole
0 abc def 1
0 a f 0
c.moveToFirst();
int i=0;
while(c.moveToNext()) {
String id = c.getString(c.getColumnIndex("id"));
String rb = c.getString(c.getColumnIndex("rule_body"));
String cn = c.getString(c.getColumnIndex("rule_cons"));
String bl = c.getString(c.getColumnIndex("boole"));
table[i][0] = id;
table[i][1] = rb;
table[i][2] = cn;
table[i][3] = bl;
++i;
}
for(int a=0;a<count_row;a++)
for(int b=0;b<count_col;b++) {
obj_ml.append(String.valueOf(table[a][b]));
}
so far i am getting all the result in a single line. any help will be appreciated.
Change your for-loop as below
for (int a=0;a<count_row;a++)
{
for(int b=0;b<count_col;b++)
{
obj_ml.append(String.valueOf(table[a][b]));
}
// add to obj_ml new line character '\n'
obj_ml.append("\n");
}

How can I validate Android edittext for accepting string and integer

What is the validation expression for string(space)integer? I want to enter the data in the format of "month date"(eg.March 22) in database.
I think it'll help you
String abc = "March 2";
String[] split = abc.split(" ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < split.length; i++) {
sb.append(split[i]);
if (i != split.length - 1) {
sb.append(" ");
}
}
String combined = sb.toString();
At 0 position you'll get your months then get into a String and matches with your static array.
And at 1 position, you'll get your date, you can match it too.

Convert Android Cursor to ArrayList of arrays

I try to convert my Cursor data to a arraylist. But at the end all the data in the arraylist is overwrited with the last row. What do i do wrong?
Cursor c = myDbHelper.getLvl1Cata();
String[] data = new String[3];
c.moveToFirst();
while(!c.isAfterLast()) {
data[0] = Integer.toString(c.getInt(0));
data[1] = c.getString(1);
data[2] = Integer.toString(c.getInt(2));
Log.e("cc", data[1]);
catalogueData.add(data);
c.moveToNext();
}
Try this
Cursor c = myDbHelper.getLvl1Cata();
String[] data;
if (c != null) {
while(c.moveToNext()) {
data = new String[3]; // Note this addition
data[0] = Integer.toString(c.getInt(0));
data[1] = c.getString(1);
data[2] = Integer.toString(c.getInt(2));
Log.e("cc", data[1]);
catalogueData.add(data);
}
c.close();
}
data is an array of strings. In the original code, you added the same array to your catalogueData structure several times. You changed the value of the array's contents each time, but it was still the same array object. So you ended up with catalogueData holding several references to a single array, and that array can only have one value for data[0]: the last thing you set it to.
This answer fixes that by using a new and different array for each row in the cursor.
Try this:
if(mycursor!=null){
do{
TextView name = (TextView)view.findViewById(R.id.contact_name);
name.setText(cursor.getString(cursor.getColumnIndex
(Displayname)));
mycursor.moveToNext();
}while (mycursor.isLast());
}
Put String[] data = new String[3]; into the while loop. You're overwriting the array object with each iteration.

StringBuilder related issue

i have following code in which, i am fetching the data from the sqlite database. I am able to get all the values well whenever the data is upgraded but cant able to upgrade the values stored into a StringBuilder.So whenever the data is upgraded StringBuilder show the first data of the data base.
private void showData(Cursor cursor) {
StringBuilder stbuilder = new StringBuilder();
while(cursor.moveToNext())
{
start_time = cursor.getString(1);
end_time = cursor.getString(2);
duration_time = cursor.getString(3);
phone_option = cursor.getString(4);
phone_mode = cursor.getString(5);
stbuilder.append(start_time+" "+end_time+" "+duration_time+" "+phone_option+" "+phone_mode);
Log.i("STARTtttDB", ""+start_time);
Log.i("enddddDB", ""+end_time);
Log.i("duratonnn", ""+duration_time);
Log.i("OpTIONnnnn",""+phone_option);
Log.i("M O D E ",""+phone_mode);
String data=stbuilder.toString();
Log.i("Data OUtput",data);
}
You should create a new StringBuilder for each iteration, or reset it using:
stBuilder.setLength(0).
Also, instead of using string concatenation (+) inside of StringBuilder.append(), you should probably have a series of appends:
stBuilder.append(start_time);
stBuilder.append(" ");
stBuilder.append(end_time);
...
Also note that you can safely forgo the use of StringBuilder altogether as the compiler optimizes string concatenation using StringBuilder anyway. Here is a performance study I found.
You need to reset the StringBuilder with a call to setLength(0);
while(cursor.moveToNext())
{
stBuilder.setLength(0);
.....
}

Categories

Resources