android listview check on long press with custom adapter - android

I did not find any right solutions for my case on the internet.
I have a couple of items from a database in my listview object. For this I created a custom BaseAdapter and implemented a OnLongClickListener() inside the adapter class (I know that I would overwrite how a listview handle the animations etc. but this is just for learning).
I can select multiple items with a long click and change the background color of each of the rows. But I can not reset the rows (unchecked and "normal" background color) although I implemented these behaviors in my listener.
What am I doing wrong?
Here is my custom adapter class
package com.dacmas.shoppingapp;
import android.content.Context;
import android.text.AndroidCharacter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.amulyakhare.textdrawable.TextDrawable;
import com.amulyakhare.textdrawable.util.ColorGenerator;
import java.util.ArrayList;
import java.util.List;
public class ShoppingListAdapter extends BaseAdapter {
private ArrayList< ShoppingListEntry > m_itemList;
private Context m_context;
private LayoutInflater m_inflater;
private View m_convertView;
public ShoppingListAdapter(Context context, ArrayList< ShoppingListEntry > shoppingListEntryArrayList ) {
m_itemList = shoppingListEntryArrayList;
m_context = context;
m_inflater = ( LayoutInflater.from( context ) );
}
#Override
public int getCount() { return m_itemList.size(); }
#Override
public Object getItem( int position ) { return m_itemList.get( position ); }
#Override
public long getItemId( int position ) { return position; }
#Override
public View getView(final int position, View convertView, final ViewGroup parent ) {
if ( convertView == null ) {
m_convertView = LayoutInflater.from( m_context ).inflate( R.layout.list_row_shoppinglist, parent, false );
}
ImageView accountImage = (ImageView) m_convertView.findViewById(R.id.text_drawable);
String firstLetterOfName = "A";
ColorGenerator generator = ColorGenerator.MATERIAL;
int color = generator.getRandomColor();
TextDrawable drawable = TextDrawable.builder()
.buildRound(firstLetterOfName, color);
accountImage.setBackground(drawable);
TextView title = ( TextView ) m_convertView.findViewById( R.id.txt_title );
TextView info = ( TextView ) m_convertView.findViewById( R.id.txt_info );
String itemTitle;
String productBrand = m_itemList.get( position ).getProductBrand().toString();
if ( !productBrand.isEmpty() ) {
itemTitle = productBrand + " " + m_itemList.get( position ).getProductName().toString();
} else {
itemTitle = m_itemList.get( position ).getProductName().toString();
}
title.setText( itemTitle );
String itemInfo;
String productAmount = m_itemList.get( position ).getProductAmount();
itemInfo = productAmount;
info.setText( itemInfo );
m_convertView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
if ( ((ListView)parent).isItemChecked(position) ) {
( ( ListView )parent ).setItemChecked( position, false );
v.setBackgroundColor(m_convertView.getResources().getColor(R.color.colorWhite));
} else {
( ( ListView )parent ).setItemChecked( position, true );
v.setBackgroundColor(m_convertView.getResources().getColor(R.color.colorSelectedItem));
}
return false;
}
});
return m_convertView;
}
}

The question is - When do you want the deselection to happen?
Ideally, once you perform a long press, you enter in to a single selection mode. From that point on wards, it should handle clicks (touches) to toggle selection (and optionally change color)
Now that you are done with your selection choices, you may want to
Perform an action like deleting the selected items.
Wait for some other action to exit the selection mode.
In case of option two, trapping the back button press(onBackPressed) might do the trick. in that you can write your logic clear all selections (color and check box)
It might be a good idea if you can bake that logic into your custom adapter for purposes of re-usability.

Related

Use/Display specific ArrayList String for ListView

