简体   繁体   中英

use Textview.setText in other method Android

How i can use textview value in other method? I want to intent value of "txtTitle.setText" to other activity (cars) but i can't use that on onclick method .

    public View getView(int position, View view, ViewGroup parent) {
       LayoutInflater inflater = context.getLayoutInflater();
       View rowView = inflater.inflate(R.layout.single_list, null, true);

       TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
       txtTitle.setText(companyname[position]);

    txtTitle.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(context, Cars.class);
            intent.putExtra("carId", txtTitle.getText());
            context.startActivity(intent);
        }
    });

Please edit my code thanks

Your TextView txtTile is only know in your getView method, because you created it there - you need to declare this variable in your class, outside of any method.

Simply call:

TextView txtTitle;

and in getView you set it like this:

 txtTitle = (TextView) rowView.findViewById(R.id.txt);

您不能以这种方式使用它, txtTitle的范围缩小为getView方法( 请参阅 txtTitle ),而应该将TextView定义为全局字段,以便可以在任何需要的地方使用它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM