custom Expanded gridview not showing items on performing adding removing operation on list of gridview

  • Replies:1
Pravin Bhosale
  • Forum posts: 1

Feb 23, 2016, 7:24:18 AM via Website

I am developing an app in which i have my custom Expanded gridview and custom expanded adapter

below is expanded listview

public class ExpandedGridView extends GridView {
private android.view.ViewGroup.LayoutParams params;
private int old_count = 0;

public ExpandedGridView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void onDraw(Canvas canvas) {
    if (getCount() != old_count) {
        old_count = getCount();
        params = getLayoutParams();
        params.height = getCount() * (old_count > 0 ? getChildAt(0).getHeight()+01 : 0);
        setLayoutParams(params);
    }

    super.onDraw(canvas);
}

}

and below is the expanded adapter code for gridview

 public class MoneySearchResultsExpandedAdapter extends ArrayAdapter<MobileAPIMoneyChoice> {

private LruCache<String, Bitmap> mMemoryCache;
private Activity activity;
//private ExpandableListView elv;
private LayoutInflater inflater;
private ArrayList<MobileAPIMoneyChoice> mobAPIMoneyChoices;
private Activity _InvokerContext;
public static ArrayList<MobileAPIMoneyChoice> mobAPIMoneyChoicesStatic;
private URL url=null;
public void add(MobileAPIMoneyChoice mobAPIBooking) {
    this.mobAPIMoneyChoices.add(mobAPIBooking);
}

public MoneySearchResultsExpandedAdapter(Activity invokerContext, int id , ArrayList<MobileAPIMoneyChoice> mobAPIBookings) {
    super(invokerContext,id,mobAPIBookings);
    this._InvokerContext = invokerContext;
    this.mobAPIMoneyChoices = mobAPIBookings;



    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);


    final int cacheSize = maxMemory / 8;

    mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
        @Override
        protected int sizeOf(String key, Bitmap bitmap) {

            return bitmap.getByteCount() / 1024;
        }
    };

}

public void setInflater(LayoutInflater inflater, Activity activity) {
    this.inflater = inflater;
    this.activity = activity;
}



private void runGetImage(ImageView imgMoneyLogo, String imageURL) {
    Drawable MoneyImage = Utility.LoadImageFromWebOperations(imageURL);
    Log.d("pro","image url+"+imageURL);
    _InvokerContext.runOnUiThread(new SetMoneyImageRunnable(imgMoneyLogo, MoneyImage));

}

private class SetMoneyImageRunnable implements Runnable {
    ImageView _ImgMoneyLogo;
    Drawable _MoneyImage;

    public SetMoneyImageRunnable(ImageView imgMoneyLogo, Drawable MoneyImage) {
        _ImgMoneyLogo = imgMoneyLogo;
        _MoneyImage = MoneyImage;
    }

    @Override
    public void run() {
        _ImgMoneyLogo.setImageDrawable(_MoneyImage);
    }

}

public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
    if (getBitmapFromMemCache(key) == null) {
        mMemoryCache.put(key, bitmap);
    }
}

