How to access arraylist of arraylist in adapters - android

Hi i am making android application. I am using json parsing in my application. first of all this is my json response
{
"Data": {
"headerText": "Revolution",
"productStartingPrice": "$38.78",
"sizeList": [
{
"textUpper": "Revolution for (Pink)",
"textLower": "$38.78",
"packList": [
{
"textUpper": "3 DOSES",
"textLower": "$38.78",
}
{
"textUpper": "6 DOSES",
"textLower": "$71.60",
}
]
},
{
"textUpper": "Revolution for (Blue)",
"textLower": "$46.92",
"packList": [
{
"textUpper": "3 DOSES",
"textLower": "$46.92",
}
{
"textUpper": "6 DOSES",
"textLower": "$79.56",
}
{
"textUpper": "12 DOSES",
"textLower": "$137.27",
}
]
}
]
},
"Status": 1,
"Message": "",
"UserMessage": ""
}
Now I am using custom alert dialog to load sizelist array and another dailog for packlist array. Now what i want is when user select (Pink) from sizelist then in second dailog i want to display 3 doses and 6 doses from packlist and when user select (Blue) i want to display 3 doses, 6 doses ,12 doses. Right now i am getting only last two items of (Blue) 6 doses ,12 doses. can any one help me to solve this?
private void showDialog(){
dialogf = new Dialog(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_main, null);
ListView lv = (ListView) view.findViewById(R.id.custom_list);
Button close_btn = (Button) view.findViewById(R.id.close_btn_sizelist);
sizeadapter = new CustomListAdapterSize(getActivity(), sizelists);
lv.setAdapter(sizeadapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
dialogf.setContentView(view);
dialogf.show();
close_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialogf.dismiss();
}
});
}
private void showDialogpacks(){
dialogfpacks = new Dialog(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_main_packs, null);
ListView lv = (ListView) view.findViewById(R.id.custom_list_packs);
Button close_btn = (Button) view.findViewById(R.id.close_btn_packlist);
adapterpacks = new CustomListAdaptertwo(getActivity(), sizelists);
lv.setAdapter(adapterpacks);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
dialogfpacks.setContentView(view);
dialogfpacks.show();
}
public class CustomListAdapterSize extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
private List<ProductLandingSizeListModel> adaptersizelist;
private CheckBox selected =null;
private RadioButton mSelectedRB;
public CustomListAdapterSize(Context context,List<ProductLandingSizeListModel> movieItems) {
this.context = context;
this.adaptersizelist=movieItems;
}
#Override
public int getCount() {
return adaptersizelist.size();
}
#Override
public Object getItem(int position) {
return adaptersizelist.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.listitem_productsizes, null);
holder.txtproname = (TextView) convertView.findViewById(R.id.txtproductsizenames);
holder.txtproprice = (TextView) convertView.findViewById(R.id.txtproductsizeprice);
holder.radioBtn = (CheckBox)convertView.findViewById(R.id.productsize_radio);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
final ProductLandingSizeListModel m = adaptersizelist.get(position);
holder.txtproname.setText(m.getSizeList_textUpper());
holder.txtproprice.setText(m.getSizeList_textLower());
if (position == mSelectedPosition ) {
if (selected == null) {
holder.radioBtn.setChecked(true);
selected = holder.radioBtn;
}
}
if (position == mSelectedPosition )
holder.radioBtn.setChecked(true);
else
holder.radioBtn.setChecked(false);
holder.radioBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (selected != null) {
selected.setChecked(false);
}
mSelectedPosition =position;
if(mSelectedPositionpack!=0)
{
mSelectedPositionpack=0;
}
holder.radioBtn.setChecked(true);
selected = holder.radioBtn;
notifyDataSetChanged();
dialogf.dismiss();
showDialogpacks();
}
});
return convertView;
}
class ViewHolder{
TextView txtproname,txtproprice;
public CheckBox radioBtn;
}
}
public class CustomListAdaptertwo extends BaseAdapter {
private CheckBox selected =null;
private Context context;
private LayoutInflater inflater;
private List<ProductLandingSizeListModel> adaptersizelist;
public CustomListAdaptertwo(Context context,List<ProductLandingSizeListModel> adaptersizelist) {
this.context = context;
this.adaptersizelist=adaptersizelist;
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
if (adaptersizelist != null) {
return adaptersizelist.size();
}
return 0;
}
#Override
public Object getItem(int position) {
return adaptersizelist.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.custom_spinnertwo, null);
holder.txtproname = (TextView) convertView.findViewById(R.id.text_main_seentwo);
holder.txtproprice = (TextView) convertView.findViewById(R.id.sub_text_seentwo);
holder.checkpack = (CheckBox)convertView.findViewById(R.id.productpack_radio);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
final ProductLandingSizeListModel m = adaptersizelist.get(position);
for(int i=0;i<adaptersizelist.get(position).getPacklistmodel().size();i++)
{
System.out.println("PACK na data"+adaptersizelist.get(position).getPacklistmodel().get(i).getPackList_textUpper());
holder.txtproname.setText(adaptersizelist.get(position).getPacklistmodel().get(i).getPackList_textUpper());
holder.txtproprice.setText(adaptersizelist.get(position).getPacklistmodel().get(i).getPackList_textLower());
}
if (position == mSelectedPositionpack ) {
if (selected == null) {
holder.checkpack.setChecked(true);
selected = holder.checkpack;
}
}
if (position == mSelectedPositionpack )
holder.checkpack.setChecked(true);
else
holder.checkpack.setChecked(false);
holder.checkpack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (selected != null) {
selected.setChecked(false);
}
mSelectedPositionpack =position;
holder.checkpack.setChecked(true);
selected = holder.checkpack;
// selected_pack_text.setText(movieItems.get(position)+" - "+"$"+packItems.get(position));
notifyDataSetChanged();
dialogfpacks.dismiss();
}
});
return convertView;
}
class ViewHolder{
TextView txtproname,txtproprice;
CheckBox checkpack;
}
}

