i'm new to android and i'm working on a application of student record where i have added the record successfully but i'm having issues in updating and deleting data from data base .
As i update the data in database it should also get updated in my list view that is on the main screen show only the roll no and other fields show when i click on roll no.
When i add a new record it appears successfully in listview and roll no. appears on the listview . unable to do the update and delete operations.
Listview Fragment is
public class TopRatedFragment extends Fragment {
Intent i;
TextView studentid;
Button Bite;
dbbase data;
Cursor mcursor;
ListView List;
Context context;
Simpleadapter adapter;
int nr = 0;
ActionMode mActionMode;
String valstudentid;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_top_rated,
container, false);
dbbase data = new dbbase(getActivity());
ArrayList<HashMap<String, String>> studentList = data.getAllstudent();
if (studentList.size() != 0) {
List = (ListView) rootView.findViewById(R.id.listview1);
adapter = new Simpleadapter(getActivity(), studentList,
R.layout.viewrecord,
new String[] { "studentId", "rollno" }, new int[] {
R.id.studentId, R.id.recordroll });
// SimpleAdapter adapter = new SimpleAdapter(
// getActivity(),studentList, R.layout.viewrecord, new String[] {
// "studentId","rollno"}, new int[] {R.id.studentId,
// R.id.recordroll});
List.setAdapter(adapter);
List.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getActivity().getApplicationContext(),
"clicked" + position, Toast.LENGTH_SHORT).show();
}
});
List.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
List.setMultiChoiceModeListener(new MultiChoiceModeListener() {
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
adapter.clearSelection();
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
nr = 0;
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode,
MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.add:
nr = 0;
studentid = (TextView) getActivity().findViewById(
R.id.studentId);
String valstudentid = studentid.getText().toString();
Intent it = new Intent(getActivity(), edit.class);
it.putExtra("studentId", valstudentid);
getActivity().startActivity(it);
break;
case R.id.action_settings:
break;
case R.id.update:
break;
}
return false;
}
@Override
public void onItemCheckedStateChanged(ActionMode mode,
int position, long id, boolean checked) {
// TODO Auto-generated method stub
if (checked) {
nr++;
// mAdapter.setNewSelection(position, checked);
} else {
nr--;
// mAdapter.removeSelection(position);
}
mode.setTitle(nr + " selected");
}
});
List.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
List.setItemChecked(position,
!adapter.isPositionChecked(position));
return false;
}
});
}
return rootView;
}
}
My database class
public class dbbase extends SQLiteOpenHelper {
private static final String meritech = null;
SQLiteDatabase db = getWritableDatabase();
public dbbase(Context applicationcontext) {
super(applicationcontext, "androidsqlite.db", null, 1);
Log.d(meritech, "Created");
}
Context mcontext;
TopRatedFragment frag = new TopRatedFragment();
public void resetdatabase() {
db.execSQL("DROP TABLE IF EXISTS student");
onCreate(db);
}
public void deletedatabase() {
mcontext.deleteDatabase("student");
db.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
// create database
db.execSQL("" + "CREATE TABLE IF NOT EXISTS student ("
+ "studentid INTEGER PRIMARY KEY , " + "rollno VARCHAR, "
+ "name TEXT ," + "marks VARCHAR," + "class VARCHAR);");
Log.d(meritech, "created table ");
}
public void insertstudent(HashMap<String, String> queryValues) {
ContentValues Values = new ContentValues();
Values.put("rollno", queryValues.get("rollno"));
Values.put("name", queryValues.get("name"));
Values.put("class", queryValues.get("class"));
Values.put("marks", queryValues.get("marks"));
db.insert("student", null, Values);
db.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
String query;
query = "DROP TABLE IF EXISTS student";
db.execSQL(query);
onCreate(db);
}
public void deleteAnimal(String id) {
Log.d(meritech, "delete");
SQLiteDatabase database = this.getWritableDatabase();
String deleteQuery = "DELETE FROM students where studentId='" + id
+ "'";
Log.d("query", deleteQuery);
database.execSQL(deleteQuery);
}
public int updatestudent(HashMap<String, String> queryValues) {
ContentValues values = new ContentValues();
values.put("rollno", queryValues.get("rollno"));
values.put("name", queryValues.get("name"));
values.put("class", queryValues.get("class"));
values.put("marks", queryValues.get("marks"));
return db.update("student", values, "studentId" + "= ?",
new String[] { String.valueOf("studentId") });
}
public void deletestudent(long id) {
Log.d(meritech, "delete");
String deleteQuery = "DELETE FROM student where student='" + id + "'";
Log.d("query", deleteQuery);
db.execSQL(deleteQuery);
}
public ArrayList<HashMap<String, String>> getAllstudent() {
ArrayList<HashMap<String, String>> wordList;
wordList = new ArrayList<HashMap<String, String>>();
String selectQuery = "SELECT * FROM student";
Cursor c = db.rawQuery(selectQuery, null);
if (c.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put("studentId", c.getString(0));
map.put("rollno", c.getString(1));
// map.put("name", c.getString(2));
// map.put("class", c.getString(3));
// map.put("marks", c.getString(4));
wordList.add(map);
} while (c.moveToNext());
}
return wordList;
}
public ArrayList<HashMap<String, String>> viewall() {
ArrayList<HashMap<String, String>> wordList1;
wordList1 = new ArrayList<HashMap<String, String>>();
String selectQuery = "SELECT * FROM student";
Cursor c = db.rawQuery(selectQuery, null);
if (c.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put("studentId", c.getString(0));
map.put("rollno", c.getString(1));
map.put("name", c.getString(2));
map.put("class", c.getString(3));
map.put("marks", c.getString(4));
wordList1.add(map);
} while (c.moveToNext());
}
return wordList1;
}
public HashMap<String, String> getstudentInfo(String id) {
// TODO Auto-generated method stub
HashMap<String, String> wordList = new HashMap<String, String>();
SQLiteDatabase database = this.getReadableDatabase();
String selectQuery = "SELECT * FROM student where studentId='" + id
+ "'";
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
// HashMap<String, String> map = new HashMap<String, String>();
wordList.put("rollno", cursor.getString(1));
wordList.put("name", cursor.getString(2));
wordList.put("Class", cursor.getString(3));
wordList.put("marks", cursor.getString(4));
// wordList.add(map);
} while (cursor.moveToNext());
}
return wordList;
}
}
and My editRecord class
public class edit extends Activity {
EditText roll, name, Class, marks;
Button save;
dbbase data;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.editrecord);
data = new dbbase(getApplicationContext());
save = (Button) findViewById(R.id.buteditrecordsave);
roll = (EditText) findViewById(R.id.editrecordRollno);
name = (EditText) findViewById(R.id.editrecordName);
Class = (EditText) findViewById(R.id.editrecordclass);
marks = (EditText) findViewById(R.id.editrecordMarks);
Intent objIntent = getIntent();
String studentId = objIntent.getStringExtra("studentId");
Log.d("Reading: ", "Reading all contacts..");
HashMap<String, String> studentList = data.getstudentInfo(studentId);
Log.d("name", studentList.get("rollno"));
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
HashMap<String, String> queryValues = new HashMap<String, String>();
queryValues.put("studentid", studentId);
queryValues.put("roll", roll.getText().toString());
queryValues.put("name", name.getText().toString());
queryValues.put("class", Class.getText().toString());
queryValues.put("marks", marks.getText().toString());
data.updatestudent(queryValues);
Intent objIntent1 = new Intent(edit.this, MainActivity.class);
startActivity(objIntent1);
}
});
}
}
I'm applying all these operation on onitemlongclicklistner on listview as the button appears in contextual action bar
My list adapter
public class Simpleadapter extends SimpleAdapter {
private HashMap<Integer, Boolean> mSelection = new HashMap<Integer, Boolean>();
public Simpleadapter(Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
// TODO Auto-generated constructor stub
}
public void setNewSelection(int position, boolean value) {
mSelection.put(position, value);
notifyDataSetChanged();
}
public boolean isPositionChecked(int position) {
Boolean result = mSelection.get(position);
return result == null ? false : result;
}
public Set<Integer> getCurrentCheckedPosition() {
return mSelection.keySet();
}
public void removeSelection(int position) {
mSelection.remove(position);
notifyDataSetChanged();
}
public void clearSelection() {
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);// let the adapter
// handle
// setting up
// the row views
// v.setBackgroundColor(getgetResources().getColor(android.R.color.background_light));
// //default color
v.setBackgroundColor(Color.WHITE);
if (mSelection.get(position) != null) {
v.setBackgroundColor(mSelection.get(position) ? 0x9934B5E4
: Color.TRANSPARENT);
// this is a selected position so make it red
}
return v;
}
}
Aucun commentaire:
Enregistrer un commentaire