public Bitmap getBitmapFromMemCache(String key) {
    return mMemoryCache.get(key);
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder=null;
    if (convertView == null) {


        convertView = inflater.inflate(R.layout.Money_search_result_choice_item, null);

            viewHolder = new ViewHolder();
            viewHolder.imageview=(ImageView) convertView.findViewById(R.id.imgMoneyLogo);
            viewHolder.progressBar=(ProgressBar)convertView.findViewById(R.id.progressresultchoice);
            convertView.setTag(viewHolder);


    }
    else
    {
        viewHolder=(ViewHolder)convertView.getTag();
    }




    final MobileAPIMoneyChoice mobAPIMoneyChoice = mobAPIMoneyChoices.get(position);


    if ((mobAPIMoneyChoice.mobileAPILimitedMoneyDetailInfo != null) && (mobAPIMoneyChoice.mobileAPILimitedMoneyDetailInfo.apiImage != null) ) {
        Log.d("pro", "url of imnage" + mobAPIMoneyChoice.mobileAPILimitedMoneyDetailInfo.apiImage.imageURL);
        viewHolder.imageUrl=mobAPIMoneyChoice.mobileAPILimitedMoneyDetailInfo.apiImage.imageURL;
        loadBitmap(viewHolder);

    }




    if ((mobAPIMoneyChoice.mobileAPILimitedMoneyDetailInfo != null) && (mobAPIMoneyChoice.mobileAPILimitedMoneyDetailInfo.apiImage != null)) {
        Runnable getImage = new Runnable() {
            @Override
            public void run() {
                ///runGetImage(imgMoneyLogo, mobAPIMoneyChoice.mobileAPILimitedMoneyDetailInfo.apiImage.imageURL);

            }
        };
        Thread thread = new Thread(null, getImage);
        thread.start();
    } else {

    }
    TextView lblMoneyName = (TextView) convertView.findViewById(R.id.lblMoneyName);
    lblMoneyName.setText(String.valueOf(mobAPIMoneyChoice.itemName));

    //added by pravin B

    ArrayList<MobileAPIMoneyBoardChoice> boardchoice=new ArrayList<MobileAPIMoneyBoardChoice>(mobAPIMoneyChoice.mobileAPIMoneyBoardChoices);

    MobileAPIMoneyBoardChoice mobMoneyBoardChoice=boardchoice.get(0);
    TextView price=(TextView)convertView.findViewById(R.id.lblMoneyprice);
    price.setText(String.valueOf( mobMoneyBoardChoice.approxPriceIUC));
    TextView currencycode=(TextView)convertView.findViewById(R.id.lblMoneypricecurrency);
    currencycode.setText(Config.UserCurrencyCode);
    //pravins end

    ArrayList<ImageView> imgStarRatings = new ArrayList<ImageView>();
    imgStarRatings.add((ImageView) convertView.findViewById(R.id.imgstarRating1));
    imgStarRatings.add((ImageView) convertView.findViewById(R.id.imgstarRating2));
    imgStarRatings.add((ImageView) convertView.findViewById(R.id.imgstarRating3));
    imgStarRatings.add((ImageView) convertView.findViewById(R.id.imgstarRating4));
    imgStarRatings.add((ImageView) convertView.findViewById(R.id.imgstarRating5));
    for (int i = 0; i < 5; i++) {
        imgStarRatings.get(i).setImageResource(R.drawable.star_off);
    }
    for (int i = 0; i < mobAPIMoneyChoice.starRating; i++) {
        imgStarRatings.get(i).setImageResource(R.drawable.star_on);
    }


    //ExpandableListView eLV = (ExpandableListView) parent;

    //eLV.expandGroup(position);

    convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            MoneySearchResultsActivity MoneySearchResultsActivity = (MoneySearchResultsActivity) activity;
            Intent showSelectedMoneyDetailsIntent = new Intent(MoneySearchResultsActivity, ShowSelectedMoneyDetailsActivity.class);
            showSelectedMoneyDetailsIntent.putExtra(StringConstants.QVarSelectedMoneyResultIndex, mobAPIMoneyChoice.mobileAPIMoneyChoiceIndex);
            mobAPIMoneyChoicesStatic = mobAPIMoneyChoices;
            MoneySearchResultsActivity.startActivityForResult(showSelectedMoneyDetailsIntent, IntConstants.RequestCodes.showMoneyDetails);
        }
    });

    //elv.setDividerHeight(0);
    return convertView;
}

public class ViewHolder {
    protected ImageView imageview;
    protected  String imageUrl;
    protected Drawable drawable;
    protected Bitmap bitmap;
    protected ProgressBar progressBar;
}
public void loadBitmap(ViewHolder viewHolder) {
    final String imageKey = String.valueOf(viewHolder.imageUrl);
    Log.d("pro","imagekey"+imageKey);
    final Bitmap bitmap = getBitmapFromMemCache(imageKey);
    if (bitmap != null)
    {
        viewHolder.progressBar.setVisibility(View.GONE);
        viewHolder.imageview.setImageBitmap(bitmap);
    }
    else
    {
        viewHolder.progressBar.setVisibility(View.VISIBLE);
        viewHolder.imageview.setVisibility(View.GONE);
        DownloadAsyncTask task = new DownloadAsyncTask();
        task.execute(viewHolder);
    }
}
private  class DownloadAsyncTask extends AsyncTask<ViewHolder,ViewHolder,ViewHolder> {

