vendredi 24 juillet 2015

Android 5 SQLIte encoding

I have problem with SQLite in new Android 5 version (build 22). When I insert my data and read data from it, i get problems with encoding, shortly logcat cannot show it. I tried to show another text, even in russian language, it works well, so the problem in sqlite, I guess. How can I fix it? here is the code, of course:

package com.example.bermud06.p036_1sqlitequery;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;


    public class MainActivity extends Activity implements View.OnClickListener {

        final String LOG_TAG="myLogs";
       String name[]={"Китай","США","Бразилия","Россия",
        "Япония","Германия","Египет","Италия","Франция","Канада"};

      //  String name[]={"China","USA","Brasil","Russia",
      //          "Japon","Germany","Egypt","Italy","France","Canade"};
        int people[]={1400,311,195,142,128,82,80,60,66,35};
        String region[]={"Азия","Америка","Америка","Европа","Азия","Европа","Африка","Европа","Европа","Америка"}  ;
      //  String region[]={"Asia","America","America","Europe","Asia","Europe","Africa","Europe","Europe","America"};

        Button btnAll,btnFunc,btnPeople,btnSort,btnGroup,btnHaving;
        EditText etFunc,etPeople, etRegionPeople;
        RadioGroup rgSort;


        DBHelper dbHelper;
        SQLiteDatabase db;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            btnAll =(Button) findViewById(R.id.btnAll);
            btnFunc=(Button) findViewById(R.id.btnFunc);
            btnGroup=(Button) findViewById(R.id.btnGroup);
            btnHaving=(Button) findViewById(R.id.btnHaving);
            btnPeople=(Button) findViewById(R.id.btnPeople);
            btnSort=(Button) findViewById(R.id.btnSort);

            btnAll.setOnClickListener(this);
            btnFunc.setOnClickListener(this);
            btnGroup.setOnClickListener(this);
            btnHaving.setOnClickListener(this);
            btnPeople.setOnClickListener(this);
            btnSort.setOnClickListener(this);

            etFunc=(EditText)findViewById(R.id.etFunc);
            etRegionPeople=(EditText)findViewById(R.id.etRegionPeople);
            etPeople=(EditText)findViewById(R.id.etPeople);

            rgSort=(RadioGroup)findViewById(R.id.rgSort);

            dbHelper=new DBHelper(this);

            db=dbHelper.getWritableDatabase();

            Cursor c=db.query("mytable",null,null,null,null,null,null);

            if (c.getCount()==0)
            {
                ContentValues cv=new ContentValues();

                for (int i=0;i<name.length;i++){
                    cv.put("name",name[i]);
                    cv.put("people",people[i]);
                    cv.put("region",region[i]);
                    Log.d(LOG_TAG,"id = "+db.insert("mytable",null,cv));
                }

            }
            c.close();
            dbHelper.close();

            onClick(btnAll);


        }

        @Override
        public void onClick(View v) {

            db=dbHelper.getWritableDatabase();

            String sFunc=etFunc.getText().toString();
            String sPeople=etPeople.getText().toString();
            String sRegionPeople=etRegionPeople.getText().toString();

            String[] columns=null;
            String selection=null;
            String[] selectionargs=null;
            String groupBy=null;
            String having=null;
            String orderBy=null;

            Cursor c=null;

            switch (v.getId())
            {
                case R.id.btnAll:
                    Log.d(LOG_TAG,"--- Проверка ---");
                    c=db.query("mytable",null,null,null,null,null,null);
                    break;

                case R.id.btnFunc:
                    Log.d(LOG_TAG,"--- ??????? "+sFunc+" ---");
                    columns=new String[]{sFunc};
                    c=db.query("mytable",columns,null,null,null,null,null);
                    break;

                case R.id.btnPeople:

                    Log.d(LOG_TAG,"--- ????????? ?????? "+sPeople+" ---");
                    selection="people > ?";
                    selectionargs=new String[]{sPeople};

                    c=db.query("mytable",null,selection,selectionargs,null,null,null);
                    break;
                case R.id.btnGroup:

                    Log.d(LOG_TAG,"--- ????????? ?? ??????? ---");
                    columns=new String[]{"region","sum(people) as people"};
                    groupBy="region";
                    c=db.query("mytable",columns,null,null,groupBy,null,null);
                    break;
                case R.id.btnHaving:
                    Log.d(LOG_TAG,"--- ?????? ? ?????????? ?????? "+sRegionPeople+" ---");
                    columns=new String[]{"region","sum(people) as people"};
                    groupBy="region";
                    having="sum(people) > "+sRegionPeople;
                    c=db.query("mytable",columns,null,null,groupBy,having,null);
                    break;

                case R.id.btnSort:
                    switch (rgSort.getCheckedRadioButtonId())
                    {
                        case R.id.rName:
                            Log.d(LOG_TAG,"--- ?????????? ?? ???????????? ---");
                            orderBy="name";
                            break;

                        case R.id.rPeople:
                            Log.d(LOG_TAG,"--- ?????????? ?? ????????? ---");
                            orderBy="people";
                            break;
                        case R.id.rRegion:
                            Log.d(LOG_TAG,"--- ?????????? ?? ??????? ---");
                            orderBy="region";
                            break;
                    }

                    c=db.query("mytable",null,null,null,null,null,orderBy);
                    break;

            }
            if (c!=null)
            {
                if (c.moveToFirst())
                {
                    String str;

                    do {
                        str="";
                        for (String cn : c.getColumnNames()){
                            str=str.concat(cn+" = "+
                            c.getString(c.getColumnIndex(cn))+"; ");
                        }
                        Log.d(LOG_TAG,str);

                    }while (c.moveToNext());

                }
                c.close();
            } else {
                Log.d(LOG_TAG,"Cursor is null");

                dbHelper.close();
            }
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }

        class DBHelper extends SQLiteOpenHelper{

            public DBHelper(Context cntx){
                super(cntx,"myDB",null,1);
            }
            @Override
            public void onCreate(SQLiteDatabase db) {
                Log.d(LOG_TAG,"--- onCreate database ---");

                try{
                db.execSQL("create table mytable (" +
                        "id integer primary key autoincrement,"+"name text," +
                        "people integer,"+"region text"+");");
                    }
                catch (Exception e)
                {
                    Log.d(LOG_TAG,e.toString());
                }
            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

            }
        }
    }

Aucun commentaire:

Enregistrer un commentaire