Android - Different items in GridView not showing up - android

I am working on app which has a GridView of images but the last item in GridView is a button that loads more item into Grid. But i am facing a strange issue and that is Button shows up sometime and sometime it doesn't specially when i scroll up and down really fast on GridView. I think it's because of recycle issue but i don't know how to fix it.
Here is my code:-
public class ButtonGridAdapter extends BaseAdapter implements OnCheckedChangeListener, OnClickListener {
private ArrayList<String> thumbUrls;
private ArrayList<String> mainUrls;
private LayoutInflater layoutInflater;
private Context context;
private Intent intent;
private ArrayList<String> positions = new ArrayList<String>();
private static ArrayList<String> urlsToUpload = new ArrayList<String>();
private int width;
public ButtonGridAdapter(Context context, ArrayList<String> thumbUrls, ArrayList<String> mainUrls, String clicked)
{
this.context = context;
this.thumbUrls = thumbUrls;
this.mainUrls = mainUrls;
intent = new Intent(context, GalleryView.class);
this.layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
DisplayMetrics displaymetrics = new DisplayMetrics();
((Activity) context).getWindow().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
this.width = displaymetrics.widthPixels;
System.out.println(width+" width");
}
#Override
public int getCount() {
return thumbUrls.size()+1;
}
#Override
public Object getItem(int arg0) {
return thumbUrls.get(arg0);
}
#Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder
{
ImageView mainImage;
CheckBox checkBox;
Button button;
}
public static class ButtonHolder
{
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder;
if(row==null || row.getTag().equals("button"))
{
row = layoutInflater.inflate(R.layout.griditem, parent, false);
holder = new ViewHolder();
holder.mainImage = (ImageView)row.findViewById(R.id.itemgrid);
holder.checkBox = (CheckBox)row.findViewById(R.id.itemselected);
holder.button = null;
row.setTag(holder);
}
else
{
holder = (ViewHolder)row.getTag();
}
if(position==thumbUrls.size())
{
int width = row.getWidth();
int height = row.getHeight()-70;
row = layoutInflater.inflate(R.layout.mybutton, parent, false);
holder.button = (Button)row.findViewById(R.id.gridbutton);
holder.button.setText("Load More");
LayoutParams params = holder.button.getLayoutParams();
params.height = height;
params.width = width;
holder.button.requestLayout();
holder.button.setOnClickListener(this);
System.out.println("Called");
row.setTag("button");
return row;
}
else
{
setImageSize(holder.mainImage);
System.out.println(position);
holder.mainImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
intent.putExtra("position", position);
intent.putStringArrayListExtra("MainUrls", mainUrls);
intent.putStringArrayListExtra("Thumbnails", thumbUrls);
context.startActivity(intent);
}
});
holder.checkBox.setOnCheckedChangeListener(null);
holder.checkBox.setTag(position);
holder.checkBox.setChecked(positions.contains(thumbUrls.get(position)));
holder.checkBox.setOnCheckedChangeListener(this);
UrlImageViewHelper.setUrlDrawable(holder.mainImage, thumbUrls.get(position), R.drawable.extras_load);
return row;
}
}
private void setImageSize(ImageView imageView) {
LayoutParams params = imageView.getLayoutParams();
int leftWidth = width - 80;
params.width = leftWidth/3;
params.height = leftWidth/3;
imageView.requestLayout();
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
positions.add(thumbUrls.get((Integer)buttonView.getTag()));
urlsToUpload.add(mainUrls.get((Integer)buttonView.getTag()));
}
else
{
positions.remove(thumbUrls.get((Integer)buttonView.getTag()));
urlsToUpload.remove(mainUrls.get((Integer)buttonView.getTag()));
}
}
public static ArrayList<String> getUrlsToUpload()
{
return urlsToUpload;
}
#Override
public void onClick(View v) {
if(v.getId()==R.id.gridbutton)
{
InstagramImpl.getInstance().fetchMorePhotos();
}
}
}
Any help would be really really appreciate.
Thanks.

