How to get textView text in onclicklistener inside listView adapter

I have a View list that contains an imageView, a TextView and two buttons

what i want when i click a button, for example for the third item in the list view. I want to get the text that is in text view when I click a button that is in the same position as the text view. How do I do this in my list view adapter inside getView () in 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;
     }
   } 

      

+3


source to share


4 answers


In the button onClick

Just do

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

      

i.e. 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 do textview

before final

, i.e. change

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

      

to

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

      

+5


source


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);*/

    }
       });

      



+2


source


Using

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

      

in the click

eventbutton

+2


source


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

      

0


source







All Articles