0

私はJavaを初めて使用し、問題に遭遇しました。TextView を設定しsetText、ダイアログからボタンがクリックされたときにメソッドを呼び出したいと考えています。これTextViewは私の主な活動で設定されています。エラーが発生するため、静的に初期化していません。問題のコード行は、クラスのタブで定義されTabs.total.setText("PRINTING TO TEXT VIEW");たコンテンツを設定することです。TextView

変数totalは次のように定義されます-

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

この問題に関するいくつかの資料をよく探しましたが、これまでのところ解決策を見つけることができませんでした。誰かが助けてくれることを願っています。ありがとう!!

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;
}

}

4

2 に答える 2

0

あなたがやりたいことは、独自の onClickListener() を作成することだと思います。

このソリューションを確認してください: OnClickListener にパラメーターを渡すには?

TextView をコンストラクターに渡すだけです。

于 2013-02-18T20:31:34.023 に答える
0

2 つの問題があります。

  1. ビューを格納するために静的変数を使用することは、まったく推奨されません (メモリ リーク)。関数へのパラメーターとして、またはフィールドとして参照を配置するだけです。

  2. 「else」の部分では、変数「v」を更新して新しいデータを表示するのではなく、データを入れたビューを作成したときだけです。

于 2013-02-18T21:00:57.083 に答える