    @Override
    protected ViewHolder doInBackground(ViewHolder... params) {
        InputStream is;
        Drawable d=null;
        ViewHolder viewholder = params[0];
        String url=viewholder.imageUrl;
        Log.d("pro","image url"+url);
        if(url ==null)
        {
                //0vi hreturn  null;
        }
        try {

            is = (InputStream) new URL(url).getContent();
            viewholder.bitmap = BitmapFactory.decodeStream(is);
            viewholder.drawable = Drawable.createFromStream(is, "src name");
            Log.d("pro","adding key"+viewholder.imageUrl.toString());
            addBitmapToMemoryCache(viewholder.imageUrl.toString(), viewholder.bitmap);
        }catch (Exception e)
        {

        }
        return viewholder;
    }

    protected void onPostExecute(ViewHolder result) {
        Log.i("Async-Example", "onPostExecute Called");
        if (result.bitmap == null) {
            result.progressBar.setVisibility(View.GONE);
            result.imageview.setImageResource(R.drawable.noimage);
            result.imageview.setVisibility(View.VISIBLE);
        } else {

            result.progressBar.setVisibility(View.GONE);
            result.imageview.setImageBitmap(result.bitmap);
            result.imageview.setVisibility(View.VISIBLE);
        }
    }


}

}

problem is when i apply filter on those gridview results filtering is working properly but if in case of filter setting where the gridview resultsare not qualifying to be shown in gridview means empty list of grid view and again if i am changing filter setting to show some results my object of list is filled with results but gridview is not showing them even if i set notifyondatasetchanged()

below is code of filter and refreshing of gridview

int t = m_MoneySearchResultChoices.size();

            MobileAPIMoneyChoice mobileAPIMoneyChoice;

            for(Iterator<MobileAPIMoneyChoice> i =m_MoneySearchResultChoices.iterator();i.hasNext();)
            {
                mobileAPIMoneyChoice =i.next();

                if ((mobileAPIMoneyChoice.starRating) < from || (mobileAPIMoneyChoice.starRating) >to ) {
                    Log.d("pro_Money2", "deleted" + mobileAPIMoneyChoice.starRating +"Money name"+mobileAPIMoneyChoice.itemName);
                    i.remove();
                }

            }

            runOnUiThread(renderChangedAdapter);


            private Runnable renderChangedAdapter=new Runnable() {
@Override
public void run() {
    m_adapter = new MoneySearchResultsExpandedAdapter(MoneySearchResultsActivity.this,R.layout.Money_search_result_choice_item, m_MoneySearchResultChoices);
    m_adapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),MoneySearchResultsActivity.this);
    lstResults.setAdapter(m_adapter);
    _RefreshResultAdapter();

}

};

private void _RefreshResultAdapter()
{
Log.d("pro", "steppp");
m_adapter.clear();
if (m_MoneySearchResultChoices != null && m_MoneySearchResultChoices.size() > 0) {

    Log.d("pro", "in refreshresultadapter");
    m_adapter.notifyDataSetChanged();
    for (int i = 0; i < m_MoneySearchResultChoices.size(); i++)
    {
        m_adapter.add(m_MoneySearchResultChoices.get(i));

    }



}
try {
    m_ProgressDialog.dismiss();
    m_ProgressDialog = null;
    m_adapter.notifyDataSetChanged();
} catch (Exception e) {
}

}

Reply
Philipp Eichhorn
  • Forum posts: 12

Mar 10, 2016, 4:06:44 PM via Website

Quick word of advice: that is way to much code for anyone hear to read through and find the problem. Try boiling down the code to its (bare) minimum, that might also help you in the search of your bug.

Good luck!

Reply