Reputation: 643
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;
}
}
Upvotes: 3
Views: 3755
Reputation: 70
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();
}
});
Upvotes: 0
Reputation:
Use
String txtStr = textView.getText().toString();
in click
event of button
Upvotes: 2
Reputation: 18977
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);*/
}
});
Upvotes: 2
Reputation: 14398
In button onClick
Simply 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 make textview
to final
i.e. Change
TextView textView = (TextView) rowView.findViewById(R.id.label);
to
final TextView textView = (TextView) rowView.findViewById(R.id.label);
Upvotes: 5