I'm having trouble displaying the right String from an ArrayList in my ListView.
My Array (m_aDataList) looks like this:
m_aDataList {ArrayList}
{...}0
m_cText = "R;21;9;River Street 2;12154;;1;.......
m_cTimeStamp = 1556553367492
m_nID = 7
m_nStatus = 0
m_nType = 10002
{...}1
....
This is how I'm currently trying to Display the ArrayList in my ListView:
ArrayAdapter<Message> tuAdapter;
MessageManager tu = new MessageManager();
ArrayList<Message> list = tu.getMessageData();
tuAdapter = new ArrayAdapter<Message>(this, android.R.layout.simple_list_item_1, list);
lvOrders.setAdapter(tuAdapter);
It technically works and adds something like this to my ListView:
de.telematik.testapp.entities.Message#232d9c76
But what I'm trying to do is, only show Messages where m_nType == 10002 and Display them like this in my ListView:
Order: 12154 (order number from m_cText)
Checking weither or not m_nType is = 10002 or 10000 shouldn't be the problem. But how do I get the order number out of the String m_cText and then display it in my ListView?
In any case, thanks for your help.
You are using a basic default list adapter, but you are passing it an array of objects (Message). In order to control what displays in the list, you will need to create your own custom adapter class, that extends (probably base adapter) one of the standard adapter classes. You will call it similarly to the way you pass your list to your simple list adapter. Here is a sample custom list adapter:
ClaimListAdapter.java
package com.mycompany.myapp.adapter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import com.mycompany.myapp.R;
import com.mycompany.myapp.ClaimListFragment;
import com.mycompany.myapp.model.ClaimItem;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
public class ClaimListAdapter extends BaseAdapter {
private Context context;
private ArrayList<ClaimItem> claimItems;
ClaimListFragment fragment;
public ClaimListAdapter(ClaimListFragment fragment, Context context, ArrayList<ClaimItem> claimItems){
this.context = context;
this.claimItems = claimItems;
this.fragment = fragment;
}
#Override
public int getCount() {
return claimItems.size();
}
#Override
public Object getItem(int position) {
return claimItems.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#SuppressLint("InflateParams")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// This method gets called for every item in the list when it is about to be displayed in the list
// This helps you get a reference to the layout of the list item
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.claim_list_item, null);
}
// If you have a button that you want to perform some function, such as delete, and then call
// a method back in the parent to update the items in the array, this is one way to do it
Button btnDelete = (Button) convertView.findViewById(R.id.claim_delete_in_list);
// Save the position of the item in the list, so, you know which item was clicked
btnDelete.setTag(position);
btnDelete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Integer position = (Integer)v.getTag();
// Call this method which is back in the parent, which in this case is a fragment, but could be an Activity
fragment.deleteItemList(position);
}
});
btnDelete.setVisibility(View.GONE);
// Here you get a reference to the various TextViews in your layout
TextView txtTitle = (TextView) convertView.findViewById(R.id.claim_title);
TextView txtStatus = (TextView) convertView.findViewById(R.id.claim_status);
TextView txtDate = (TextView) convertView.findViewById(R.id.claim_date);
TextView txtDistance = (TextView) convertView.findViewById(R.id.claim_distance);
TextView txtAmount = (TextView) convertView.findViewById(R.id.claim_amount);
// Here you get the various strings out of your object for display
String claim_title = claimItems.get(position).getDocumentID();
if (claim_title == null) {
claim_title = "";
}
String claim_status = claimItems.get(position).getClaimStatus();
String claim_date = claimItems.get(position).getClaimDate();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date = null;
try {
date = (Date) dateFormat.parse(claim_date);
dateFormat = new SimpleDateFormat("M/d/yyyy");
String formattedDate = dateFormat.format(date);
txtDate.setText(formattedDate);
} catch (ParseException e) {
e.printStackTrace();
}
double total_miles = claimItems.get(position).getTotalMiles();
double total_amount = claimItems.get(position).getTotalAmount();
// Here you set the values in the TextView for display
txtTitle.setText(claim_title);
txtStatus.setText(claim_status);
txtDistance.setText("" + total_miles + "mi");
txtAmount.setText("$" + total_amount);
return convertView;
}
}
Alternatively, you could create a string array containing only the order numbers from your array of messages, and pass that array to your list adapter instead of the array of messages.