I post full getView method from my other answer. Simple you check the position first. If it's the last item (load more button) return layout immediately.
private final Media special = new Media("-1", "", "", "", "");
public MediaGridAdapter(Context context, int imageID, ArrayList<Media> array, int type, boolean isGetMore) {
list = array;
mediaType = type;
this.context = context;
if(list != null) {
list.add(special);
}
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
RelativeLayout item;
ViewHolder holder;
//this part check it's the last item. -1 is my special item id in contructor.
if(list.get(position).getId().equals("-1")) {
item = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.item_special_more, null);
item.setTag(MORE_BUTTON);
return item;
}
//
if(convertView == null || convertView.getTag().toString().equals(MORE_BUTTON)) {
Context context = parent.getContext();
item = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.item_with_icon_vertical, null);
holder = new ViewHolder();
holder.thumbnail = (ImageView) item.findViewById(R.id.iv_icon_media);
item.setTag(holder);
} else {
item = (RelativeLayout) convertView;
holder = (ViewHolder) item.getTag();
}
}
// set data.
holder.thumbnail.setBitmap(...)
return item;

Maybe you can try delete row==null and holder = (ViewHolder)row.getTag(); it's because of recycle issue,you can have a try first.But it will affect efficiency. Another thing i want to say,how do position==thumbUrls.size();? Maybe it's position==thumbUrls.size()-1;.Ok,that's my view,i help it work.

Related

android:how to use viewflipper in listview to animate one row at a time

