0

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;

        }

    }
cht
  • 367
  • 2
  • 6
  • 20

1 Answers1

0

First of all you should recycle list items (only inflate view if convertView is null).

Why should I recycle views:
Check this post. Here's a great blog post about that.
Basically you don't change the layout of a list item most of the time but change the content of it only. If you hava a list with 1000 items you would inflate 1000 xml layouts when scrolling down (1000 when scrolling up again). This is really expensive. If you recycle the view you would inflate about 10 views once (amount of visible views) and reuse them the whole time while scrolling. And since the recycling mechanisim is a built in feature it won't take much effort to use it...

If your talking about this button:

timeButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        v.getParent();
        Toast.makeText(getApplicationContext(),
            ((Button)v).getText(),
            Toast.LENGTH_SHORT).show();
    }
});

And this title:

final TextView title = (TextView) view.findViewById(R.id.title);

Why you don't call title.getText() inside the on click listener?

Community
  • 1
  • 1
Knickedi
  • 8,742
  • 3
  • 43
  • 45