简体   繁体   中英

how to get textView text in onclicklistener inside listView adapter

i have a list View which contains imageView, TextView and two Buttons

what i want is when i click on the button for example for the third item in the list view i want to get the text that in the textview when i click on the button which is in the same position as the text view.. how to do that in my list view adapter inside getView() in the onclicklistener ?

here is my SimpleArrayAdapter.java

   public class MySimpleArrayAdapter extends ArrayAdapter<String> {
     private final Context context;
     private final String[] values;

     public MySimpleArrayAdapter(Context context, String[] values) {
       super(context, R.layout.my_listview, values);
       this.context = context;
       this.values = values;
     }

     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
       LayoutInflater inflater = (LayoutInflater) context
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       View rowView = inflater.inflate(R.layout.my_listview, parent, false);
       Button d=(Button) rowView.findViewById(R.id.d);
       Button a=(Button) rowView.findViewById(R.id.a);
       TextView textView = (TextView) rowView.findViewById(R.id.label);
       ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
       textView.setText(values[position]);

       // Change the icon for Windows and iPhone
       String s = values[position];
       if (s.startsWith("Windows7") || s.startsWith("iPhone")
           || s.startsWith("Solaris")) {
         imageView.setImageResource(R.drawable.no);
       } else {
         imageView.setImageResource(R.drawable.ok);
       }

       d.setOnClickListener(new View.OnClickListener() {

           @Override
           public void onClick(View view) {
        // here where i want to get the current textview text


        Log.d(null, "");
    /*Intent intent = new Intent(context, MainActivity2.class);
    context.startActivity(intent);*/

    }
       });

       a.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {
    Log.d(null, "aaaaaaaaaaaaaaaaa");

    }
    });


       return rowView;
     }
   } 

In button onClick Simply do

String txt = textView.getText().toString();

ie rewrite your code as

 d.setOnClickListener(new View.OnClickListener() {

           @Override
           public void onClick(View view) {
        // here where i want to get the current textview text
    String txt = textView.getText().toString();  // add here


        Log.d(null, "");
    /*Intent intent = new Intent(context, MainActivity2.class);
    context.startActivity(intent);*/

    }
       });

and make textview to final ie Change

 TextView textView = (TextView) rowView.findViewById(R.id.label);

to

final TextView textView = (TextView) rowView.findViewById(R.id.label);
final TextView textView = (TextView) rowView.findViewById(R.id.label);

d.setOnClickListener(new View.OnClickListener() {

           @Override
           public void onClick(View view) {

           String myText = textView.getText().toString();

        Log.d(null, "");
    /*Intent intent = new Intent(context, MainActivity2.class);
    context.startActivity(intent);*/

    }
       });

Use

String txtStr = textView.getText().toString();

in click event of button

try this:

final TextView textView = (TextView) rowView.findViewById(R.id.label);

d.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {    
        textView.getText().toString();      
    }
});

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