Android checkbox.isChecked() does not work properly - java

I have a ListView in which a checkbox is displayed in each row.
Each and every time the checkbox is touched, I check wheteher it is checked or not.
But every time, the first item always return false. However, If if checked the 2nd item, the .ischecked() method of the checkbox of the first item alway return true.
Here is my code:
public class CustomSpecificCar extends ArrayAdapter<JSONObject>{
ListSpecificCars caller;
private JSONObject currentJson;
private CheckBox cbSelectedCar;
private int position;
public CustomSpecificCar(Context ctxt, List<JSONObject> list, ListSpecificCars caller){
super(ctxt, R.layout.custom_specific_car_row, list);
this.caller = caller;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View customView = inflater.inflate(R.layout.custom_specific_car_row, parent, false);
this.position = position;
// Set the reference of the layout
currentJson = getItem(this.position);
cbSelectedCar = (CheckBox)customView.findViewById(R.id.cbSelectedCar);
TextView tvBrand = (TextView)customView.findViewById(R.id.tvBrand);
TextView tvModel = (TextView)customView.findViewById(R.id.tvModel);
TextView tvOwnerEditable = (TextView)customView.findViewById(R.id.tvOwnerEditable);
TextView tvPriceEditable = (TextView)customView.findViewById(R.id.tvEstimatedPriceEditable);
try {
tvBrand.setText(currentJson.getString("brand"));
tvModel.setText(currentJson.getString("model"));
tvOwnerEditable.setText(currentJson.getString("owner"));
} catch (JSONException e) {
Log.e(e.getClass().getName(), "JSONException", e);
}
cbSelectedCar.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
if (cbSelectedCar.isChecked()){
caller.updateClickedUsername(currentJson, true); // Add to the List
Log.d("Added click ", "ok");
}
else if (!cbSelectedCar.isChecked()) {
caller.updateClickedUsername(currentJson, false); // Delete from the List
Log.d("Deleted click ", "nok");
}
}});
return customView;
}
}
What should I do to solve this issue?
Many thanks in advance!

You should use setOnCheckedChangeListener.
Sample:
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
}
});
In your code:
cbSelectedCar.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked){
caller.updateClickedUsername(currentJson, true); // Add to the List
Log.d("Added click ", "ok");
}
else if (!isChecked) {
caller.updateClickedUsername(currentJson, false); // Delete from the List
Log.d("Deleted click ", "nok");
}
}
});

For using checked box as a Onclicklistener this will help you
CheckBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
Toast.makeText(this, "CheckBox is checked", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "CheckBox is not checked", Toast.LENGTH_SHORT).show();
}
});

Related

How do can I use this reload() method to the onLongClick method?

I am applying a delete function to my notes app, but when I delete something, it doesnt changes on the screen until I do some action that reloads it, like creating a new note, or accesing a note and the exiting it (even the one deleted). Only then, the deleted note will disappear, even though I clearly used the reload function. Here is my adapter, where I have the problem.
public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.NoteViewHolder> {
public static class NoteViewHolder extends RecyclerView.ViewHolder {
public LinearLayout containerView;
public TextView nameTextView;
public NotesAdapter adapter = new NotesAdapter();
public NoteViewHolder(View view) {
super(view);
this.containerView = view.findViewById(R.id.note_row);
this.nameTextView = view.findViewById(R.id.note_row_name);
this.containerView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(final View view) {
final Note note = (Note) containerView.getTag();
PopupMenu popupMenu = new PopupMenu(view.getContext(), view);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
if (menuItem.getItemId() == R.id.delete) {
Toast.makeText(view.getContext(), "Note deleted", Toast.LENGTH_SHORT).show();
MainActivity.database.noteDao().delete(note.id);
adapter.reload;
return true;
} else {
return false;
}
}
});
popupMenu.inflate(R.menu.delete_menu);
popupMenu.show();
return true;
}
});
this.containerView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Context context = v.getContext();
Note note = (Note) containerView.getTag();
Intent intent = new Intent(v.getContext(), NoteActivity.class);
intent.putExtra("id", note.id);
intent.putExtra("content", note.content);
context.startActivity(intent);
}
});
}
}
private List<Note> notes = new ArrayList<>();
#Override
public NoteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.note_row, parent, false);
return new NoteViewHolder(view);
}
#Override
public void onBindViewHolder(NoteViewHolder holder, int position) {
Note current = notes.get(position);
holder.containerView.setTag(current);
holder.nameTextView.setText(current.content);
}
#Override
public int getItemCount() {
return notes.size();
}
public void reload() {
notes = MainActivity.database.noteDao().getAll();
notifyDataSetChanged();
}
}
Please help me, I ve working on this for days now, and I am stuck at a simple problem
Reload data after the database has been updated. Invoke reload() after deleting a note:
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
if (menuItem.getItemId() == R.id.delete) {
Toast.makeText(view.getContext(), "Note deleted", Toast.LENGTH_SHORT).show();
MainActivity.database.noteDao().delete(note.id);
reload(); // reload data
return true;
} else {
return false;
}
}
});