Change your showDialogpacks like this.On click of the first dialog pass the position that is been clicked and then inside that pass only the item
inside sizelists for particular position.
private void showDialogpacks(int position){
dialogfpacks = new Dialog(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_main_packs, null);
ListView lv = (ListView) view.findViewById(R.id.custom_list_packs);
Button close_btn = (Button) view.findViewById(R.id.close_btn_packlist);
adapterpacks = new CustomListAdaptertwo(getActivity(), sizelists.get(position).getPacklistmodel());
lv.setAdapter(adapterpacks);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
dialogfpacks.setContentView(view);
dialogfpacks.show();
}
Also change the second adapter list type to ProductLandingPackListModel.
public CustomListAdaptertwo(Context context,List<ProductLandingPackListModel> adaptersizelist) {
this.context = context;
this.adaptersizelist=adaptersizelist;
inflater = LayoutInflater.from(context);
}
And in the inside of the CustomAdapdtertwo getView method
final ProductLandingPackListModel model = adaptersizelist.get(position);
holder.txtproname.setText(model.getPackList_textUpper());
holder.txtproprice.setText(model.getPackList_textLower());

Hi Chris based upon size selection set packlist...update pack list of every size user selects

Related

How to get selected item from GridView and show in ListView

I have GridView which contain the number of items, If user click on save button then selected value show in ListView, I tried but not got succeied, Can any one help to solve this problem?
MainActivity.java
fillDataMyProperty();
if (myPropertyArrayList != null && myPropertyArrayList.size() > 0) {
myPropertListAdapter = new MyPropertListAdapter(MainActivity.this, myPropertyArrayList, txtSubmit);
gridMyProperty.setAdapter(myPropertListAdapter);
}
My GridAdapter is
public class MyPropertListAdapter extends BaseAdapter {
private Context context;
private ArrayList<ItemModel> arrayList;
private boolean check = false;
private View view;
private TextView txtSubmit;
public MyPropertListAdapter(Context context, ArrayList<ItemModel> arrayList, TextView txtSubmit) {
this.context = context;
this.arrayList = arrayList;
this.txtSubmit = txtSubmit;
}
#Override
public int getCount() {
return arrayList.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public int getViewTypeCount() {
return super.getViewTypeCount();
}
#SuppressLint("InflateParams")
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.grid_item_row, null);
viewHolder = new ViewHolder();
viewHolder.txtItem = (TextView) convertView.findViewById(R.id.txtItem);
viewHolder.rlMain = (RelativeLayout) convertView.findViewById(R.id.rlMain);
viewHolder.imgCheck = (ImageView) convertView.findViewById(R.id.imgCheck);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txtItem.setText(arrayList.get(position).getItem_name());
if (arrayList.get(position).isCheck() == true) {
viewHolder.txtItem.setBackgroundResource(R.drawable.ic_green_facility);
Glide.with(context).load(R.drawable.ic_facility_check).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).dontAnimate().into(viewHolder.imgCheck);
} else if (arrayList.get(position).isCheck() == false) {
viewHolder.txtItem.setBackgroundResource(R.drawable.ic_white_facility);
viewHolder.imgCheck.setVisibility(View.GONE);
}
viewHolder.rlMain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
view = v;
if (arrayList.get(position).isCheck() == false) {
Log.d("if---------------------", String.valueOf(arrayList.get(position).isCheck()));
viewHolder.txtItem.setBackgroundResource(R.drawable.ic_green_facility);
viewHolder.imgCheck.setVisibility(View.VISIBLE);
Glide.with(context).load(R.drawable.ic_facility_check).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).dontAnimate().into(viewHolder.imgCheck);
arrayList.get(position).setCheck(true);
check = true;
} else if (arrayList.get(position).isCheck() == true) {
Log.d("else-------------------", String.valueOf(arrayList.get(position).isCheck()));
viewHolder.txtItem.setBackgroundResource(R.drawable.ic_white_facility);
viewHolder.imgCheck.setVisibility(View.GONE);
arrayList.get(position).setCheck(false);
check = false;
}
}
});
txtSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showItemsDilaog();
}
});
return convertView;
}
public class ViewHolder {
TextView txtItem;
RelativeLayout rlMain;
ImageView imgCheck;
}
private void showItemsDilaog() {
final Dialog mDialog = new Dialog(context);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
mDialog.setContentView(R.layout.dialog_list_item);
ImageView imgCancel = (ImageView) mDialog.findViewById(R.id.imgCancel);
ListView listItem = (ListView) mDialog.findViewById(R.id.listItem);
if (arrayList != null && arrayList.size() > 0) {
ItemsAdapter mAdapter = new ItemsAdapter(context, arrayList);
listItem.setAdapter(mAdapter);
}
imgCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDialog.dismiss();
}
});
mDialog.show();
}}
ItemsAdapter.Java
class ItemsAdapter extends BaseAdapter {
// private String[] mStrings = new String[]{"Elevator", "Air Conditionor", "Electricity", "Houe Keeping", "Garden", "All"};
private ViewHolder viewHolder;
private Context context;
private ArrayList<ItemModel> mArrayList;
public ItemsAdapter(Context context, ArrayList<ItemModel> maintananceModelArrayList) {
this.context = context;
this.mArrayList = maintananceModelArrayList;
}
#Override
public int getCount() {
return mArrayList.size();
}
#Override
public Object getItem(int position) {
return mArrayList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_row, null);
viewHolder = new ViewHolder();
viewHolder.txtItem = (TextView) convertView.findViewById(R.id.txtItem);
viewHolder.lineSeperator = (View) convertView.findViewById(R.id.lineSeperator);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (arrayList.get(position).isCheck() == true) {
viewHolder.txtItem.setText(arrayList.get(position).getItem_name());
Log.d("Items:::::::::::::::",arrayList.get(position).getItem_name());
}
if (position == arrayList.size() - 1) {
viewHolder.lineSeperator.setVisibility(View.GONE);
} else {
viewHolder.lineSeperator.setVisibility(View.VISIBLE);
}
return convertView;
}
public class ViewHolder {
TextView txtItem;
View lineSeperator;
}
}