I have a listview with a custom adapter.Each row of the listview contains two textviews and a viewflipper.
When I click a button, I want to animate/flip each row with a random text.
For animation, I used view flipper,but the animation is not working one after the other , rather it works simultaneously for all the rows.
My button click code is as follows.
public void onButtonClick(View view) {
customAdapter.setFlipRow(!(customAdapter.isFlipRow()));
listView.invalidateViews();
}
The custom adapter:
public class CustomAdapter extends BaseAdapter {
Context context;
String countryList[];
boolean flag[]= null;
int flags[];
LayoutInflater inflter;
Random r = null;
private static String TAG = "CustomAdapter";
public ViewHolder getViewHolder() {
return viewHolder;
}
ViewHolder viewHolder;
public boolean isFlipRow() {
return flipRow;
}
public void setFlipRow(boolean flipRow) {
this.flipRow = flipRow;
}
private boolean flipRow = false;
public CustomAdapter(Context applicationContext, String[] countryList) {
this.context = context;
this.countryList = countryList;
flag = new boolean[countryList.length];
for(int f = 0;f<flag.length;f++)
{
flag[f] = false;
}
inflter = (LayoutInflater.from(applicationContext));
r = new Random(5);
}
private static class ViewHolder {
TextView country;
TextView country1;
ViewFlipper viewFlipper;
ListView lv;
}
#Override
public View getView(int pos, View view, ViewGroup viewGroup) {
View v = view;
if(view == null)
{
viewHolder = new ViewHolder();
view = inflter.inflate(R.layout.listadapter, null);
viewHolder.country = (TextView) view.findViewById(R.id.textView);
viewHolder.country1 = (TextView) view.findViewById(R.id.bottomtextView);
viewHolder.viewFlipper = (ViewFlipper) view.findViewById(R.id.flipper);
viewHolder.lv = (ListView)view.findViewById(R.id.listView);
view.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.country.setText(countryList[pos]);
int rInt = r.nextInt(5);
if(isFlipRow()){
viewHolder.country1.setText("Random No: "+rInt);
viewHolder.viewFlipper.setFlipInterval(1000);
if(pos <= countryList.length-1)
{
flag[pos] = true;
if(pos == 0 && flag[pos]){
viewHolder.viewFlipper.startFlipping();
}
if(pos != 0 && flag[pos-1] == true){
viewHolder.viewFlipper.startFlipping();
flag[pos-1] = false;
/*viewHolder.viewFlipper.stopFlipping();
I tried to have a boolean flag for each row,
setting it before startFlipping() and resetting it after
stopFlipping().But adding this stopFlipping() here
stops all rows except first row and all rows flip at the
same time.Moreover getView is called only during setAdapter()
and invalidateviews().*/
}
if(pos == countryList.length-1){
Arrays.fill(flag,false);
}
}
}
else{
viewHolder.viewFlipper.stopFlipping();
Arrays.fill(flag,false);
}
return view;
}
}
All I need is to animate each row one at a time.But now all the rows are animating at the same time.Any insights or inputs will be really helpful.
Try this:
public void onButtonClick(View view) {
customAdapter.flipRow(view);
}
and adapter:
public class CustomAdapter extends BaseAdapter {
Context context;
String countryList[];
int rnds[];
LayoutInflater inflater;
Random r;
private boolean flipRow = false;
private int flipRowNum = 0;
Button btn;
final private static String TAG = "CustomAdapter";
final private static int FLIP_DELAY = 1000;
final private static int FLIP_COUNT = 2;
public boolean isFlipRow() {
return flipRow;
}
public void flipRow(View view) {
btn = (Button)view;
btn.setEnabled(false);
flipRow = true;
notifyDataSetChanged();
}
public CustomAdapter(Context context, String[] countryList) {
this.context = context;
this.countryList = countryList;
inflater = (LayoutInflater.from(context));
r = new Random(5);
rnds = new int[countryList.length];
for(int f = 0;f<rnds.length; f++)
{
rnds[f] = r.nextInt(5);
}
}
private class ViewHolder {
TextView country;
TextView country1;
ViewFlipper viewFlipper;
ListView lv;
}
#Override
public int getCount() {
return countryList.length;
}
#Override
public String getItem(int i) {
return countryList[i];
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int pos, View view, ViewGroup viewGroup) {
final ViewHolder viewHolder;
if(view == null)
{
viewHolder = new ViewHolder();
view = inflater.inflate(R.layout.listadapter, null);
viewHolder.country = (TextView) view.findViewById(R.id.textView);
viewHolder.country1 = (TextView) view.findViewById(R.id.bottomtextView);
viewHolder.viewFlipper = (ViewFlipper) view.findViewById(R.id.flipper);
//viewHolder.lv = (ListView)view.findViewById(R.id.listView);
view.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.country.setText(countryList[pos]);
viewHolder.country1.setText("Random No: " + rnds[pos]);
viewHolder.viewFlipper.setFlipInterval(0);
if(isFlipRow() && (pos == flipRowNum)){
viewHolder.viewFlipper.startFlipping();
viewHolder.viewFlipper.setFlipInterval(FLIP_DELAY);
viewHolder.viewFlipper.postDelayed(new Runnable() { //Stop flipping
#Override
public void run() {
viewHolder.viewFlipper.stopFlipping();
flipRow = !flipRow;
flipRowNum++;
if(flipRowNum == countryList.length) flipRowNum = 0;
btn.setEnabled(true);
}
}, (2*FLIP_COUNT - 1)*FLIP_DELAY + 50);
}
return view;
}
}
Hope that helps!

Base Adapter onclick not working

I am trying to use the material dialogs library Material Dialog and i can't get the callback to work for the custom adapter.
The items are displayed perfectly but on click of the items i don't get any callback.
Base Adapter
public class EcardBaseAdapter extends BaseAdapter implements View.OnClickListener {
private LayoutInflater mInflater;
private List<? super Greeting> items;
private Context context;
private final int width;
private final int height;
public EcardBaseAdapter(List<Greeting> items, Context context) {
this.context = context;
mInflater = LayoutInflater.from(context);
this.items = items;
final DisplayMetrics metrics = context.getResources().getDisplayMetrics();
this.width = metrics.widthPixels;
this.height = metrics.heightPixels;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int i) {
return items.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
ViewHolder holder;
if (convertView == null) {
view = mInflater.inflate(R.layout.custom_item, parent, false);
holder = new ViewHolder();
holder.avatar = (ImageView) view.findViewById(R.id.image_path);
holder.name = (TextView) view.findViewById(R.id.custom_event_name);
view.setTag(holder);
} else {
view = convertView;
holder = (ViewHolder) view.getTag();
}
if (items.get(position) instanceof ECard) {
ECard eCard = (ECard) items.get(position);
Log.i("test", eCard.getImage() + " " + eCard.getSubject());
if (eCard.getImage() != null) {
Glide.with(context)
.load(eCard.getImage())
.fitCenter()
.override(width, height / 2)
.into(holder.avatar);
//holder.avatar.setImageURI(Uri.parse(eCard.getImage()));
}
holder.name.setTextColor(ContextCompat.getColor(context, R.color.darkGrey));
holder.name.setText(eCard.getSubject());
}
return view;
}
#Override
public void onClick(View view) {
Log.i("click", "itemclick");
}
private class ViewHolder {
public ImageView avatar;
public TextView name;
}
}
Code in the activity which uses the adapter
new MaterialDialog.Builder(birthday_details.this)
.title("Ecard")
.adapter(adapter,
new MaterialDialog.ListCallback() {
#Override
public void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
Toast.makeText(birthday_details.this, "Clicked item " + which, Toast.LENGTH_SHORT).show();
}
})
.show();
I did not understandd all your code, but I seems you never call
view.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("click", "itemclick");
}
};
or in your case
view.setOnClickListener(this);

