How to get textView text in onclicklistener inside the listView adapter… here is a solution to the problem.
How to get textView text in onclicklistener inside the listView adapter
I have a ListView with an imageView, a TextView, and two buttons
What I want is that when I click the button of the third item in the ListView, I want to get the text in the TextView
when I click the button in the same position as the TextView. How do I do this in the ListView adapter inside getView() in onclicklistener?
This 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;
}
}
Solution
Simply do it in the button onClick
String txt = textView.getText().toString();
That is, rewrite your code to
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 the textview
final
, i.e. change
TextView textView = (TextView) rowView.findViewById(R.id.label);
to
final TextView textView = (TextView) rowView.findViewById(R.id.label);