How to avoid default text issue in Custom Spinner Android?

i have done custom spinner with custom adapter in it but getting some default text everytime how to avoid this issue everytime it should only display 0th position text on spinner:
CourseAdapter courseAdapter = new CourseAdapter(mContext);
customViewHolder.spinner.setAdapter(courseAdapter);
while CourseAdapter.java
public class CourseAdapter extends BaseAdapter {
public static final String TAG=CourseAdapter.class.getName();
List<String> courses =new ArrayList<>(); // code to get the courses ArrayList
Context context;
private void fillCourses()
{
for(int i =0;i<10;i++)
{
if(i == 0)
{
courses.add("Choose");
}
else
{
courses.add("courses:"+i);
}
}
}
public CourseAdapter(Context context)
{
this.context = context;
fillCourses();
}
#Override
public int getCount() {
return courses.size();
}
#Override
public Object getItem(int position) {
return courses.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderFilterItem viewHolder;
if (convertView == null) {
Log.d(TAG, "fillCourses size:" + courses.size());
convertView = LayoutInflater.from(context)
.inflate(R.layout.layout_customspinner, parent, false);
viewHolder = new ViewHolderFilterItem();
viewHolder.textView = (TextView) convertView.findViewById(R.id.txtViewFilterItem);
viewHolder.checkBox = (CheckBox)convertView.findViewById(R.id.checkboxFilterItem);
//store the holder with the view.
convertView.setTag(viewHolder);
return convertView;
}
else
{
Log.d(TAG, "convertview != null:" + courses.size());
viewHolder = (ViewHolderFilterItem) convertView.getTag();
}
if(courses != null && courses.size() > 0) {
String itemText = courses.get(position);
if (itemText != null) {
viewHolder.textView.setText(itemText);
viewHolder.checkBox.setChecked(true);
}
if(position == 0)
{
viewHolder.checkBox.setVisibility(View.GONE);
}
}
return convertView;
}
private class ViewHolderFilterItem{
TextView textView;
CheckBox checkBox;}
}[![[why Filter item text with checkbox appearing ?][1]][1]
you have to use courseAdapter.notifyDataSetChanged() after setting customViewHolder.spinner.setAdapter(courseAdapter);
Hope this helps.

Maintain the state of selected listview Imagebutton on scrolling

I try to implement add to favorites functionality in my app .I have two Image-buttons addToFavouritiesBtn and removeFromFavouritiesBtn in list-view . At a time only one Image-button get visible on selection.
My problem is , when I select addToFavouritiesBtn it gets selected , but when I scroll-down list-view and come back to that Image-button , it remains unselected.
Please tell me how to maintain state of the selected items on scrolling.
Here is my code.
public class Product{
private boolean selected = false;
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
ProdctListAdaptet.class
public class ProductListAdapter extends BaseAdapter {
private Activity activity;
private List<Product> productItems;
private Context context;
int pos;
int flag = 0 ;
public ProductListAdapter(Context context,Activity activity, List<Product> productItems,int flag) {
this.activity = activity;
this.productItems = productItems;
this.context = context;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// ProductHolder holder = null;
final ProductHolder holder;
if (convertView == null) {
holder = new ProductHolder();
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.product_list_row_item, parent,false);
holder.thumbNail = (ImageView) convertView
.findViewById(R.id.product_image);
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.btnAddFavourite = (ImageButton) convertView.findViewById(R.id.AddFavourites);
holder.btnRemoveFavourite = (ImageButton) convertView.findViewById(R.id.RemoveFavourites);
holder.btnAddFavourite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
flag = 1;
System.out.println("Add favourities");
pos = (Integer) v.getTag();
productItems.get(pos).setSelected(false);
holder.btnAddFavourite.setVisibility(View.GONE);
holder.btnRemoveFavourite.setVisibility(View.VISIBLE);
//notifyDataSetChanged();
}
});
holder.btnRemoveFavourite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
flag = 2;
System.out.println("Remove favorities");
pos = (Integer) v.getTag();
productItems.get(pos).setSelected(true);
holder.btnAddFavourite.setVisibility(View.VISIBLE);
holder.btnRemoveFavourite.setVisibility(View.GONE);
// notifyDataSetChanged();
}
});
convertView.setTag(holder);
} else {
holder = (ProductHolder) convertView.getTag();
}
holder.btnAddFavourite.setTag(position);
holder.btnRemoveFavourite.setTag(position);
Product state = productItems.get(position);
System.out.println("Product position -----> " + state);
if (state.isSelected()) {
System.out.println("Selected state ******************* " + state.isSelected());
System.out.println("selected");
holder.btnAddFavourite.setVisibility(View.VISIBLE);
holder.btnRemoveFavourite.setVisibility(View.GONE);
} else {
System.out.println("Selected state ******************* " + state.isSelected());
System.out.println(" not selected");
holder.btnAddFavourite.setVisibility(View.GONE);
holder.btnRemoveFavourite.setVisibility(View.VISIBLE);
}
convertView.setTag(holder);
return convertView;
}
final Product product = (Product) productItems.get(position);
//final Product product = (Product) getItem(position);
holder.thumbNail.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
holder.title.setText(product.getName());
Picasso.with(activity).load(product.getHref())
.placeholder(R.drawable.ic_default_banner)
.into(holder.thumbNail);
private class ProductHolder {
TextView title;
ImageView thumbNail;
ImageButton btnAddFavourite;
ImageButton btnRemoveFavourite;
}
}
you have to use youradapetr.notifydatasetchanged() after change in dataset in your button onclick function
, in your case simply add notifyDataSetChanged(); like below in both function
holder.btnAddFavourite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("Add favourities");
int pos = (Integer) v.getTag();
productItems.get(pos).setSelected(false);
holder.btnAddFavourite.setVisibility(View.GONE);
holder.btnRemoveFavourite.setVisibility(View.VISIBLE);
notifyDataSetChanged();
}
});
I think the problem is inside the onClickListener. Use position which is coming as parameters instead of pos for setting the state inside your listeners.
holder.btnAddFavourite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("Add favourities");
productItems.get(position).setSelected(false);
holder.btnAddFavourite.setVisibility(View.GONE);
holder.btnRemoveFavourite.setVisibility(View.VISIBLE);
}
});
holder.btnRemoveFavourite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("Remove favorities");
productItems.get(position).setSelected(true);
holder.btnAddFavourite.setVisibility(View.VISIBLE);
holder.btnRemoveFavourite.setVisibility(View.GONE);
}
});
EDIT 1:-
Can your try below getView method. I think you are setting incorrect state in onclick listener. You are setting setSelected(true) in btnRemoveFavourite but it should be setSelected(false). Same applies for btnAddFavorite.
Please try below code and accept the answer if its correct.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ProductHolder holder = null;
if (convertView == null) {
holder = new ProductHolder();
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.product_list_row_item, parent, false);
holder.thumbNail = (ImageView) convertView.findViewById(R.id.product_image);
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.btnAddFavourite = (ImageButton) convertView.findViewById(R.id.AddFavourites);
holder.btnRemoveFavourite = (ImageButton) convertView.findViewById(R.id.RemoveFavourites);
convertView.setTag(holder);
} else {
holder = (ProductHolder) convertView.getTag();
}
final Product product = productItems.get(position);
holder.btnAddFavourite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
product.setSelected(true);
holder.btnAddFavourite.setVisibility(View.GONE);
holder.btnRemoveFavourite.setVisibility(View.VISIBLE);
}
});
holder.btnRemoveFavourite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
product.setSelected(false);
holder.btnAddFavourite.setVisibility(View.VISIBLE);
holder.btnRemoveFavourite.setVisibility(View.GONE);
}
});
if (product.isSelected()) {
System.out.println("Selected state ******************* " + state.isSelected());
System.out.println("selected");
holder.btnAddFavourite.setVisibility(View.GONE);
holder.btnRemoveFavourite.setVisibility(View.VISIBLE);
} else {
System.out.println("Selected state ******************* " + state.isSelected());
System.out.println(" not selected");
holder.btnAddFavourite.setVisibility(View.VISIBLE);
holder.btnRemoveFavourite.setVisibility(View.GONE);
}
return convertView;
}
The Issue:
getView() in your adapter gets called from time to time. As a result, if the selected state is not set for the button, it will revert to the original state.
Solution:
Keep a boolean in the model class [Product] with getters and setters:
boolean didSelect;
public boolean isDidSelect() {
return didSelect;
}
public void setDidSelect(boolean didSelect) {
this.didSelect = didSelect;
}
In getView(), depending on the value in the boolean set the selection of the button.
Here i am giving a simple demo for you,You Can update it according your requirement,I hope you will get your Solution.
public class MainActivity extends Activity {
String s[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
"M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y",
"Z" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Product> products = new ArrayList<Product>();
ListView list = (ListView) findViewById(R.id.listview);
for (int i = 0; i < s.length; i++) {
Product item = new Product();
item.setCheck(false);
item.setName(s[i].toString());
products.add(item);
}
ProductListAdapter ac = new ProductListAdapter(MainActivity.this, R.layout.row,
products);
list.setAdapter(ac);
}// on create method end
class ProductListAdapter extends BaseAdapter {
private List<Product> productItems;
Context context;
int layoutId;
public ProductListAdapter(Context context, int layoudId, List<Product> productItems) {
// super(context,productItems);
this.layoutId = layoudId;
this.context = context;
this.productItems = productItems;
}
public class ProductHolder {
ImageView image;
TextView name;
}
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
final ProductHolder holder;
Product rowitems = productItems.get(position);
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(layoutId, parent, false);
holder = new ProductHolder();
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.image = (ImageView) convertView
.findViewById(R.id.invite_click);
holder.image.setBackgroundResource(R.drawable.green);
convertView.setTag(holder);
}
else {
holder = (ProductHolder) convertView.getTag();
}
holder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (productItems.get(position).isCheck) {
productItems.get(position).isCheck = false;
// holder.image.setImageBitmap(null);
holder.image.setBackgroundResource(R.drawable.green);
} else {
productItems.get(position).isCheck = true;
holder.image
.setBackgroundResource(R.drawable.green_tick);
}
}
});
if (productItems.get(position).isCheck) {
productItems.get(position).isCheck = true;
holder.image.setBackgroundResource(R.drawable.green_tick);
} else {
productItems.get(position).isCheck = false;
holder.image.setImageBitmap(null);
holder.image.setBackgroundResource(R.drawable.green);
}
holder.name.setText(rowitems.getName());
Log.e("name----->", productItems.get(position).toString());
return convertView;
}
#Override
public int getCount() {
return productItems.size();
}
#Override
public Object getItem(int position) {
return productItems.get(position);
}
#Override
public long getItemId(int position) {
return productItems.indexOf(getItem(position));
}
}
}

