mercredi 23 décembre 2015

Android - Delete and update (GridView) Row in SQLite [on hold]

i have problem with update and delete row on click gridview,how can i resolve it, have this code from tutorial, unfortunately without update and delete function.....hope anyone can help.

MainActivity:

import java.util.ArrayList;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.Toast;

public class MainActivity extends Activity {
   
    private EditText nameEditText;
    private EditText rollEditText;
    private EditText courseEditText;
    private Button insertButton;
    private Button deleteButton;
    private Button updateButton;
    private GridView gridView;
    private ArrayList<String> list;
    private ArrayAdapter<String> adapter;

    DataBaseHandler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        nameEditText=(EditText) findViewById(R.id.NameEditText);
        rollEditText=(EditText) findViewById(R.id.RollEditText);
        courseEditText=(EditText) findViewById(R.id.CourseEditText);
        insertButton=(Button) findViewById(R.id.InsertButton);
        updateButton=(Button) findViewById(R.id.UpdateButton);
        deleteButton=(Button) findViewById(R.id.DeleteButton);
        gridView=(GridView) findViewById(R.id.gridView1);
        
        list=new ArrayList<String>();
        adapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item,list);
        String name, roll, course;
        name="";
        roll="";
        course="";
        handler=new DataBaseHandler(getBaseContext());
        handler.open();
        try {
            Cursor c = handler.DisplayData();
            if (c.moveToFirst()) {
                do {
                    name = c.getString(c.getColumnIndex("name"));
                    roll = c.getString(c.getColumnIndex("roll"));
                    course = c.getString(c.getColumnIndex("course"));
                
                    list.add(name);
                    list.add(roll);
                    list.add(course);
                    gridView.setAdapter(adapter);
                } while (c.moveToNext());
            } else {
                Toast.makeText(getApplicationContext(), "No data found", Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "No data found" + e.getMessage(), Toast.LENGTH_LONG).show();
        }
        handler.close();
        
        insertButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = nameEditText.getText().toString();
                String roll = rollEditText.getText().toString();
                String course = courseEditText.getText().toString();
                handler = new DataBaseHandler(getBaseContext());
                handler.open();
                long id = handler.InsertData(name, roll, course);
                Toast.makeText(getBaseContext(), "Your data in inserted", Toast.LENGTH_LONG).show();
                nameEditText.setText("");
                rollEditText.setText("");
                courseEditText.setText("");
                handler.close();

                list = new ArrayList<String>();
                adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, list);

                name = "";
                roll = "";
                course = "";
                handler = new DataBaseHandler(getBaseContext());
                handler.open();
                try {
                    Cursor c = handler.DisplayData();
                    if (c.moveToFirst()) {
                        do {
                            name = c.getString(c.getColumnIndex("name"));
                            roll = c.getString(c.getColumnIndex("roll"));
                            course = c.getString(c.getColumnIndex("course"));
                            list.add(name);
                            list.add(roll);
                            list.add(course);
                            gridView.setAdapter(adapter);
                        } while (c.moveToNext());
                    } else {
                        Toast.makeText(getApplicationContext(), "No data found", Toast.LENGTH_LONG).show();
                    }
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), "No data found" + e.getMessage(), Toast.LENGTH_LONG).show();
                }
                handler.close();
            }
        });
        updateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
    }
}

DataBaseHandler:

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHandler
{      
    public static final String NAME="name";
    public static final String ROLL="roll";
    public static final String COURSE="course";
    public static final String TABLE_NAME="studenttable";
    public static final String DATABASE_NAME="studentdb";
    public static final int DATABASE_VERSION=1;
 
    public static final String TABLE_CREATE="create table studenttable(name text not null, roll text not null, course text not null);";
    DataBaseHelper dbhelper;
    Context context;
    SQLiteDatabase db;
    public DataBaseHandler(Context ctx)
    {
        this.context=ctx;
        dbhelper=new DataBaseHelper(context);
    }
    private static class DataBaseHelper extends SQLiteOpenHelper
    {       
        public DataBaseHelper(Context ctx)
        {
            super(ctx,DATABASE_NAME,null,DATABASE_VERSION);
        }
        @Override
      
        public void onCreate(SQLiteDatabase db)
        {
            db.execSQL(TABLE_CREATE);
        }
        @Override
        //Called when the database needs to be upgraded.
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXIST studenttable");
            onCreate(db);
        }
    }
    public DataBaseHandler open()
    {           
        db=dbhelper.getWritableDatabase();
        return this;
    }
    public void close()
    {       
        dbhelper.close();
    }

    public long InsertData(String name, String roll,String course)
{        
    ContentValues content=new ContentValues();
    content.put(NAME, name);
    content.put(ROLL, roll);
    content.put(COURSE, course);
    return db.insertOrThrow(TABLE_NAME,null, content);
}       
    public Cursor DisplayData()
    {        
        return db.rawQuery("SELECT * FROM studenttable", null);          
    }   
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:background="#7e0470">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Name"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <EditText
        android:id="@+id/NameEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="5dp"
        android:ems="10"
        android:textColor="#ffffff" />
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/NameEditText"
        android:text="roll"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <EditText
        android:id="@+id/RollEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="5dp"
        android:ems="10"
        android:textColor="#ffffff" />
    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/RollEditText"
        android:text="course"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <EditText
        android:id="@+id/CourseEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="5dp"
        android:ems="10"
        android:textColor="#ffffff">
        <requestFocus />
    </EditText>

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/CourseEditText"
        android:layout_marginTop="10dp"
        android:id="@+id/linearLayout">

        <Button
            android:id="@+id/InsertButton"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:text="Insert" />

        <Button
            android:id="@+id/UpdateButton"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_marginLeft="4dp"
            android:text="Update" />

        <Button
            android:id="@+id/DeleteButton"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_marginLeft="4dp"
            android:text="Delete" />


    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout"
        android:layout_alignParentBottom="true"
        android:background="#fb57cf">
        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="Name"
            android:textAppearance="?android:attr/textAppearanceLarge" />
        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="150dp"
            android:text="roll"
            android:textAppearance="?android:attr/textAppearanceLarge" />
        <TextView
            android:id="@+id/textView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:text="course"
            android:layout_marginRight="50dp"
            android:textAppearance="?android:attr/textAppearanceLarge" />
        <GridView
            android:id="@+id/gridView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/textView4"
            android:fastScrollAlwaysVisible="true"
            android:fastScrollEnabled="true"
            android:numColumns="3">
        </GridView>
    </RelativeLayout>
</RelativeLayout>

Aucun commentaire:

Enregistrer un commentaire