Using Checkbox with recyclerview

I am new to android. I am having trouble with recyclerview with check boxes.
I am using a recycler view that has check boxes. Now the titles with checked check boxes should appear in my activity. I have added the check box selected title in the array list how do I get them in the activity?
I just need the checked check boxes array list in my activity.
below is my adapter class
public class CheckboxAdapter extends
RecyclerView.Adapter<CheckboxAdapter.ViewHolder> {
private List<SearchListItem> searchListItems;
private OnSearchItemSelected onSearchItemSelected;
public ArrayList<String> addTitle;
CheckboxAdapter(List<SearchListItem> arrayList, OnSearchItemSelected onSearchItemSelected) {
this.searchListItems = arrayList;
this.onSearchItemSelected = onSearchItemSelected;
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
#BindView(R.id.title)
TextView mTxtTitle;
#BindView(R.id.checkBox)
CheckBox checkBox;
ViewHolder(View row) {
super(row);
ButterKnife.bind(this, row);
row.setOnClickListener(this);
}
#Override
public void onClick(View view) {
int pos = getAdapterPosition();
SearchListItem searchListItem = searchListItems.get(pos);
try {
onSearchItemSelected.onClick(pos, searchListItem);
} catch (Exception e) {
e.printStackTrace();
}
}
}
#Override
public int getItemCount() {
// TODO Auto-generated method stub
return searchListItems.size();
}
#Override
public void onBindViewHolder(#NonNull final CheckboxAdapter.ViewHolder holder, final int position) {
SearchListItem output = searchListItems.get(position);
holder.mTxtTitle.setText(output.title);
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
addAllTitle(holder);
}
}
});
}
public OnSearchItemSelected getOnSearchItemSelected() {
return onSearchItemSelected;
}
public void addAllTitle(ViewHolder holder) {
for (int i = 0; i < addTitle.size(); i++) {
addTitle.add(String.valueOf(holder.mTxtTitle));
}
}
#NonNull
#Override
public CheckboxAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int arg1) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View contactView = inflater.inflate(R.layout.adapter_checkbox, parent, false);
return new CheckboxAdapter.ViewHolder(contactView);
}
}
you have to add one boolean in your model class such as isChecked
make it true if checkBox is checked else false
get same list which is passed in activity to adapter, this list will give you all checked boxes
for ex
in activity
adapter = new MyAdapter(myList);
/*you can get all checked boxes here, paste this code where you need it*/
for(i=0; i<myList.size();i++){
if(myList.isChecked()){
//... do somehing ...
}
}
in adapter
onBindViewHolder() method
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
myList.setchecked(isChecked);
}
});

switch state is not stable in listview when scrolling

