0

I am very new to Java and have encountered a problem. I have set up a TextView and want to call the setText method when a button is clicked from my dialog. This TextView is set up in my main activity. I have not initialized it as static as this produces an error. The line of code in question is - Tabs.total.setText("PRINTING TO TEXT VIEW"); I want this to set the content of my TextView defined in my class Tabs.

Variable total is defined like this -

TextView total = (TextView) findViewById(R.id.total_view);

I have looked a lot for some material on this problem but have failed to find a solution so far. I hope somebody can help. Thanks!!

public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    final View v;
    if(convertView==null){
        LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = li.inflate(R.layout.scrollergrid, null);
        final TextView tv = (TextView)v.findViewById(R.id.icon_text);
        tv.setTag(pos);
        tv.setText(mItems[pos]);
        TextView price = (TextView)v.findViewById(R.id.price_text);
        price.setText("$" + Prices[pos]);

        pos += 1;
        final ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
        iv.getLayoutParams().height = 150;
        iv.getLayoutParams().width = 150;
        iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
        iv.setImageResource(mThumbIds[position]);
        iv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                id = tv.getTag();
                System.out.println(mItems[(Integer) id] + " $" + Prices[(Integer) id]);

                final Dialog dialog = new Dialog(mContext);
                dialog.setContentView(R.layout.alertdialog);
                dialog.setTitle(mItems[(Integer) id]);

                ImageView dialogImage = (ImageView) dialog.findViewById(R.id.alert_image);
                dialogImage.setImageResource(mThumbIds[(Integer) id]);

                TextView itemDescription = (TextView) dialog.findViewById(R.id.item_description);
                itemDescription.setText("A nice little piece of food. Good for all the family. Full of flavour!!");

                TextView itemPrice = (TextView) dialog.findViewById(R.id.item_price);
                itemPrice.setText("$" + Prices[(Integer) id]);

                Button goBack = (Button) dialog.findViewById(R.id.go_back);
                goBack.setOnClickListener(new OnClickListener() {
                @Override
                    public void onClick(View v) {
                        dialog.cancel();
                    }
                });

                Button order = (Button) dialog.findViewById(R.id.order);
                order.setOnClickListener(new OnClickListener() {
                @Override
                    public void onClick(View v) {
                        Tabs.Order.add(mItems[(Integer) id] + " " + Prices[(Integer) id]);
                        System.out.println(Tabs.Order);
                        Tabs.total.setText("HIIII");
                        dialog.cancel();
                    }
                });

                dialog.show();
            } 

        });

    }
    else
    {
        v = convertView;
    }
    return v;
}

}

halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

0

I think what you want to do is make your own onClickListener().

Check out this solution: How to pass parameters to OnClickListener?

And just pass in your TextView to the constructor.

Community
  • 1
  • 1
gomezfx
  • 147
  • 2
  • 8
0

You have two problems:

  1. Using static variables for storing views is not recommended at all (memory leaks). You should simply put the reference either as a parameter to the function or as a field.

  2. On the "else" part, you didn't update the variable "v" to have the new data being shown, only upon creation of the view you put data into it.

halfer
  • 19,824
  • 17
  • 99
  • 186
android developer
  • 114,585
  • 152
  • 739
  • 1,270