Search code examples
javaandroidlistviewandroid-listviewbaseadapter

How to access Activity object in Adapter class


How can i access object of activity in base adapter class. I used adapter class as adapter for my list-view. I want to access textview which is outside the listview but listview and textview are in same activity. I tried in adapter class like this:

            holder.grandTotal = (TextView) ShopCartActivity.findViewById(R.id.txtGrandTotal);
        holder.grandTotal.setText(String.valueOf(new DecimalFormat("##.##").format(grandTotal)));

But error is coming on this syntax:

ShopCartActivity. //ERROR

I tried like this too:

ShopCartActivity.this or ShopCartActivity.class

I tried this in constructor of adapter class it works (But value is not calculated yet) but when i put it in getView() method in which my all calculations are happening, its not working.

Basically i want to set value of textview after loop returns the value in base adapter. Is there a way i can access the object with findviewbyid method?


Solution

  • Pass context when you create your adapter, Use that context to get the inflated view.

    Adapter adapter = new Adapter(this);
    

    Then in Adpater Class constructor :

    public Adapter(Context context){
    context.findViewById(R.id.textview);
    }