How do I update a SQLite row from a ListView item?

I have an application that uses sqlite, it stores information about hardware store items and displays them in a ListView, this list view shows the name of the item, the price, the quantity, and the supplier. And each list item also has a Sell button and when I click the button it is supposed to subtract 1 from that specific item's quantity and update the database, but since the button is created in the CursorAdapter Im not sure how to access the database and update it.
This is my CursorAdapter:
package com.example.android.inventoryapp;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.android.inventoryapp.data.InventoryContract.InventoryEntry;
public class InventoryCursorAdapter extends CursorAdapter {
public InventoryCursorAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView itemNameView = view.findViewById(R.id.item_name);
TextView itemPriceView = view.findViewById(R.id.item_price);
TextView itemQuantityView = view.findViewById(R.id.item_quantity);
TextView itemSupplierView = view.findViewById(R.id.item_supplier);
ImageView sellButton = view.findViewById(R.id.sell_button);
int nameColumnIndex = cursor.getColumnIndex(InventoryEntry.COLUMN_ITEM_NAME);
int priceColumnIndex = cursor.getColumnIndex(InventoryEntry.COLUMN_ITEM_PRICE);
int quantityColumnIndex = cursor.getColumnIndex(InventoryEntry.COLUMN_ITEM_QUANTITY);
int supplierColumnIndex = cursor.getColumnIndex(InventoryEntry.COLUMN_ITEM_SUPPLIER);
int quantity = cursor.getInt(quantityColumnIndex);
String name = cursor.getString(nameColumnIndex);
String price = String.valueOf(cursor.getInt(priceColumnIndex)) + context.getString(R.string.currency_symbol);
String quantityStr = String.valueOf(quantity);
String supplier = cursor.getString(supplierColumnIndex);
itemNameView.setText(name);
itemPriceView.setText(price);
itemQuantityView.setText(quantityStr);
itemSupplierView.setText(supplier);
}
}
In your activity that holds the adapter reference, create an inner class something like:
public class MyClickListener {
public void handleClick(Item item) {
// access your DB here, {item} is available if you need the data
}
}
then when you create your adapter
myAdapter = new InventoryCursorAdapter(context, cursor, new MyClickListener());
save the reference to that click listener in your adapter.
then in the adapter's BindView method (if you need the item data to update the database, pass it through the click listener)
sellButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Item item = myItemSet.get(position);
myListener.handleClick(item);
}
});

Android Gridview and Button OnItemclick