I'am new to android and i use sample for my listview. because of that i can't change the code. i have one switch button in each item of my Listview. when i scroll the Listview switches change state randomly. how can i make switch state stable?
my listview class adapter:
public class MyCustomCursorAdapter extends CursorAdapter {
LIGHTS calling_activity;
private DatabaseHelper dbHelper;
public MyCustomCursorAdapter(Context context, Cursor c) {
super(context, c, 0);
this.calling_activity = (LIGHTS) context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
return view;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.adapter,parent,false);
}
#Override
public void bindView(final View view, Context context, Cursor cursor) {
((TextView)view.findViewById(R.id.id)).setText(cursor.getString(cursor.getColumnIndex(dbHelper._ID)));
((TextView)view.findViewById(R.id.KEYCODE)).setText(cursor.getString(cursor.getColumnIndex(dbHelper.TITLE)));
((TextView)view.findViewById(R.id.NAME)).setText(cursor.getString(cursor.getColumnIndex(dbHelper.DESC)));
ImageView option = view.findViewById(R.id.itemoption);
Switch thisswitch = view.findViewById(R.id.onoff);
thisswitch.setTag(cursor.getString(cursor.getColumnIndex(dbHelper._ID)));
thisswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { calling_activity.myOnCheckedChangedHandler((String)buttonView.getTag(),isChecked);
}
});
option.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView itemID = (TextView) view.findViewById(R.id.id);
TextView itemTitle = (TextView) view.findViewById(R.id.KEYCODE);
TextView itemDesc = (TextView) view.findViewById(R.id.NAME);
String myId = itemID.getText().toString();
String myTitle = itemTitle.getText().toString();
String myDesc = itemDesc.getText().toString();
Intent intent = new Intent(calling_activity, ModifyActivity.class);
intent.putExtra("Id", myId);
intent.putExtra("Title", myTitle);
intent.putExtra("Desc", myDesc);
calling_activity.startActivity(intent);
}
});
}
}
and in my Lights activity :
#Override
public void myOnCheckedChangedHandler(String id, boolean check_status) {
Toast.makeText(
this,
"You changed the status for the row with an id of " + id +
" the status is now " + new Boolean(check_status).toString(),
Toast.LENGTH_SHORT).show();
String name = cursor.getString(cursor.getColumnIndex(dbHelper.DESC));
if(new Boolean(check_status).toString().equals("true")){
Toast.makeText(this,name +" is ON", Toast.LENGTH_SHORT).show();
}
}
Please manage through if/else case inside bindView at adapter, You need to check the switch condition like below :
// set the code into bind
if(switchCondition1 == 1)
{
//set the code as per condition wise
}else{
//
}
After change the state you also need to change the state into your list item and use notifydatasetChanged() method to refresh the list items.

Radio Button gets uncked and automatically checked in listview scrolling

