foxy
foxy

Reputation: 154

Android custom adapter + listview ( not fully working )

I'm playing with my first android app with database. Got my custom adapter and its own rowview working. It should contain icon, name and description. Everything, except name is there. Tried to debug it, write it out in logcat and everyting seems fine. Just my app won't show it.

Could anybody take a look and help me a bit? Thanks a lot in advance.

My custom row view called exercise_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >

<ImageView
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="60dip"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="6dip"
    android:contentDescription="TODO"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/description"
    android:layout_width="fill_parent"
    android:layout_height="26dip"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@id/icon"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:textSize="12sp" />

<TextView
    android:id="@+id/name"
    android:layout_width="fill_parent"
    android:layout_height="34dip"
    android:layout_above="@id/description"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@id/icon"
    android:textSize="16sp" />

Now layout of activity we're working in (the most important part is list, I suppose) :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ExerciseManagementActivity" >


<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="24dp"
    android:text="@string/title_activity_exercise_management" 
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/button_add"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="26dp"

    android:text="Add new" />

 <EditText
     android:id="@+id/editText1"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_below="@+id/button_add"
     android:layout_centerHorizontal="true"
     android:layout_margin="10dp"
     android:layout_marginTop="25dp"
     android:ems="10"
     android:hint="name of exercise" >

     <requestFocus />
 </EditText>

 <ListView
     android:id="@android:id/list"
     android:layout_width="match_parent"
     android:layout_height="270dp"
     android:layout_below="@+id/editText1"
     android:layout_centerHorizontal="true"
     android:layout_marginTop="32dp" >
 </ListView>

 <TextView
android:id="@+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text="@string/main_no_items"/>

And finally , custom adapter :

public class ExerciseAdapter extends ArrayAdapter<Exercise>{

      private final Context context;


      public ExerciseAdapter(Context context, int resource, ArrayList<Exercise> results) {
        super(context, resource, results);
        this.context = context;
      }

      /*private view holder class*/
        private class ViewHolder {
            ImageView imageView;
            TextView txtTitle;
            TextView txtDesc;
        }


        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;

            LayoutInflater mInflater = (LayoutInflater) context
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.exercise_item, null);
                holder = new ViewHolder();
                holder.txtDesc = (TextView) convertView.findViewById(R.id.description);
                holder.txtTitle = (TextView) convertView.findViewById(R.id.name);
                holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
                convertView.setTag(holder);
            } else
                holder = (ViewHolder) convertView.getTag();

            holder.txtDesc.setText(getItem(position).getDescription());
            Log.d("ITEM DESC", "ITEMDESC: " + getItem(position).getDescription() );
            holder.txtTitle.setText( getItem(position).getName());
            Log.d("ITEM NAME", "ITEMNAME: " + getItem(position).getName() );
            holder.imageView.setImageResource(R.drawable.ic_launcher);

            return convertView;
        }

    } 

As I mentioned before, there is no error, app runs ok. Correct icons and descriptions are displayed, but there are no names ( even though the are in logcat ). I tried to set hardcoded strings as names, but with no luck. There must be something in connection with layout, or am I wrong ?

Once again, thanks a lot in advance. Igor

Upvotes: 4

Views: 234

Answers (1)

Pulkit Sethi
Pulkit Sethi

Reputation: 1325

remove these lines android:layout_alignParentBottom="true" android:layout_alignParentRight="true" from

Upvotes: 1

Related Questions