Here is my buttonAdapter class that i think is accurate:
package com.example.test;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
public class ButtonAdapter extends BaseAdapter {
private Context mContext;
public String [] fName = { "File 1", "File 2", "Roflcopters"};
// Gets the context so it can be used later
public ButtonAdapter(Context c) { mContext = c; }
// Total number of things contained within the adapter
public int getCount () { return 8; }
// Require for structure, not really used in my code.
public Object getItem (int position) { return null; }
// Require for structure, not really used in my code. Can be used to get the id of an item in the adapter for manual control.
public long getItemId (int position) { return position; }
public View getView (int position, View convertView, ViewGroup parent){
Button btn;
if (convertView == null) { // if it's not recycled, initialize some attributes
btn = new Button (mContext);
btn.setLayoutParams (new GridView.LayoutParams (190, 190));
btn.setPadding (1, 1, 1, 1);
} else {
btn = (Button) convertView;
}
// btn.setText(filesnames[position]);
// filenames is an array of strings
//btn.setTextColor (Color.WHITE);
//btn.setBackgroundResource (R.drawable.sample_2);
//btn.setBackgroundColor (Color.BLACK);
btn.setHighlightColor(Color.GREEN);
btn.setId (position);
return btn;
}
}
Here is my home class. I can't get the onItemClick to work out. What am I doing wrong here:
package com.example.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;
public class home extends Activity implements OnItemClickListener {
public final static String EXTRA_MESSAGE1 = "com.example.text.MESSAGE";
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView (R.layout.activity_home);
GridView gridview = (GridView) findViewById (R.id.gridview);
gridview.setAdapter (new ButtonAdapter (this));
/*gridview.setOnItemClickListener (new OnItemClickListener () {
public void onItemClick (AdapterView <?> parent, View v, int position, long id) {
Toast.makeText (home.this, "" + position, Toast.LENGTH_LONG).show ();
Intent intent = new Intent (this, alarm.class);
String message = "Position:" + position;
intent.putExtra(EXTRA_MESSAGE1, message);
startActivity (intent);
}
});
* */
}
#Override
public void onItemClick (AdapterView <?> parent, View v, int position, long id) {
Intent intent = new Intent (this, alarm.class);
String message = "Position:" + position;
intent.putExtra(EXTRA_MESSAGE1, message);
startActivity (intent);
}
}
The onItemClick doesn't work and neither does the commented 'setOnItemClickListener' when it isn't commented out and 'onItemClick' is commented. What am I doing wrong?
If GridView, ListView have click able controls like BUtton, then onItemClick will not fired.
You need to implement Button Click listener in your getView method of the adapter.
like
public View getView(int position, View convertView, ViewGroup parent) {
Button btn;
if (convertView == null) { // if it's not recycled, initialize some
// attributes btn = new Button (mContext);
btn.setLayoutParams(new GridView.LayoutParams(190, 190));
btn.setPadding(1, 1, 1, 1);
} else {
btn = (Button) convertView;
} // btn.setText(filesnames[position]); // filenames is an array of
// strings //btn.setTextColor (Color.WHITE);
// btn.setBackgroundResource (R.drawable.sample_2);
// btn.setBackgroundColor (Color.BLACK);
btn.setHighlightColor(Color.GREEN);
btn.setId(position);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Handle the click here
}
});
return btn;
}
You can add this line in the root layout of GridView items:
android:descendantFocusability="blocksDescendants"
Then onItemClickListener.onItemClick() will not fire when you tap on subviews which OnClickListener has been defined for them separately.
I've tested that Set Button.onClickListener() (in API 15) won't solve the problem.
So the GridView will not trigger onItemClick if it contains clickable views.
You can use ImageView instead Button.
i have the same problem when i was trying to implement onitemclick on gridview where filled with button. Because the button is stealing the focus of each space on gridview, you have to give the inflated button android:focusable="false" . however, the button is taking up almost entire space inside a grid, so you have to click the very edge of the button to trigger onitemclick call-back. i am suggesting you can set onclick or use image and designe it like a button.

android checkbox onCheckedChanged is not invoked

