I wanna delete items in my listview by clicking a button in my list view item. This works but i dont know how to refresh the listview after i clicked the button. can anyone help me?
here is some of my code
public class ActionsListView extends AppCompatActivity {
private Button deleteBtn, editBtn, addBtn;
private ListView listView;
DBAdapter myDb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_actions_list_view);
deleteBtn = (Button) findViewById(R.id.deleteBtn);
editBtn = (Button) findViewById(R.id.editBtn);
addBtn = (Button) findViewById(R.id.alv_AddBtn);
openDB();
populateListView();
}
private void openDB(){
myDb = new DBAdapter(this);
myDb.open();
}
private void populateListView(){
Cursor cursor = myDb.getAllRows();
String [] fromFieldNames = new String[] {DBAdapter.KEY_ACTION_NAME, DBAdapter.KEY_ACTION_DURATION};
int [] toViewIDs = new int[]{R.id.itemActionDisplay, R.id.itemDurationDisplay};
MySimpleCursorAdapter myCursorAdapter;
myCursorAdapter = new MySimpleCursorAdapter(getBaseContext(), R.layout.item_action, cursor, fromFieldNames, toViewIDs, 0);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(myCursorAdapter);
}
My custom simplecursoradapter
public MySimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
#Override
public View newView(Context _context, Cursor _cursor, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) _context.getSystemService(_context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item_action, parent, false);
return view;
}
#Override
public void bindView(View view, final Context context, Cursor cursor) {
String actionName = cursor.getString(cursor.getColumnIndex(DBAdapter.KEY_ACTION_NAME));
String actionDuration = cursor.getString(cursor.getColumnIndex(DBAdapter.KEY_ACTION_DURATION));
TextView actionNameTxtField = (TextView) view.findViewById(R.id.itemActionDisplay);
TextView actionDurationTxtField = (TextView) view.findViewById(R.id.itemDurationDisplay);
actionNameTxtField.setText(actionName);
actionDurationTxtField.setText(actionDuration);
Button yourButton = (Button) view.findViewById(R.id.deleteBtn);
yourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (view != null) {
DBAdapter myDb = new DBAdapter(context);
myDb.open();
myDb.deleteAll();
notifyDataSetChanged();
}
}
});
}
Related
Here is the code I am using to show a custom ListView using simple CursorAdapter
I am using this code to show cart items, and I want to add button in ListView
As I am new to android development I'm not able to figure out what the problem is
myDbHelper.openDataBase();
final Cursor ictemp = myDbHelper.getOrdredItems(myDbHelper);
if (ictemp != null) {
ictemp.moveToFirst();
count = ictemp.getCount();
Log.d("count", "count===" + count);
String[] from = new String[] { "item_name", "item_rate", "qty",
"unit" };
int[] to = new int[] { R.id.tv_Name, R.id.tv_Rate, R.id.et_qty,
R.id.tv_unit };
final SimpleCursorAdapter sc = new SimpleCursorAdapter(this,
R.layout.list_row2, ictemp, from, to, 0);
final Cursor crs = myDbHelper.getTotal(myDbHelper);
if (crs != null) {
crs.moveToFirst();
String total = crs.getString(0);
Gtotal.setText("Rs." + total);
tvcount.setText("" + count);
}
lv.setAdapter(sc);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view,
final int pos, long arg3) {
remove = (Button) arg0.findViewById(R.id.btn_remove);
switch (arg0.getId()) {
case R.id.btn_remove:
Toast.makeText(ctx, "Clicked on " + pos,
Toast.LENGTH_SHORT).show();
break;}
}
});
One solution is to create a CustomAdapter that extends SimpleCursorAdapter.
Then override bindView method
In bindView, find the Button then handle onClickEvent
public class CustomAdapter extends SimpleCursorAdapter {
private Context mContext;
private Context appContext;
private int layout;
private Cursor cr;
private final LayoutInflater inflater;
public CustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.layout = layout;
this.mContext = context;
this.inflater = LayoutInflater.from(context);
this.cr = c;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(layout, null);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
...
TextView tv_Name = (TextView) view.findViewById(R.id.tv_Name);
tv_Name.setText(...);
...
Button btnRemove = (Button) view.findViewById(R.id.btn_remove);
btnRemove.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// button click
// Remove your item here
}
});
}
}
Use it in Activity by
final CustomAdapter sc = new CustomAdapter(this,R.layout.list_row2,ictemp, from, to, 0);
lv.setAdapter(sc)
Hope this helps
I am new to android, In my application i have a list view with image,textview's and button.when i click the button in the listview all the data's of the particular items are stored to the sqlite db and also to the webservice then i get a response string from the webservice, i updated this string in to my one of the field of sqlite DB.here i need to set this string into the button of listview by refreshing the listview..how can i do this below is my code
public class MainActivity extends ListActivity implements RegisterContactToServer.AsyncResponseListener {
ListView lv;
Cursor cursor,cursor1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_contact);
cursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
startManagingCursor(cursor);
String[] from={ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone._ID};
int[] to={R.id.name_entry,R.id.number_entry};
ContactList listadapter=new ContactList(this,R.layout.single_row1,cursor,from,to);
setListAdapter(listadapter);
lv=getListView();
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
class ContactList extends SimpleCursorAdapter implements Filterable {
private Context context;
String strfromMobileno="";
private int layout;
public ContactList(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.context=context;
this.layout=layout;
// TODO Auto-generated constructor stub
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(layout, parent, false);
return v;
}
#Override
public void bindView(View v, final Context context, Cursor c) {
int nameCol = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
String name = c.getString(nameCol);
int noCol = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String no = c.getString(noCol);
final CheckBox p1 = (CheckBox) v.findViewById(R.id.checkBox1);
final CheckBox p2 = (CheckBox) v.findViewById(R.id.checkBox2);
final CheckBox p3 = (CheckBox) v.findViewById(R.id.checkBox3);
final TextView name_text = (TextView) v.findViewById(R.id.name_entry);
if (name_text != null) {
name_text.setText(name);
}
final TextView no_text = (TextView) v.findViewById(R.id.number_entry);
if (no_text != null) {
no_text.setText(no);
}
final Contact contact=new Contact();
final Button btn=(Button)v.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String contactName = name_text.getText().toString();
String contactNo =no_text.getText().toString();
String statusR=btn.getText().toString();
contact.setFromMobileno(contactName);
contact.setToMobileno(contactNo);
if(p1.isChecked()){
int priority1=Integer.parseInt(p1.getText().toString());
contact.setPriority1(1);
}else{contact.setPriority1(0);}
if(p2.isChecked())
{
int priority2=Integer.parseInt(p2.getText().toString());
contact.setPriority2(1);
}else{contact.setPriority2(0);}
if(p3.isChecked()){
int priority3=Integer.parseInt(p3.getText().toString());
contact.setPriority3(1);
}else{contact.setPriority3(0);}
contact.setStatus(statusR);
if (!contactName.equals("") ){
RegisterConactDBAdapter cdbhelper=new RegisterConactDBAdapter(context);
cdbhelper.open();
cdbhelper.CreateUser(contact);
RegisterContactToServer reg=new RegisterContactToServer(context);
reg.execute(contact);
Intent intent = new Intent(context,ListRefresh.class);
startActivity(intent);
}
}
});
}
}
#Override
public void onresponse(String status, String jsonData) {
// TODO Auto-generated method stub
RegisterConactDBAdapter cdbheler=new RegisterConactDBAdapter(getBaseContext());
cdbheler.open();
cdbheler.UpdateStatus(jsonData);
}
}
I am trying to get the selected checkboxes deleted. I have no clue what to do after this code below:
I am using a simple cursor adapter. I also want to know how I could select and deselect all the checkboxes. I had researched everything but can't find any answer. I've been stuck for 3 days. ALso, Im not sure if the code below is even the correct path i need.
public class SecondCheckedListView extends ListActivity {
private Cursor mCursor;
private Long mRowId;
DBAdapter db = new DBAdapter(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondcheckedlistview);
db.open();
fillData();
}
private void fillData() {
mCursor = db.getAllContacts();
startManagingCursor(mCursor);
String[] from = new String[]{DBAdapter.KEY_FIRSTNAME, DBAdapter.KEY_LASTNAME};
int[] to = new int[]{R.id.textView1, R.id.textView2};
SimpleCursorAdapter d =
new SimpleCursorAdapter(this, R.layout.rowcheckedlistview, mCursor, from, to);
setListAdapter(d);
}
public class MyAdapter extends SimpleCursorAdapter {
private final Context mContext;
private final int mLayout;
private final Cursor mCursor;
private final int mNameIndex;
private final int mIdIndex;
private final LayoutInflater mLayoutInflater;
private final class ViewHolder {
public TextView firstname;
public TextView lastname;
public CheckBox cBox;
}
public MyAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.mContext = context;
this.mLayout = layout;
this.mCursor = c;
this.mNameIndex = mCursor.getColumnIndex(DBAdapter.KEY_FIRSTNAME);
this.mIdIndex = mCursor.getColumnIndex(DBAdapter.ROW_ID);
this.mLayoutInflater = LayoutInflater.from(mContext);
}
#Override
public View getView(final int pos, View convertView, ViewGroup parent) {
if (mCursor.moveToPosition(pos)) {
ViewHolder viewHolder;
if(convertView == null) {
convertView = mLayoutInflater.inflate(mLayout, null);
viewHolder = new ViewHolder();
viewHolder.firstname = (TextView) convertView.findViewById(R.id.textView1);
viewHolder.lastname = (TextView) convertView.findViewById(R.id.textView2);
viewHolder.cBox = (CheckBox) convertView.findViewById(R.id.bcheck);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
String firstname = mCursor.getString(mNameIndex);
String rowId = mCursor.getString(mIdIndex);
}
return convertView;
}
}
}
what do you mean "delete"? You want to remove the checkbox from the listview? You might need
to do something like
viewHolder.RemoveView(cBox);
If you want to select/delsect all checkboxes, you'll probably want that based upon some other botton or checkbox. I do that with another checkbox like so:
private OnClickListener setSelectAllListener = new OnClickListener() {
#Override
public void onClick(View v) {
selectAllTouched = true;
if (!selectAllCheck.isChecked()) {
selectAll = false;
studs.notifyDataSetChanged();
}
else {
selectAll = true;
studs.notifyDataSetChanged();
}
}
};
And then in my custom adapter that does stuff with each checkbox in a listview, I use this:
if (selectAllTouched) {
if(selectAll){
holder.here.setChecked(true);
itemCheckedHere.set(pos, true);
int who= new Integer(holder.text2.getText().toString());
mDbHelper.updateAttend(who, classnum, ATTEND, 1, attendDate );
}
else{
holder.here.setChecked(false);
itemCheckedHere.set(pos, false);
int who = new Integer(holder.text2.getText().toString());
mDbHelper.updateAttend(who, classnum, ATTEND, 0, attendDate );
}
}
I used a sparse array, itemCheckedHere, to hold the values of all the check boxes so scrolling doesn't change the values on resued views.
I know that data is inserted in the database , and it is listed correctly but the notifydatasetchanges doesn't works , the list view doesn't get refreshed, so I have to restart my activity....
Cursor c = db.selectAll();
final ListView lv = (ListView) findViewById(R.id.listView1);
adapter=new MyContactsAdapter(this, c);
lv.setAdapter(adapter);
db.insertInTable("asd","asd");
//insertion in table works, I am sure, cause when I start the activity the new values are listed
//this the part that doesn't works
((MyContactsAdapter)lll.getAdapter()).notifyDataSetChanged();
adapter.notifyDataSetChanged();
lll.refreshDrawableState();
private class MyContactsAdapter extends CursorAdapter{
private Cursor mCursor;
private Context mContext;
private final LayoutInflater mInflater;
public MyContactsAdapter(Context context, Cursor cursor) {
super(context, cursor, true);
mInflater = LayoutInflater.from(context);
mContext = context;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView t = (TextView) view.findViewById(R.id.textView2);
t.setText(cursor.getString( 0 ));
t = (TextView) view.findViewById(R.id.TextView01);
t.setText(cursor.getString(1));
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view = mInflater.inflate(R.layout.item, parent, false);
return view;
}
}//adapter
Ok, something like, From your code,
Cursor c;
#Override
onCreate(.... {
setContentView(...);
final ListView lv = (ListView) findViewById(R.id.listView1);
db.insertInTable("asd","asd");
c = db.selectAll();
adapter=new MyContactsAdapter(this, c);
lv.setAdapter(adapter);
// some button click...
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
db.insertInTable("xyz","xyz");
c = db.selectAll();
adapter.notifyDataSetChanged();
}
});
Try this, Let see what happen...
I've got ListActivity and I am using custom CursorAdapter.
In each item of the list I've got also checkbox...
Now I have in my list screen a perm button, when you press on it,
It should find all the checkboxes which are 'checked' and do some operations on the item which it's checkbox is 'checked'.
How can I retrieve all the checked ones?
I've done focusable:false, so I can use OnClickListener, but I don't know how farther then
this..
Thanks,
some code:
This is in my ListActivity class:
final String columns[] = new String[] { MyUsers.User._ID,
MyUsers.User.MSG, MyUsers.User.LOCATION };
int[] to = new int[] { R.id.toptext, R.id.bottomtext,R.id.ChkBox,
R.id.Location};
Uri myUri = Uri.parse("content://com.idan.datastorageprovider/users");
Cursor cursor = getContentResolver().query(myUri, columns, null, null, null);
startManagingCursor(cursor);
ListCursorAdapter myCursorAdapter=new ListCursorAdapter(this,R.layout.listitem, cursor, columns, to);
this.setListAdapter(myCursorAdapter);
and this is my Custom Cursor adapter class:
public class ListCursorAdapter extends SimpleCursorAdapter
{
private Context context;
private int layout;
public ListCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to)
{
super(context, layout, c, from, to);
this.context = context;
this.layout = layout;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
Cursor c = getCursor();
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(layout, parent, false);
return v;
}
#Override
public void bindView(View v, Context context, Cursor c)
{
TextView topText = (TextView) v.findViewById(R.id.toptext);
if (topText != null)
{
topText.setText("");
}
int nameCol = c.getColumnIndex(MyUsers.User.MSG);
String name = c.getString(nameCol);
TextView buttomTxt = (TextView) v.findViewById(R.id.bottomtext);
if (buttomTxt != null)
{
buttomTxt.setText("Message: "+name);
}
nameCol = c.getColumnIndex(MyUsers.User.LOCATION);
name = c.getString(nameCol);
TextView location = (TextView) v.findViewById(R.id.Location);
if (locationLinkTxt != null)
{
locationLinkTxt.setText(name);
}
}
//Modify to meet your needs
String[] from = new String[]{ YourDbAdapter.KEY_WORDS_ROWID, YourDbAdapter.KEY_WORDS_ROWID};
int[] to = new int[]{ R.id.row_item_checkbox};
mAdapter = new SimpleCursorAdapter(this, R.layout.row_item_with_checkbox, yourDbCursor, from, to);
mAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
#Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if(view.getId() == R.id.myCheckbox )
{
CheckBox cb = (CheckBox) view;
cb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final long id = cursor.getLong(columnIndex); //Your row id
//You can do the necessary work here such as adding to an List or somehting
}
});
return true;
}
return false;
}
});