i have a program in which i have multiple questions and 2 options (radiobuttons) when i check 1 and scroll listview it gets unchecked. i will post the whole code kindly fix it for me. I think its something wrong with adapter class
MainActivity
public class MainActivity extends AppCompatActivity {
ListView simpleList;
String[] questions;
Button submit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the string array from string.xml file
questions = getResources().getStringArray(R.array.questions);
// get the reference of ListView and Button
simpleList = (ListView) findViewById(R.id.simpleListView);
submit = (Button) findViewById(R.id.submit);
// set the adapter to fill the data in the ListView
CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), questions);
simpleList.setAdapter(customAdapter);
// perform setOnClickListerner event on Button
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String message = "";
// get the value of selected answers from custom adapter
for (int i = 0; i < CustomAdapter.selectedAnswers.size(); i++) {
message = message + "\n" + (i + 1) + " " + CustomAdapter.selectedAnswers.get(i);
}
// display the message on screen with the help of Toast.
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
});
}
Custom Adapter
public class Questions_Adapter extends BaseAdapter {
Activity context;
String[] questionsList;
String age_range;
LayoutInflater inflter;
public static ArrayList<Integer> selectedAnswers;
public Questions_Adapter(Activity applicationContext, String[] questionsList, String age_range) {
context = applicationContext;
this.questionsList = questionsList;
this.age_range = age_range;
// initialize arraylist and add string for all the questions
selectedAnswers = new ArrayList<>();
for (int i = 0; i < questionsList.length; i++) {
selectedAnswers.add(0);
}
inflter = (LayoutInflater.from(applicationContext));
}
#Override
public int getCount() {
return questionsList.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
private class Viewholder{
TextView question;
RadioButton op1, op2, op3, op4, op5, op6;
}
#Override
public View getView(final int position, View view, ViewGroup viewGroup) {
Viewholder viewholder;
LayoutInflater inflater = context.getLayoutInflater();
if (view == null){
viewholder = new Viewholder();
view = inflater.inflate(R.layout.list_items,null);
viewholder.question = (TextView) view.findViewById(R.id.question);
viewholder.op1 = (RadioButton) view.findViewById(R.id.yes);
viewholder.op2 = (RadioButton) view.findViewById(R.id.no);
viewholder.op3 = (RadioButton) view.findViewById(R.id.op);
viewholder.op4 = (RadioButton) view.findViewById(R.id.op4);
viewholder.op5 = (RadioButton) view.findViewById(R.id.op5);
viewholder.op6 = (RadioButton) view.findViewById(R.id.op6);
view.setTag(viewholder);
}
else {
viewholder = (Viewholder) view.getTag();
}
// perform setOnCheckedChangeListener event on yes button
viewholder.op1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// set Yes values in ArrayList if RadioButton is checked
if (isChecked)
selectedAnswers.set(position, 1);
}
});
viewholder.op2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
selectedAnswers.set(position, 2);
}
});
viewholder.op3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
selectedAnswers.set(position, 3);
}
});
viewholder.op4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
selectedAnswers.set(position, 4);
}
});
viewholder.op5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
selectedAnswers.set(position, 10);
}
});
viewholder.op6.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
selectedAnswers.set(position, 0);
}
});
// set the value in TextView
viewholder.op1.setText("None or Little of a time");
viewholder.op2.setText("Some of the time");
viewholder.op3.setText("A good part of the time");
viewholder.op4.setText("Most or All of the time");
viewholder.op5.setText("Yes");
viewholder.op6.setText("No");
viewholder.question.setText(questionsList[position]);
if(age_range.equals("ZSDS")){
viewholder.op5.setVisibility(View.GONE);
viewholder.op6.setVisibility(View.GONE);
}
if(age_range.equals("GDS")){
viewholder.op1.setVisibility(View.GONE);
viewholder.op2.setVisibility(View.GONE);
viewholder.op3.setVisibility(View.GONE);
viewholder.op4.setVisibility(View.GONE);
}
return view;
}
}
First of all you need to save the tick(checkbox selection) in your model, say isSelected = true & call notifyDataSetChanged. and then inside getView() method u need to check for this flag and set checkbox state based on that.
basically your view is regenerate after every scroll because you are not checking whether view is null or not.
You have to check first view and use holder instead of creating new object of widget every time . here is solution hope , it will work with your listview.
public class CustomAdapter extends BaseAdapter {
static class ViewHolder {
TextView question ;
RadioButton yes,no;
}
Context context;
String[] questionsList;
LayoutInflater inflter;
public static List<String> selectedAnswers;
public CustomAdapter(Context applicationContext, String[] questionsList) {
context = applicationContext;
this.questionsList = questionsList;
// initialize arraylist and add static string for all the questions
selectedAnswers = new ArrayList<>();
for (int i = 0; i < questionsList.length; i++) {
selectedAnswers.add("Not Attempted");
}
inflter = (LayoutInflater.from(applicationContext));
}
#Override
public int getCount() {
return questionsList.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(final int i, View view, ViewGroup viewGroup) {
ViewHolder holder = new ViewHolder();
if(view==null){
view = inflter.inflate(R.layout.list_items, null);
// get the reference of TextView and Button's
holder.question = (TextView) view.findViewById(R.id.question);
holder.yes = (RadioButton) view.findViewById(R.id.yes);
holder.no = (RadioButton) view.findViewById(R.id.no);
// perform setOnCheckedChangeListener event on yes button
view.setTag(holder);
}else{
holder = (ViewHolder) view.getTag();
}
holder.yes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// set Yes values in ArrayList if RadioButton is checked
if (isChecked)
selectedAnswers.set(i, "Yes");
}
});
// perform setOnCheckedChangeListener event on no button
holder.no.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// set No values in ArrayList if RadioButton is checked
if (isChecked)
selectedAnswers.set(i, "No");
}
});
// set the value in TextView
question.setText(questionsList[i]);
return view;
}
}
When you scroll the view is recycled for displaying the items so the selection of the other old list item that went out of view is coming for the wrong items. So you need to reset the selection for Radio Button when the get view is called.
So you can add the following code in the getVeiw() Method for the selection to work properly
if(position < selectedAnswers.size() && selectedAnswers.get(position) != null) {
switch(selectedAnswers.get(position)) {
case 1:
viewholder.op1.setChecked(true);
break;
case 2:
viewholder.op2.setChecked(true);
break;
case 3:
viewholder.op3.setChecked(true);
break;
case 4:
viewholder.op4.setChecked(true);
break;
case 10:
viewholder.op5.setChecked(true);
break;
case 0:
viewholder.op6.setChecked(true);
break;
default:
viewholder.op1.setChecked(false);
viewholder.op2.setChecked(false);
viewholder.op3.setChecked(false);
viewholder.op4.setChecked(false);
viewholder.op5.setChecked(false);
viewholder.op6.setChecked(false);
}
}
This will set your selection of the radio button on scroll of the listview.