I have defined onCheckedChanged for the checkbox in my listview.
When i click on the check box to check / uncheck it this function is getting invoked.
But when i setthe state of the check box from code like
check.setChecked(true);
the onCheckedChanged is not getting invoked.
Please help.
Adapter file :
package com.idg.project.adapters;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
import com.idg.project.R;
import com.idg.project.activities.ScanListActivity;
import com.idg.project.activities.SearchResultActivity;
import com.idg.project.adapters.WishListAdapter.ViewHolder;
import com.idg.project.entity.ScannedProduct;
public class ScanListAdapter extends BaseAdapter {
private Context context;
private List<ScannedProduct> productList;
protected LayoutInflater mInflater;
Button showOrHideButton;
static public int count = 0;
String barcodeForSelectedRow;
String formatForSelectedRow;
OnItemClickListener rowListener;
Activity parentActivity;
boolean isWishList;
public ScanListAdapter(Context context, List<ScannedProduct> objects,
Button button, Activity parentActivity) {
super();
this.productList = objects;
this.context = context;
this.mInflater = LayoutInflater.from(context);
showOrHideButton = button;
this.parentActivity = parentActivity;
this.isWishList = isWishList;
}
public int getCount() {
return productList.size();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public void notifyDataSetChanged() {
// TODO Auto-generated method stub
super.notifyDataSetChanged();
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
final int pos = position;
Log.i("checkboxflag at : ", pos+"is"+(productList.get(pos).getCheckboxflag()));
Log.i("getview : fresh", "getview"+pos);
convertView = mInflater.inflate(R.layout.product_list_row, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView.findViewById(R.id.productid);
holder.text1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(parentActivity,
SearchResultActivity.class);
intent.putExtra("barcode", productList.get(pos)
.getBarcode());
intent.putExtra("format", productList.get(pos).getFormat());
intent.putExtra("IsScan", false);
Log.i("", "" + productList.get(pos).getBarcode());
parentActivity.startActivity(intent);
Log.i("", "" + pos);
}
});
holder.text2 = (TextView) convertView.findViewById(R.id.price);
// holder.text2.setOnClickListener(listener);
holder.image = (ImageView) convertView
.findViewById(R.id.productimageid);
convertView.setTag(holder);
// holder.image.setOnClickListener(listener);
holder.text1.setText(productList.get(position).getTitle());
holder.text2.setText(productList.get(position).getPrice().toString());
if (productList.get(position).getSmallImage() != null) {
byte[] bb = (productList.get(position).getSmallImage());
holder.image.setImageBitmap(BitmapFactory.decodeByteArray(bb, 0,
bb.length));
} else {
holder.image.setImageBitmap(null);
holder.image.setBackgroundResource(R.drawable.highlight_disabled);
}
// holder.image.setImageBitmap(Utils.loadBitmap(productList.get(position).getSmallImage()));
final CheckBox check = (CheckBox) convertView
.findViewById(R.id.checkbox);
check.setClickable(true); // to remove anything carried over from prev convert view
if(productList.get(pos).getCheckboxflag()==1)
{
Log.i("CheckBox set checked",""+pos);
check.setChecked(true);
}
else{
Log.i("CheckBox set unchecked",""+pos);
check.setChecked(false);
}
setWishListItemsInScanList(pos, convertView);
check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Log.i("OnclickListener","Current Position"+pos);
if (check.isChecked()
&& productList.get(pos).getWishListFlag() == 0) {
if(check.isClickable()){
Log.i("CheckBox check",""+pos);
ScanListActivity.updateCheckBoxSelection(1, pos);
ScanListAdapter.count++;
}
} else if (!check.isChecked()
&& productList.get(pos).getWishListFlag() == 0){
if(check.isClickable()){
ScanListActivity.updateCheckBoxSelection(0, pos);
ScanListAdapter.count--;
Log.i("CheckBox UNcheck",""+pos);
}
}
if (ScanListAdapter.count == 0) {
// showOrHideButton.setClickable(false);
// showOrHideButton.setVisibility(View.GONE);
showOrHideButton.setEnabled(false);
} else {
// showOrHideButton.setVisibility(View.VISIBLE);
showOrHideButton.setEnabled(true);
}
}
});
return convertView;
}
private void setWishListItemsInScanList(int pos, View convertView) {
if (productList.get(pos).getWishListFlag() == 1) {
Log.i("CheckBox set checked from wish list",""+pos);
CheckBox check = (CheckBox) convertView.findViewById(R.id.checkbox);
check.setClickable(false);
check.setChecked(true);
}
}
static class ViewHolder {
TextView text1;
ImageView image;
TextView text2;
}
}
List activity file :
package com.idg.project.activities;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.idg.project.R;
import com.idg.project.adapters.WishListAdapter;
import com.idg.project.adapters.ScanListAdapter;
import com.idg.project.entity.ScannedProduct;
import com.idg.project.services.ScannedProductDataAccessManager;
public class ScanListActivity extends BaseActivity {
static Button scanlist;
ScanListAdapter listAdapter;
static List<ScannedProduct> productList;
/* Notes for the Developer :
* For tracking the checked items Checkboxflag
* is maintained.
* Point1 : Select all will just set this flag in the local list and then call notifyDatachange of the adapter
* within adapter the check box is set or reset based on this flag for each row
*
* Point 2: When individual rows are selected , there is an onclick of the check box is invoked
* Here the Checkboxflag of the local list is set /unset . Also we need a way to knpw the select all button is
* to enabled or diabled. for that Count variable is updated here.
* Now Important point is these two actions shoulnt be taking place if the checkbox state change due to select all
* So there is a special check of isclickable in the onclicklistener
*
* Point 3: In scan list the items in the wish list are to be marked. This again needs special logic.
* This is done in the adapter code by checking all the rows whose wishListFlag is 1 and making it non clickable
*
* Important : Listview has the concept of ViewGroup and each view group is usually the rows fitting in the display screen
* so when we scroll, the viewGropu changes.
* Convertview is get reused for view groups. So need to careful undesired values that will be carried to next viewgroup*/
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.scan_list);
productList = new ArrayList<ScannedProduct>();
productList = getProductList();
for(int i=0;i<productList.size();i++){
Log.i("checkboxflag at : ", i+"is"+(productList.get(i).getCheckboxflag()));
}
final ListView lv = (ListView) findViewById(R.id.list);
scanlist = (Button) findViewById(R.id.addtowishlist);
scanlist.setEnabled(false);
listAdapter = new ScanListAdapter(this, productList, scanlist, this);
lv.setAdapter(listAdapter);
}
private List<ScannedProduct> getProductList() {
List<ScannedProduct> productList = new ArrayList<ScannedProduct>();
ScannedProductDataAccessManager productDataBaseManager = new ScannedProductDataAccessManager(
getApplicationContext());
String[] colList = { "title", "smallImage", "price" };
productList = productDataBaseManager.fetchAllProducts();
return productList;
}
static boolean selectFlag = false;
public void selectAll(View view) {
ListView listView = (ListView) findViewById(R.id.list);
view = findViewById(R.id.select_all);
if (selectFlag == false) {
for (int i = 0; i < listView.getAdapter().getCount(); i++) {
productList.get(i).setCheckboxflag(1);
}
view.setBackgroundResource(R.drawable.login_remme_dwn_btn);
selectFlag = true;
TextView text=(TextView) findViewById(R.id.select);
text.setText("Unselect All");
scanlist.setEnabled(true);
} else {
for (int i = 0; i < listView.getAdapter().getCount(); i++) {
productList.get(i).setCheckboxflag(0);
}
view.setBackgroundResource(R.drawable.login_remme_up_btn);
selectFlag = false;
TextView text=(TextView) findViewById(R.id.select);
text.setText("Select All");
scanlist.setEnabled(false);
}
((BaseAdapter)listView.getAdapter()).notifyDataSetChanged(); // we are only setting the flags in the list
// so need to notify adapter to reflect same on checkbox state
//listView.refreshDrawableState();
}
public void addToWishList(View view) {
ListView listView = (ListView) findViewById(R.id.list);
for (int i = 0; i < listView.getAdapter().getCount(); i++) {
ScannedProduct product = productList.get(i);
if (product.getWishListFlag() == 0 && product.getCheckboxflag()==1) {
product.setWishListFlag(1);
new ScannedProductDataAccessManager(getApplicationContext())
.updateProduct(product, "title",
new String[] { product.getTitle() });
product.setCheckboxflag(0);
//ScanListAdapter.count--;
}
Log.i("ScanList selected", product.getTitle());
}
Toast.makeText(getApplicationContext(),
"Added selected items to Wish List", Toast.LENGTH_SHORT).show();
scanlist.setEnabled(false);
((BaseAdapter)listView.getAdapter()).notifyDataSetChanged();
}
static public void updateCheckBoxSelection(int flag,int pos){ // when individual row check box is checked/ unchecked
// this fn is called from adapter to update the list
productList.get(pos).setCheckboxflag(flag);
}
}
Since your checkbox is inside listview, so you need to call notifyDataSetChanged method on your list's adapter to refresh it's contents.
update
instead of ((BaseAdapter)listView.getAdapter()).notifyDataSetChanged();, try calling listAdapter.notifyDataSetChanged();
I got the answer / bug in my code
i am not reusing convertview so its every time a new holder.
I am changing the flag of the checkbox and then assigning a statechange listener for the checkbox
thus its not getting invoked
when i changed the order to assign checkchangelistener before actually changing the state , its working as expected. The listener is getting called.
thanks all of you

