单击按钮时如何刷新gridview

时间:2018-07-04 02:14:59

标签: java android sqlite android-gridview

当我单击删除按钮时,我想在SQLite和gridview中删除数据。

但仅在两次按下按钮时有效。

尝试添加adapter.notifyDataSetChanged();gridView.invalidateViews();,但相同。

我想知道是deleteButton.setOnClickListener是问题还是SetGridView()

感谢您的帮助。谢谢

MainActivity.java

public class MainActivity extends Activity {
//Data members
private EditText nameEditText;
private EditText rollEditText;
private EditText courseEditText;
private Button insertButton;
private Button deleteButton;

//object of DatabaseHandler class
DatabaseHandler handler;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    SetGridView();
    nameEditText = (EditText) findViewById(R.id.NameEditText);
    rollEditText = (EditText) findViewById(R.id.RollEditText);
    courseEditText = (EditText) findViewById(R.id.CourseEditText);
    insertButton = (Button) findViewById(R.id.InsertButton);
    deleteButton = (Button) findViewById(R.id.DeleteButton);

    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());//getting the context object
            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();
            SetGridView();
        }
    });

    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());//getting the context object
            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();
            SetGridView();
        }
    });

    deleteButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View w) {
            handler = new DatabaseHandler(getBaseContext());
            handler.open();
            handler.clearTable();
            handler.close();
            adapter.notifyDataSetChanged();
            gridView.setAdapter(adapter);
            gridView.invalidateViews();
            SetGridView();
        }
    });

    super.onCreate(savedInstanceState);
}


public void SetGridView() {
    //GridView
    gridView = (GridView) findViewById(R.id.gridView1);
    //ArrayList
    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());//getting the context object
    handler.open();
    try {
        //for holding retrieve data from query and store in the form of rows
        Cursor c = handler.DisplayData();
        //Move the cursor to the first row.
        if (c.moveToFirst()) {
            do {
                name = c.getString(c.getColumnIndex("name"));
                roll = c.getString(c.getColumnIndex("roll"));
                course = c.getString(c.getColumnIndex("course"));
                //add in to array list
                list.add(name);
                list.add(roll);
                list.add(course);
                gridView.setAdapter(adapter);
            } while (c.moveToNext());//Move the cursor to the next row.
        } 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();
    }

private GridView gridView;
private ArrayList<String> list;
private ArrayAdapter<String> adapter;
}

DatabaseHandler.java

public class DatabaseHandler
{
//Variables
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;
//Table query
public static final String TABLE_CREATE="create table studenttable(name text not null, roll text not null, course text not null);";
public static final String TABLE_DELETE="DROP TABLE studenttable;";
DataBaseHelper dbhelper;
Context context;
SQLiteDatabase db;
public DatabaseHandler(Context ctx)
{
    this.context=ctx;
    dbhelper=new DataBaseHelper(context);
}

public static class DataBaseHelper extends SQLiteOpenHelper
{
    //Create a helper object to create, open, and/or manage a database.
    public DataBaseHelper(Context ctx)
    {
        super(ctx,DATABASE_NAME,null,DATABASE_VERSION);
    }
    @Override
    //Called when the database is created for the first time.
    public void onCreate(SQLiteDatabase db)
    {
        db.execSQL(TABLE_CREATE);//Here create a table
    }
    @Override
    //Called when the database needs to be upgraded.
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS studenttable;");
        onCreate(db);
    }
}
public void clearTable()
{
    db.execSQL("DELETE FROM studenttable;");
}


public DatabaseHandler open()
{
    //Create and/or open a database that will be used for reading and writing.
    db=dbhelper.getWritableDatabase();
    return this;
}


public void close()
{
    //Close any open database object.
    dbhelper.close();
}
//Insert record in the database
public long InsertData(String name, String roll,String course)
{
    //This class is used to store a set of values
    ContentValues content=new ContentValues();
    content.put(NAME, name);
    content.put(ROLL, roll);
    content.put(COURSE, course);//Adds a value to the set.
    return db.insertOrThrow(TABLE_NAME,null, content);
}
//Display record from the database
public Cursor DisplayData()
{
    //Select query
    return db.rawQuery("SELECT * FROM studenttable", null);
    //return db.query(TABLE_NAME, new String[]{NAME, ROLL,COURSE}, null, null, null, null, null);
}
}

0 个答案:

没有答案