ListView returns duplicate references to View onClick

My code expands the LinearLayout that is clicked. It works fine when there are 14 items or less in the list. Once there are more than 14 items it repeats the reference. What happens is clicking on the first item in the list expands both the 1st and the 15th item. It even seems randomized when there are 300 or more items. I've been trouble shooting this for a while with no luck
Here is my custom adapter
public class Scanvinadapter extends BaseAdapter {
private Activity activity;
private ArrayList data;
private static LayoutInflater inflater = null;
public Resources res;
Scanvinmodel model = null;
int i = 0;
ScanlistListener mCallback;
public Scanvinadapter(Activity a, ArrayList d, Resources resLocal){
activity = a;
data = d;
res = resLocal;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
try {
mCallback = (ScanlistListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement ScanlistListener");
}
}
#Override
public int getCount() {
if(data.size()<=0)
return 1;
return data.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public TextView scanlistvin;
public TextView scanlistbay;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
LinearLayout ll = null;
ViewHolder holder;
if(convertView == null){
vi = inflater.inflate(R.layout.scanlistview, null);
holder = new ViewHolder();
ll = (LinearLayout) vi.findViewById(R.id.scanlist);
ll.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT,100));
Log.v("position", String.valueOf(position));
i = i+1;
vi.setId(i);
ll.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mCallback.scanlistclick(v, 0);
}
});
//vi.setOnClickListener(new OnItemClickListener( position ));
holder.scanlistvin = (TextView) vi.findViewById(R.id.scanlistvin);
holder.scanlistbay = (TextView) vi.findViewById(R.id.scanlistbay);
vi.setTag(holder);
}else
holder = (ViewHolder)vi.getTag();
if(data.size()<=0){
holder.scanlistvin.setText("No Data");
}else{
model = null;
model = (Scanvinmodel) data.get(position);
holder.scanlistvin.setText(model.getVin());
holder.scanlistbay.setText(model.getBay());
return vi;
}
return null;
}
public void onClick(View v) {
Log.v("CustomAdapter", "=====Row button clicked=====");
}
private class OnItemClickListener implements OnClickListener{
private int mPosition;
OnItemClickListener(int position){
mPosition = position;
}
#Override
public void onClick(View v) {
Log.v("CustomAdapter", String.valueOf(mPosition) );
mCallback.scanlistclick(v, mPosition);
}
}
public interface ScanlistListener{
public void scanlistclick(View v, int i);
}
}
Here is the implement override begin called on each click. I display the id and it seems that it is duplicating ids. All the data is unique.
#Override
public void scanlistclick(View v, int i) {
new Animutils().expand(v);
Log.wtf("wtf", String.valueOf(v.getId()));
}
This is the animation that expands the view
public class Animutils {
private static final long ANIMATION_DURATION = 250;
protected static final String TAG = "Animutils.java";
public void expand(final View v) {
//v.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
final int targtetHeight = v.getMeasuredHeight();
//final int targtetHeight = 500;
//v.getLayoutParams().height = 114;
// v.setVisibility(View.VISIBLE);
Animation a = new Animation(){
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
//v.getLayoutParams().height = interpolatedTime == 1 ? LayoutParams.WRAP_CONTENT : (int)(targtetHeight * interpolatedTime);
//v.getLayoutParams().height = interpolatedTime == 1 ? 500 : (int)(targtetHeight + 25);
if(interpolatedTime == 0){
v.getLayoutParams().height = targtetHeight + 25;
}else{
if(v.getLayoutParams().height == 500){
cancel();
}else{
v.getLayoutParams().height = v.getLayoutParams().height +25;
}
}
v.requestLayout();
}
#Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
a.setDuration(ANIMATION_DURATION);
// a.setDuration((int)(targtetHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
}
public void collapse(final View v) {
final int initialHeight = v.getMeasuredHeight();
Animation a = new Animation(){
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if(interpolatedTime == 1){
v.setVisibility(View.GONE);
}else{
v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
v.requestLayout();
}
}
#Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
a.setDuration(ANIMATION_DURATION);
// a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
}
}
UPDATED ADAPTER
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
LinearLayout ll = null;
ViewHolder holder;
if(convertView == null){
vi = inflater.inflate(R.layout.scanlistview, null);
ll = (LinearLayout) vi.findViewById(R.id.scanlist);
ll.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT,100));
holder = new ViewHolder();
holder.scanlist = (LinearLayout) vi.findViewById(R.id.scanlist);
holder.scanlistvin = (TextView) vi.findViewById(R.id.scanlistvin);
holder.scanlistbay = (TextView) vi.findViewById(R.id.scanlistbay);
Log.v("position", String.valueOf(position));
vi.setTag(holder);
}else{
holder = (ViewHolder)vi.getTag();
ll = holder.scanlist;
Log.v("position", String.valueOf(position));
}
ll.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mCallback.scanlistclick(v, 0);
}
});
if(data.size()<=0){
holder.scanlistvin.setText("No Data");
}else{
model = null;
model = (Scanvinmodel) data.get(position);
holder.scanlistvin.setText(model.getVin());
holder.scanlistbay.setText(model.getBay());
}
return vi;
}
Add this to Adapter
#Override
public int getViewTypeCount() {
return getCount();
}
#Override
public int getItemViewType(int position) {
return position;
}
try this man : just pull out the click assignment from all of your if - else statement thats it, something
like the below code. So what is the problem and what is the solution of it?
you said that when i click on item 1 in list after populating the list with 14 items every thing works well but when the list populates with more items when i click on item 1, item 15th also responds. so why?
this is because of convertView. convertView is a view thats created and recycled through scrolling the list. this view makes GC be called less and also save memory for you. it first assigned by your earliest items of list after you scroll the list, for example item one of list disappears and you see item 15 the convertView of item one is passed again to you. in this time it is not null so you skip click assignment to it and it holdes the reference of last click assignment that is the assignment of item one. so when you click on item 1 it means you also click on item 15 because the click assignment for both of them is the same. so each row must be assigned by new event listener .
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
LinearLayout ll = null;
ViewHolder holder;
if(convertView == null){
vi = inflater.inflate(R.layout.scanlistview, null);
holder = new ViewHolder();
ll = (LinearLayout) vi.findViewById(R.id.scanlist);
ll.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT,100));
Log.v("position", String.valueOf(position));
i = i+1;
vi.setId(i);
holder.scanlist = (LinearLayout) vi.findViewById(R.id.scanlist);
holder.scanlistvin = (TextView) vi.findViewById(R.id.scanlistvin);
holder.scanlistbay = (TextView) vi.findViewById(R.id.scanlistbay);
vi.setTag(holder);
}else{
holder = (ViewHolder)vi.getTag();
ll= holder.scanlistview;
ll.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT,100));
}
ll.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mCallback.scanlistclick(v, 0);
}
});
if(data.size()<=0){
holder.scanlistvin.setText("No Data");
}else{
model = null;
model = (Scanvinmodel) data.get(position);
holder.scanlistvin.setText(model.getVin());
holder.scanlistbay.setText(model.getBay());
return vi;
}
return null;
}

