Search code examples
androidandroid-layoutlistviewandroid-listviewlistadapter

Listview/ListItem Button not Showing up?


I am trying to created a List where each list item has two text views and button, however, I am having trouble seeing the text view text when the button is there. What I am doing is creating a custom list adapter so that I can use the click event of the button. Any ideas why the text in my text views does not show up? The button is there for each list item and if clicked the toast message shows up.

Calling the customer adapter in my activity:

ListAdapter adapter = new CustomSimpleAdapter(getActivity(), tripList,
                        R.layout.list_row, new String[] { TAG_TRIPID, TAG_TRIPNAME,     TAG_TRIPSUMMARY, TAG_DISTANCE },
                        new int[] { R.id.pid, R.id.title, R.id.trip_sum, R.id.trip_dist });

 // updating listview
 ((ListView) lv.findViewById(R.id.list)).setAdapter(adapter);

My custom adapter:

private class CustomSimpleAdapter extends SimpleAdapter implements OnClickListener  {
    private final Activity context;
    private Button btnAddExpense = null;

    public CustomSimpleAdapter(Activity context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
        super(context, tripList, resource, from, to);
        this.context = getActivity();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.list_row, parent, false);
        btnAddExpense = (Button) rowView.findViewById(R.id.btn_addExpense);
        //btnAddExpense.setTag(position);

        btnAddExpense.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(context, "List Item Clicked.",
                           Toast.LENGTH_LONG).show();
            }
        });
        return rowView;          
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
}

The list_row xml:

<TextView
    android:id="@+id/pid"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:visibility="gone" />

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:typeface="sans"
    android:textSize="15sp"
    android:textColor="@drawable/list_item_text"
    android:textStyle="bold"/>

<TextView
    android:id="@+id/trip_sum"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/title"
    android:textSize="12sp"
    android:textColor="@drawable/list_item_text"
    android:layout_marginTop="1dip" />

<TextView
    android:id="@+id/trip_dist"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@id/title"
    android:layout_toLeftOf="@+id/btn_addExpense"
    android:textSize="24sp"
    android:textColor="@drawable/list_item_text"
    android:textStyle="bold"/>

<Button
    android:id="@+id/btn_addExpense"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/trip_sum"
    android:layout_alignRight="@+id/trip_sum"
    android:text="+Expense" />
</RelativeLayout>

Thanks for the help!

EDIT: Before I created the custom adapter I was using the SimpleAdapter and the data showed up fine.

ListAdapter adapter = new SimpleAdapter(getActivity(), tripList,
                        R.layout.list_row, new String[] { TAG_TRIPID, TAG_TRIPNAME, TAG_TRIPSUMMARY, TAG_DISTANCE },
                        new int[] { R.id.pid, R.id.title, R.id.trip_sum, R.id.trip_dist });

Solution

  • You didn't call setText() on the TextViews that is why. Also, I don't know how you came up with that layout, but it is really ugly. If you do not use the first TextView, why have it in there?

    Updated: in the getView() method, retrieve each instance of TextView like you did with the button there using the id you supplied for each TextView in your layout, once you have that, just call set text and give it the array containing the text data you want to display for each row