0

Am working for first time with SQLite on android and i have some trouble , so what i want to do is insert a date into my DB,

then onCLickLinsten to insert then retrieve the data to sent them to another activity, just tryed this to understand and practice it, here is the second part of my code:

public class MainActivity extends AppCompatActivity {

private RequestQueue requestQueue;
private static final String URL = "http://192.168.1.104/sync_adapter.php";
private StringRequest request;

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

    SQLiteDatabase myDB= null;
    final String TableName = "Personne";
    final String[] name1 = new String[1];

    final EditText edi1 =  (EditText) findViewById(R.id.edittext1);
    final TextView text1 =  (TextView) findViewById(R.id.textview1);
    Button btn =  (Button) findViewById(R.id.button);
    Button btn2 =  (Button) findViewById(R.id.button2);

    myDB = this.openOrCreateDatabase("namesDB", MODE_PRIVATE, null);
    myDB.execSQL("CREATE TABLE IF NOT EXISTS "
            + TableName
            + " (id  INT(3),name VARCHAR );");

    final SQLiteDatabase finalMyDB = myDB;
    btn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            final String text2 = edi1.getText().toString();

            requestQueue = Volley.newRequestQueue(MainActivity.this);
            request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    JSONArray contacts = null;
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(response);
                        contacts = jsonObject.getJSONArray("result");
                            JSONObject c = contacts.getJSONObject(0);
                            final String  name = c.getString("nom");

                        Toast.makeText(MainActivity.this, "Mr "+name+" ID: "+text2, Toast.LENGTH_SHORT).show();
                        text1.setText(""+name);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                }
            },
                    new Response.ErrorListener() {
                public void onErrorResponse(VolleyError error) {

                }
            }){

                protected Map<String, String> getParams() throws AuthFailureError {
                    HashMap<String,String> hashMap = new HashMap<String, String>();
                    hashMap.put("text2",text2);

                    return hashMap;
                }
            };

            requestQueue.add(request);

        }
    });



    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            finalMyDB.execSQL("INSERT INTO "
                    + TableName
                    + " (id, name)"
                    + " VALUES ('text2' ,'name');");

            Cursor c = finalMyDB.rawQuery("SELECT name FROM " + TableName , null);
                int Column1 = c.getColumnIndex("name");

            c.moveToFirst();
            if (c != null) {
                // Loop through all Results
                do {
                     name1[0] = c.getString(Column1);
                }
                while(c.moveToNext());
            }


            Intent i = new Intent(MainActivity.this,ResultActivity.class);
            i.putExtra("name1", name1[0]);
            startActivity(i);


        }
    });
}

am i doing it right ? thank's for help

7
  • Is your codes working correctly ? Commented May 25, 2016 at 14:00
  • having a crash in the second activity cosed by an error in my ListView , but for this part is it all good ? the part where i insert and retrieve ? thanks Commented May 25, 2016 at 14:14
  • Post the crash log as it is, and inform ! Commented May 25, 2016 at 14:16
  • i fixed the first error , now i got this Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView Commented May 25, 2016 at 14:22
  • then you are not posting the relevant codes, Post the related codes... Somewhere you are trying to cast LinearLayout into TextView. Commented May 25, 2016 at 14:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.