Android - Remove GridView item from inside getView()?

I need a way to remove an Item from the GridView but this must be done from within the getView() method inside my custom Adapter. Here is my GridView:
Activity:
...
String[] newList;
newList[0] = "Item 1";
newList[1] = "Item 2";
newList[2] = "Item 3";
...
GridView GV = (GridView) getActivity().findViewById(R.id.gv);
GV.setAdapter(new Adapter(getActivity(), newList));
GV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView arg0,
View arg1, int position, long arg3) {
...
}
});
Adapter
public class Adapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflator;
private String mEntries[];
public static class ViewHolder {
public TextView myTextView;
}
public Adapter (Context context, String[] entries) {
mContext = context;
mInflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mEntries = entries;
}
#Override
public int getCount() {
return mEntries.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null) {
convertView = mInflator.inflate(R.layout.gvitemlayout, parent, false);
holder = new ViewHolder();
holder.myTextView = (TextView) convertView.findViewById(R.list.removeItem);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String string = mEntries[position];
String[] data = string.split("\\.");
if (data.length < 2) {
TextView itemName = (TextView) convertView.findViewById(R.list.itemName);
itemName.setText("");
TextView itemClass = (TextView) convertView.findViewById(R.favlist.itemClass);
itemClass.setText("");
holder.myTextView.setText("");
TextView itemNone = (TextView) convertView.findViewById(R.list.itemNone);
itemNone.setText("No Items");
} else {
TextView itemName = (TextView) convertView.findViewById(R.list.itemName);
itemName.setText(data[1]);
TextView itemClass = (TextView) convertView.findViewById(R.list.itemClass);
itemClass.setText(data[0]);
}
final int info = (Integer) getItem(position);
holder.myTextView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SharedPreferences sP = mContext.getSharedPreferences("fav", mContext.MODE_PRIVATE);
Boolean b = sP.getBoolean(mEntries[info], false);
if (b == true) {
SharedPreferences.Editor editor = sP.edit();
editor.remove(mEntries[info]);
editor.commit();
// REMOVE ITEM CODE NEEDED HERE
}
}
});
return convertView;
}
}
Hope this makes it easier to understand what I need.
This is what I did, there was no need to initialise the 'adapter' variable. Inside the getView() simply do this:
Remove the Item from the list that was used as the data source.
So. if you want to remove Item 2 from the list
String mEntries[] = ITEM1, ITEM2, ITEM3, etc
Do this:
String newList[] = new String[mEntries.length - 1];
int count = 0;
for (int i = 0; i < mEntries.length; i++) {
if (mEntries.length - 1 > 0) {
if (mEntries[i] == mEntries[1]) { // mEntries[1] as the range starts from 0, so 1 would be ITEM2
// SKIP IF MATCHES THE ITEM YO WANT TO REMOVE
} else {
newList[count] = mEntries[i];
count++;
}
}
}
mEntries = newList; // save the new list into mEntries
notifyDataSetChanged(); // notify the changes and the listview/gridview will update
What you are asking for is very possible. You didn't give enough code for it to be clear exactly what's happening, but the code above you posted won't work unless the remove button is an entire view returned by getView in the adapter.
Your getView in your adapter will need to look something like this:
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.grid_view_item, null);
}
Button removeButton = (Button) convertView.findViewById(R.id.remove_button);
removeButton.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
adapter.mThumbIdsList.remove(position);
adapter.notifyDataSetChanged();
}
});
return convertView;
}
Hope this helps. Good luck!

