I want to delete a row from my list view on click of "delete" button. My listview item has following things placed horizontally: TextView-1,TextView-2,TextView-3,ImageButton-delete button. Now when I click delete button, the row should be deleted from the view. Below is the adapter code;
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
View view = (View) v.getParent();
TextView tv = (TextView) view.findViewById(R.id.item_name);
String item = tv.getText().toString();
String tableno = mListItems.get(0).getTableNumber();
orderDetailsDB = new OrderDetailsDBAdapter(
getApplicationContext());
orderDetailsDB.deleteItem(item,tableno);
I tried by setting Individual textviews to blank but its not working.
holder.itemName.setText("");
holder.amount.setText("");
holder.quantity.setText("");
I read couple of posts and they suggest to remove item from my list(mListItems) and then do adapter.notifyDataSetChanged();. Problem is I am not using array adapter for populating list view but using Custom adapter, so unable to get the position for item to be deleted. Please advise. thanks.
First write below line in your adapter's getView method.
button.setTag(position)
in onClick method
#Override
public void onClick(View v) {
int position = (Integer)v.getTag();
yourarraylistObject.remove(position);
// your remaining code
notifyDataSetChanged();
}
Just use remove() to remove list item from the adapter
for your reference
adapter = new MyListAdapter(this);
lv = (ListView) findViewById(android.R.id.list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
AlertDialog.Builder adb=new AlertDialog.Builder(MyActivity.this);
adb.setTitle("Delete?");
adb.setMessage("Are you sure you want to delete " + position);
final int positionToRemove = position;
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
MyDataObject.remove(positionToRemove);
adapter.notifyDataSetChanged();
}});
adb.show();
}
});
What you can do in order to get the position that you want to delete is to pass that into your listener:
// inside custom adapter
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
.....
deleteButton.setOnClickListener(new MyClickListener(position);
}
private class MyClickListener implements OnClickListener
{
int position = -1;
public MyClickListener(final int position)
{
this.position = position;
}
#Override
public void onClick(View v) {
// do your delete code here
notifyDataSetChanged();
}
Related
I want to implement a dialog box in on click of listitem of list view in my adapter class. How can I access my ListView from another class?
public void onClick(View v) {
AlertDialog.Builder adb = new AlertDialog.Builder(v.getRootView().getContext());
adb.setTitle("LVSelectedItemExample");
adb.setMessage("Selected Item is = " + listView.getItemAtPosition(position));
adb.setPositiveButton("Ok", null);
adb.show();
}
Why do you make in your Adapter class. Use listview.setOnItemClickListener(this) in your activity or fragment.
or
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
in your onCreate() method.
Try this -
public void onClick(View v) {
AlertDialog.Builder adb = new AlertDialog.Builder(v.getRootView().getContext());
adb.setTitle("LVSelectedItemExample");
adb.setMessage("Selected Item is = " + getItem(position));
adb.setPositiveButton("Ok", null);
adb.show();
}
or implement onItemClick() listener in your activity/fragment containing the ListView.
Your example code uses onClick, you should have onItemClick.
Your code will look like this:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MyAdapter adapter = (MyAdapter) parent.getAdapter; // use your actual adapter class name here
AlertDialog.Builder adb = new AlertDialog.Builder(v.getRootView().getContext());
adb.setTitle("LVSelectedItemExample");
adb.setMessage("Selected Item is = " + adapter.getItem(position));
adb.setPositiveButton("Ok", null);
adb.show();
}
});
And make sure you have a proper implementation of getItem in your adapter.
I have a list view that contains Product (productid, productname) + one button
I am filling ArrayList below.
ArrayList<Product> products = new ArrayList<Product>();
//fill products here
productAdapter adapter = new ProductAdapter(getApplicationContext(), products);
productListView.setAdapter(adapter);
productListView.setVisibility(View.VISIBLE);
Now in adapter
public View getView(final int position, View convertView, ViewGroup parent) {
// some other code
holder.btn_addtocart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
products.get(position).setname("ABC");
}
});
}
Problem is its not updating immediately. When we scroll down and up its getting changed.
Why is not not updating listview immediately?
How to fix this?
To refresh the list, you need to call notifyDataSetChanged() after updating the data.
eg:
holder.btn_addtocart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
products.get(position).setname("ABC");
notifyDataSetChanged();
}
});
I have a listview which uses this layout:
android.R.layout.simple_list_item_checked
I have an OnItemClickListener that gets the check status of the clicked item and either checks or unchecks that item but it does not work. There are NO errors in logcat. When I click on an item in the listview simply nothing happens.
The OnItemClickListener looks like this:
//------------------- OnItemClickListener -----------------------------
lvCheckList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
CheckedTextView textview = (CheckedTextView)view;
if (!textview.isChecked()){
textview.setChecked(true);
}else {
textview.setChecked(false);
}
}
});
Try with this...
//sample code
TextView TxtName = (TextView) findViewById(R.id.NameControlId);
TxtName.setOnClickListener(this);
#Override
public void onClick(View view)
{
switch (view.getId())
{
case R.id.NameControlId:
Toast.makeText(getApplicationContext(),"click",Toast.LENGTH_SHORT).show();
break;
}
}
I am using below code to setcolor of a selected item of a listview. The rule is only one should be colored. But with below code if I select 2 views both get colored. Can you please help me get all other views in the listview so that when I click on certain view all other views i set to different color and the selected view i set a different color(Green in this case).
Please let me know if any other solution?
lv = (ListView) view.findViewById(R.id.listf);
lv.setAdapter(text![enter image description here][1]Adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView v = (TextView) view.findViewById(R.id.template_text);
view.setBackgroundColor(Color.GREEN);
}
});
I resolved the problem using the below:
I put a loop where only the selected list item is set in RED whereas all others were set in Green, in this way only only one list item will be colored on selected.
lv = (ListView) view.findViewById(R.id.listf);
lv.setAdapter(Adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
for (int i = 0; i < 3; i++)
{
if (position == i)
{
parent.getChildAt(i).setBackgroundColor(Color.RED);
}
else
{
parent.getChildAt(i).setBackgroundColor(getResources().getColor(R.color.Dark_Green);
}
}
TextView v = (TextView) view.findViewById(R.id.template_text);
view.setBackgroundColor(Color.GREEN);
}
});
As you told you are not able to change adapter code, you can prefer solution 2.
Solution 1: Create one variable int selectedPosition and method setSelected in your adapter class
int selectedPosition = -1;
public void setSelected(int position)
{
selectedPosition = position;
notifyDatasetChanged();
}
Edit getView() of the adapter class and include following code
if(selectedPosition==position)
{
templateTextView.setBackgroundColor(Color.GREEN);
}
else templateTextView.setBackgroundColor(Color.BLUE);// default textView color
Solution 2: keep reference of previously selected textView as well each time change the color of currently selected textview to green andd previous one to blue
TextView previousSelected = null;
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(previousSelected!=null)
previousSelected.setBackgroundColor(Color.BLUE);
TextView v = (TextView) view.findViewById(R.id.template_text);
view.setBackgroundColor(Color.GREEN);
previousSelected = v;
}
});
I need help retrieve my ListView items ID, to add delete functionality.
My ListView is populated with this code:
ListView BookList = (ListView)findViewById(R.id.listView1);
DbAdapter db = new DbAdapter(getApplicationContext());
db.open();
Cursor cursor = db.fetchAllBooks();
startManagingCursor(cursor);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.book, cursor, new String[]{DbAdapter.KEY_TITLE,DbAdapter.KEY_AUTHOR},new int[]{R.id.booktitle,R.id.bookauthor});
BookList.setAdapter(adapter);
if(cursor.moveToFirst()) {
while (cursor.moveToNext());
}
db.close();
My delete code is set as this, in the DbAdapter class:
//delete a book
public boolean deleteBook(long bookID) {
return database.delete(DATABASE_TABLE, KEY_BOOKID + "=" + bookID, null) > 0;
}
Any help?
Full sources: pastebin.com/kKgePkPM
You have to add a listener to your listView. The best way is set
bookList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// DO YOUR STUFF HERE
}
});
There you will get the view and the position clicked. With the view or the position you could obtain the value desired to your "deletebook" method.
If I understand you correctly, you can add itemclicklistener to your listview, then use the row id to remove it from your database, if the bookid is the same as the row id of course.
BookList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (db.deleteBook(position)) { // Success!
// remove from listview data source here
adapter.notifyDataSetChanged();
}
}
});
try adding something like this in getView method:
Button btnDelete = (Button) convertedRowView.findViewById(R.id.btnDeleteCustom);
btnDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int itemPos = 4; // This is position of item in ArrayList
deleteBook(getItemId(itemPos));
}
});