Android, how to redraw list when the user sets checkbox?

I have a list view and each row contains two textviews and a checkbox.
I need user sets just one of checkboxes and if user sets other checkbox, previous checkbox should be cleared and second checkbox can be set.
I can generate the list but i don't know why onListItemClick() method does not work? therefore, I don't know which checkbox is set.
and my next question is, how can i redraw list after clicking checkbox by user?
my code is:
package com.Infindo.DRM;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
public class Infindo_PackageActivity extends ListActivity {
private final String[] titels = {"Try before buy", "Pay per play", "Pay per day", "Pay per week", "Pay per month", "Daily subscription", "Weekly subscription", "Monthly subscription"};
private final String[] descriptions = {"Free of charge", "Price: $1.00", "Price: $5.00", "Price: $10.00", "Price: $30.00", "Price: $5.00", "Price: $10.00", "Price: $30.00"};
private String[] flag = {"false", "false", "false", "false", "false", "false", "false", "false"};
private ListAdapter listAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.infindo_packagepage);
listAdapter = new ListAdapter(this);
setListAdapter(listAdapter);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Log.i("List Clicked:", "******");
}
public void onCheckboxClicked(View v){
int i = getSelectedItemPosition();
Log.i("item pos:", String.valueOf(i)); // every time the result is -1???
}
//*********************
// RowModel Class
//*********************
private class RowModel{
TextView title;
TextView description;
CheckBox checkbox;
}
//*********************
// ListAdapter Class
//*********************
private class ListAdapter extends ArrayAdapter<String>{
public ListAdapter(Context c) {
super(c, R.layout.infindo_listformat, titels);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
RowModel holder;
View row = convertView;
if(row == null){
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.infindo_listformat, parent, false);
holder = new RowModel();
holder.title = (TextView) row.findViewById(R.id.title);
holder.description = (TextView) row.findViewById(R.id.description);
holder.checkbox = (CheckBox) row.findViewById(R.id.checkbox);
row.setTag(holder);
} else {
holder = (RowModel) row.getTag();
}
holder.title.setText(titels[position]);
holder.description.setText(descriptions[position]);
String s = flag[position];
if(s.equalsIgnoreCase("false"))
holder.checkbox.setChecked(false);
else
holder.checkbox.setChecked(true);
return(row);
}
}
}
Update:
I have added
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
Log.i("List Redrawed:", "**notifyDataSetChanged()**");
}
into my List Adapter class and called it in
public void onCheckboxClicked(View v){
int i = getSelectedItemPosition();
Log.i("item pos:", String.valueOf(i)); // every time the result is -1???
listAdapter.notifyDataSetChanged();
}
I think the problem of redraw is solved. but I still I don't know what checkbox is clicked by the user in order to change flag array.
You can use setOnCheckedChangeListener to know which checkbox was selected. You can look at complete example of ListView with CheckBox here.
UPDATE
To make your onListItemClick() work you need to write android:focusable="false" for other items of the ListView, its because of the focus of other views ListView's onListItemClick() is not peforming as it should be performing.
Checkout this answer, why Android custom ListView unable to click on items
You probably need to handle the click from the checkbox itself and not just the listview.

Categories

Resources