Android listview row changing values

I have a ListView that contains 3 types of elements. Rows and QueueRows that are a bit complicated sets of subcontrols, and HeaderRow containing only textView.
What I do is adding HeaderRow, few QueueRows, headerRow and few Rows.
QueueRow needs to be updated every second. That is why I wrote a code that calls notifyDataSetChanged on adapter. It updates queuerow every second, but there is also a problem - My two headers switch places for half a second.
After every 0,5s they swap. Do you have any ideas how to prevent them?
That is my code:
class BuildingsAdapter extends ArrayAdapter<BaseRow> {
private static Bitmap SOURCE_BITMAP;
private MainActivity mainActivity_;
private Handler handler_;
private Runnable runnable_;
public BuildingsAdapter(MainActivity mainActivity) {
super(mainActivity, R.layout.row);
this.mainActivity_ = mainActivity;
SOURCE_BITMAP = BitmapFactory.decodeResource(
mainActivity_.getResources(), R.drawable.images);
handler_ = new Handler();
runnable_ = new Runnable() {
#Override
public void run() {
notifyDataSetChanged();
if (hasQueueItems())
handler_.postDelayed(runnable_, 1000);
}
};
}
#Override
public void add(BaseRow object) {
if (!hasQueueItems() && object.getClass().equals(QueueRow.class))
handler_.postDelayed(runnable_, 1000);
super.add(object);
}
private boolean hasQueueItems() {
for (int i = 0; i < getCount(); ++i) {
if (getItem(i).getClass().equals(QueueRow.class))
return true;
}
return false;
}
#Override
public int getItemViewType(int position) {
return getItem(position).getType();
}
#Override
public int getViewTypeCount() {
return 3;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
View row = convertView;
switch (type) {
case BaseRow.QUEUE_ROW: {
QueueRowViewHolder holder = new QueueRowViewHolder();
//whatever
return convertView;
}
case BaseRow.ROW: {
RowViewHolder holder = new RowViewHolder();
//whatever
return convertView;
}
case BaseRow.HEADER_ROW: {
HeaderViewHolder holder = new HeaderViewHolder();
if (row == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.header_row, parent, false);
HeaderRow headerRow = (HeaderRow) getItem(position);
holder.nameTextView_ = (TextView) row
.findViewById(R.id.categoryNameTextView);
holder.nameTextView_.setText(headerRow.getName());
row.setTag(holder);
} else {
holder = (HeaderViewHolder) row.getTag();
}
return row;
}
}
return null;
}
static class QueueRowViewHolder {
ImageView qItemImageView_;
TextView qItemNameTextView_;
ProgressBar qProgressBar_;
TextView qLevelTextView_;
TextView qTimeTextView_;
int position_;
}
static class RowViewHolder {
ImageView itemImageView_;
TextView nameTextView_;
TextView levelTextView_;
TextView woodTextView_;
TextView ironTextView_;
TextView stoneTextView_;
TextView goldTextView_;
Button actionButton_;
TextView timeTextView_;
}
static class HeaderViewHolder {
TextView nameTextView_;
}
Ok! I've found it out - I didn't set the values of HeaderRow ;)

Categories

Resources