callback to notifyDataSetChanged

I implemented a listview inside DialogFragment. Each item has a textview with a selector to simulate a radio group with option to select only one item at a time.
The problem is when the user clicks on an item I am updating the data on the adapter then calling to notifyDataSetChanged() and calling dialog.dismiss().
The user don`t see the selection of item before the dialog is dismissed.
I fixed this by adding postdelay to dismiss functionality, Maybe there is a better solution for this problem?
This is my code:
public class SettingsPopupAdapter extends BaseAdapter
{
private List<SettingsItem> _data;
private Context _con;
public SettingsPopupAdapter(Context con, List<SettingsItem> data)
{
_con = con;
_data = data;
}
#Override
public int getCount()
{
return _data.size();
}
#Override
public SettingsItem getItem(int position)
{
return _data.get(position);
}
#Override
public long getItemId(int position)
{
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if (convertView == null)
{
holder = new ViewHolder();
convertView = LayoutInflater.from(_con).inflate(R.layout.settings_popup_checkbox_row_item, parent, false);
holder._text = (TextView) convertView.findViewById(R.id.option_text);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
SettingsItem item = getItem(position);
holder._text.setText(item.getItemLabel());
holder._text.setSelected(item.isSelected());
return convertView;
}
private static class ViewHolder
{
public TextView _text;
}
public String getSelectedItem()
{
String selectedItem = null;
for (SettingsItem item : _data)
{
if (item.isSelected())
{
selectedItem = item.getItemLabel();
break;
}
}
return selectedItem;
}
public List<String> getSelectedItems()
{
List<String> selectedOptions = new ArrayList<String>();
for (SettingsItem item : _data)
{
if (item.isSelected())
{
selectedOptions.add(item.getItemLabel());
}
}
return selectedOptions;
}
public void setData(ArrayList<SettingsItem> items)
{
_data = items;
notifyDataSetChanged();
}
}
This code is in the fragment from where I am opening the dialog :
private SettingsPopupAdapte _modAdapter;
private ArrayList<SettingsItem> _items;
_modAdapter = new SettingsPopupAdapter(getActivity(),_items);
_modificationListView.setAdapter(_modAdapter);
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
String selectedItem = _items.get(position).getItemLabel();
selectExclusiveList(selectedItem);
Bundle b = new Bundle();
b.putInt(Consts.MODIFICATION_TYPE_KEY, _type);
b.putString(Consts.RESPONSE_DATA_KEY,selectedItem);
_listener.onRetrieveData(b);
//show the change before the dialog dismissed
getView().postDelayed(new Runnable()
{
#Override
public void run()
{
dismiss();
}
},200);
}
private void selectExclusiveList(String selectedItem)
{
for (SettingsItem item : _items)
{
item.setSelected(false);
if (item.getItemLabel().equals(selectedItem))
{
item.setSelected(true);
}
}
_modAdapter.setData(_items);
}

How to change textview of custom listview on clicking-android

I did an android application which has custom listview with checkbox & text per line.Now i want to change text color on clicking of both text or checkbox if any.How could i do this?
My Code:
String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LSOne = (ListView) findViewById(R.id.listView1);
planetList.addAll(Arrays.asList(planets));
// Create ArrayAdapter using the planet list.
LsAdapter listAdapter = new LsAdapter(this, R.layout.country_info,
planetList);
LSOne.setAdapter(listAdapter);
LSOne.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View arg1,
int position, long id) {
}
});
}
public class LsAdapter extends ArrayAdapter<String> {
private LayoutInflater mInflater;
private String[] mTaxi = null;
private String[] mid = null;
long id;
public static final boolean isEnabled = true;
private int mViewResourceId;
public LsAdapter(Context ctx, int viewResourceId,
ArrayList<String> planetList) {
super(ctx, viewResourceId);
mInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
String[] tax = planetList.toArray(new String[planetList.size()]);
mTaxi = tax;
mViewResourceId = viewResourceId;
}
#Override
public int getCount() {
return mTaxi.length;
}
#Override
public String getItem(int position) {
return mTaxi[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 20;
}
#Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
int _intPosition = getItemViewType(position);
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.country_info, null);
holder = new ViewHolder();
holder.code = (TextView) convertView.findViewById(R.id.textView1);
holder.name = (RadioButton) convertView.findViewById(R.id.radioButton1);
convertView.setTag(holder);
holder.code.setText(mTaxi[position]);
holder.name.setId(_intPosition);
if (flag == 1) {
holder.name.setEnabled(false);
} else if (flag == 0) {
holder.name.setEnabled(true);
}
holder.name.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
for (int i = 0; i < _intRadio.length; i++) {
if (i == v.getId()) {
_intRadio[i] = 1;
} else {
_intRadio[i] = 0;
}
}
holder.code.setTextColor(Color.parseColor("#008000"));
notifyDataSetChanged();
}
});
holder.code.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
holder.code.setTextColor(Color.parseColor("#008000"));
// v.setBackgroundColor(Color.BLUE);
notifyDataSetChanged();
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
if (_intRadio[_intPosition] == 1) {
holder.name.setChecked(true);
} else {
holder.name.setChecked(false);
}
return convertView;
}
private class ViewHolder {
TextView code;
RadioButton name;
Button btn;
}
}}
Thanks.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
// ROW INFLATION
Log.d("ExamAdapter", "Starting XML Row Inflation ... ");
LayoutInflater inflater = (LayoutInflater) this.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.custom_gold, parent, false);
Log.d("ExamAdapter", "Successfully completed XML Row Inflation!");
}
// Get item
Gold text = getItem(position);
subject = (TextView) row.findViewById(R.id.textView1);
time = (TextView) row.findViewById(R.id.textView2);
String content = text.content;
String times = text.time;
subject.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
subject.setBackgroundColor(R.color.red_color);
}
});
/* String[] com = content.split(Pattern.quote("*")); */
subject.setText(content.replace("*", "\n"));
return row;
}
Try This code..
Write subject.setBackgroundColor(R.color.red_color); this line inside the onClick listener...
Before notifyDataSetChanged(); in ur code, get the position and store it in a static variable. And before return convertView; check the position with the static variable and try holder.name.setBackgroundColor(<color>) . I think it ll work.
Inside your onClick listener... try like this
holder.name.setTextColor(Color.parseColor("#008000"));
Hope this will work

Categories

Resources