Flip Animation not working properly in GridView after onItemClick

I have A grid view. I have A row.xml, which I am inflating in Adapter class. My row xml has two layout (One is visible and another is invisible.) I am doing flip animation. As I click on the any item of gridview then flip animation is happening, and I am doing visible the hided layout, and hiding that visible layout.
The animation is working perfectly. But the issue is that as I am clicking on any item, and scrolling other items also flipping. So as I realize that I am not getting the specific position.
I tried the following code.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row;
final RecordHolder holder = new RecordHolder();
animation1 = AnimationUtils.loadAnimation(context, R.anim.to_middle);
animation2 = AnimationUtils.loadAnimation(context, R.anim.from_middle);
if (convertView == null) {
row = View.inflate(context, R.layout.row_phone_and_tablet1, null);
} else {
//row = View.inflate(context, R.layout.row_phone_and_tablet1, null);
row = convertView;
}
holder.frontImage = (ImageView) row.findViewById(R.id.myimage_imgvw);
holder.frontMobileName = (TextView) row.findViewById(R.id.name_txtvw);
holder.frontPrice = (TextView) row.findViewById(R.id.price_txtvw);
holder.backMobileName = (TextView) row
.findViewById(R.id.tv_device_name);
holder.descOne = (TextView) row.findViewById(R.id.tv_desc_first);
holder.descTwo = (TextView) row.findViewById(R.id.tv_desc_second);
holder.descThree = (TextView) row.findViewById(R.id.tv_desc_third);
holder.descFour = (TextView) row.findViewById(R.id.tv_desc_fourth);
holder.ccEmi = (TextView) row.findViewById(R.id.tv_price_ccemi);
holder.financeEmi = (TextView) row.findViewById(R.id.tv_price_finance);
holder.frontlayout = (LinearLayout) row.findViewById(R.id.frontlayout);
holder.backlayout = (LinearLayout) row.findViewById(R.id.backlayout);
holder.viewdetails = (Button) row.findViewById(R.id.viewdetails);
holder.tv_front_inr = (TextView) row.findViewById(R.id.tv_front_inr);
holder.tv_description = (TextView) row
.findViewById(R.id.tv_description);
holder.tv_price_ccemi_inr = (TextView) row
.findViewById(R.id.tv_price_ccemi_inr);
holder.tv_ccemi_price = (TextView) row
.findViewById(R.id.tv_ccemi_price);
holder.tv_price_finance_inr = (TextView) row
.findViewById(R.id.tv_price_finance_inr);
holder.tv_financeemi_price = (TextView) row
.findViewById(R.id.tv_financeemi_price);
holder.parentLayout = (LinearLayout) row
.findViewById(R.id.parentLayout);
final PhoneAndTabletItem phoneAndTableItem = data.get(position);
holder.viewdetails.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(context, ViewPhoneInformation.class);
i.putExtra("SKU_CODE", phoneAndTableItem.getSku_code());
context.startActivity(i);
}
});
// row.setTag(holder);
holder.frontMobileName.setText(phoneAndTableItem.getModel_name());
holder.frontPrice.setText(phoneAndTableItem.getCc_emi());
holder.backMobileName.setText(phoneAndTableItem.getModel_name());
holder.descOne.setText(phoneAndTableItem.getKey_feat_1());
holder.descTwo.setText(phoneAndTableItem.getKey_feat_2());
holder.descThree.setText(phoneAndTableItem.getKey_feat_3());
holder.descFour.setText(phoneAndTableItem.getKey_feat_4());
holder.ccEmi.setText(phoneAndTableItem.getCc_emi());
holder.financeEmi.setText(phoneAndTableItem.getFinance_emi());
holder.frontMobileName.setTypeface(tvHalvetica);
holder.frontPrice.setTypeface(tvHalvetica);
holder.backMobileName.setTypeface(tvHalvetica);
holder.descOne.setTypeface(tvHalvetica);
holder.descTwo.setTypeface(tvHalvetica);
holder.descThree.setTypeface(tvHalvetica);
holder.descFour.setTypeface(tvHalvetica);
holder.ccEmi.setTypeface(tvHalvetica);
holder.financeEmi.setTypeface(tvHalvetica);
holder.tv_front_inr.setTypeface(tvHalvetica);
holder.tv_description.setTypeface(tvHalvetica);
holder.tv_price_ccemi_inr.setTypeface(tvHalvetica);
holder.tv_ccemi_price.setTypeface(tvHalvetica);
holder.tv_price_finance_inr.setTypeface(tvHalvetica);
holder.tv_financeemi_price.setTypeface(tvHalvetica);
// here we are rotating the LinearLayout...
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setTag(position);
// Start Array Check...
Toast.makeText(context, "onClick id: " + v.getTag(), Toast.LENGTH_SHORT).show();
if (selected_views.contains((Integer) v.getTag())) {
holder.frontlayout.clearAnimation();
holder.frontlayout.setAnimation(animation1);
holder.frontlayout.startAnimation(animation1);
HashSet hs = new HashSet();
hs.addAll(selected_views);
selected_views.clear();
selected_views.addAll(hs);
selected_views.remove((Integer) v.getTag());
notifyDataSetChanged();
isBackOfCardShowing = true;
} else {
if (selected_views.size() == 0) {
holder.frontlayout.clearAnimation();
holder.frontlayout.setAnimation(animation1);
holder.frontlayout.startAnimation(animation1);
selected_views.add((Integer) v.getTag());
notifyDataSetChanged();
array_size = selected_views.size();
isBackOfCardShowing = false;
}
else {
holder.frontlayout.clearAnimation();
holder.frontlayout.setAnimation(animation1);
holder.frontlayout.startAnimation(animation1);
selected_views.add((Integer) v.getTag());
notifyDataSetChanged();
isBackOfCardShowing = false;
}
}
// End Array Check...
animation1.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
if (isBackOfCardShowing) {
holder.frontlayout.setVisibility(View.VISIBLE);
holder.backlayout.setVisibility(View.GONE);
// holder.frontImage.setImageResource(R.drawable.accesories_dashboard);
} else {
holder.backlayout.setVisibility(View.VISIBLE);
holder.frontlayout.setVisibility(View.GONE);
// holder.frontImage.setImageResource(R.drawable.vas_dashboard);
}
holder.frontlayout.clearAnimation();
holder.frontlayout.setAnimation(animation2);
holder.frontlayout.startAnimation(animation2);
}
});
}
});
holder.parentLayout.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
String inserted="";
final String SKV_CODE = phoneAndTableItem.getSku_code().toString();
handler.open();
inserted = handler.insertData(phoneAndTableItem.getModel_name(),
phoneAndTableItem.getKey_feat_1(),
phoneAndTableItem.getKey_feat_2(),
phoneAndTableItem.getKey_feat_3(),
phoneAndTableItem.getKey_feat_4(),
phoneAndTableItem.getCc_emi(),
phoneAndTableItem.getFinance_emi(),
SKV_CODE);
handler.close();
if (inserted.equals("inserted")) {
Toast.makeText(context, " Added to Shortlist " , Toast.LENGTH_SHORT).show();
} else if(inserted.equals("error")){
Toast.makeText(context, " Already present in Shortlist " , Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(context, " Try again " , Toast.LENGTH_SHORT).show();
}
return true;
}
});
return row;
}
static class RecordHolder {
ImageView frontImage;
TextView frontMobileName;
TextView frontPrice;
TextView backMobileName;
TextView descOne;
TextView descTwo;
TextView descThree;
TextView descFour;
TextView ccEmi;
TextView financeEmi;
LinearLayout frontlayout, backlayout, parentLayout;
Button viewdetails;
TextView tv_front_inr, tv_description, tv_price_ccemi_inr,
tv_ccemi_price, tv_price_finance_inr, tv_financeemi_price;
}
This code is showing properly position, but below when I am scrolling, then still I am getting flipped other items also. Please help me.
To get the exact position of item clicked in a GridView, you need to use setOnItemClickListener on your GridView reference, example:
GridView g = (GridView) findViewById(R.id.gridView1);
g.setAdapter(new ImageAdopterShow(this)); // your custom adaptor object goes here
g.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Toast.makeText(getBaseContext(), "position of item is " +position, Toast.LENGTH_SHORT).show();
}
});
This is the way i get the position of any item in my GridView. Hope this answers your question.
Your Question is kind of misleading If you can get the item position then you need to change your question for animations not working in GridView properly.

Categories

Resources