I hava an Activity, a ListView, and an ArrayAdapter.
In the ArrayAdapter, there are a TextView(I name it "title") and serval buttons created dynamically with different texts.
I want to do is when I click the button, I want to show the text on the button also the text on the "title".
The challenge is that when I do button.setOnClickListener in the Adapter, I dont know how to get the title.
On the other hand, If I do list.setOnItemClickListener, it doesn't work when I click the button. Also, because the button is dynamic, I dont know how to show it.
Thanks!
here is my getView:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Map<String, String> item = listitem.get(position);
View view = inflater.inflate(resId, parent, false);
// get views
final TextView title = (TextView) view.findViewById(R.id.title);
TextView type = (TextView) view.findViewById(R.id.type);
final TextView updateDate = (TextView) view.findViewById(R.id.updateDate);
TableLayout stLayout = (TableLayout) view
.findViewById(R.id.showtimes);
// fo
title.setFocusable(false);
updateDate.setFocusable(false);
// set views
String[] array = DatabaseHelper.MOVIE_SHOWTIMES;
for (int i = 0; i < array.length; i++) {
String datum = item.get(array[i]);
if (datum != null) {
switch (i) {
case 0:
title.setText(datum);
break;
case 1:
String types = MovieHelper.convertMovieTypes(datum);
type.setText(types);
break;
case 2:
try {
Date date = new Date(datum);
String formattedDate = DateFormatHelper.format(
date, DateFormatHelper.DATE);
updateDate.setText(formattedDate);
} catch (ParseException e) {
Log.e(TAG, e.toString());
}
break;
case 3:
try {
JSONArray times = new JSONArray(datum);
TableRow tableRow = new TableRow(context);
tableRow.setGravity(Gravity.CENTER_HORIZONTAL);
int record = 0;
for (int x = 0; x < times.length(); x++) {
String time = times.getString(x);
Date convertedTime = new Date(time);
try {
String formattedTime = DateFormatHelper
.format(convertedTime,
DateFormatHelper.TIME);
Button timeButton = new Button(context);
timeButton.setId(buttonCount);
timeButton.setText(formattedTime);
tableRow.addView(timeButton);
buttonCount++;
record++;
timeButton.setFocusable(false);
timeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.getParent();
Toast.makeText(getApplicationContext(),
((Button)v).getText(),
Toast.LENGTH_SHORT).show();
}
});
} catch (ParseException e) {
Log.e(TAG, e.toString());
}
if (record % 3 == 0 && record != 0) {
stLayout.addView(tableRow);
tableRow = new TableRow(context);
tableRow.setGravity(Gravity.CENTER_HORIZONTAL);
}
}
if (record % 3 != 0 && record != 0) {
stLayout.addView(tableRow);
}
} catch (JSONException e) {
Log.e(TAG, e.toString());
}
break;
}
}
}
return view;
}
}