Skip to content Skip to sidebar Skip to footer

Listview: Only One List Item With Multiple Textviews

I need to show multiple text views in only one list item and the rest of the list view items will have just one textview. How do I achieve this? Any samples or tutorials you can p

Solution 1:

Make your own adapter:

BaseAdapter already provieds methods for working with different View types (different layout for list item cells) and to effektivly recycle views:

  • getViewTypeCount(): The count of how many different Views (layouts) are present.
  • getItemViewType(): Returns a integer to identify the view type of the item in the cell. Note the internally BaseAdapter implementation uses an array. Therefore your values returned here must be from 0 to n. You are not allowed to skip indexes.

    public class MyAdapter extends BaseAdapter {

    privatefinalintVIEW_TYPE_NORMAL=0;
    privatefinalintVIEW_TYPE_4_TEXTS=1;
    
    /**
     * The inflater for
     */protected LayoutInflater inflater;
    protected Context context;
    
    publicMyAdapter(Context context) {
        this.context = context;
        this.inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
    @OverridepublicintgetViewTypeCount() {
        // There are two view typesreturn2;
    }
    
    @OverridepublicintgetItemViewType(int position) {
        if (position == 0)
            return VIEW_TYPE_4_TEXTS;
        elsereturn VIEW_TYPE_NORMAL;
    }
    
    @Overridepublic View getView(int position, View convertView, ViewGroup parent) {
        inttype= getItemViewType(position);
        if (convertView == null) {
            convertView = newView(type, parent);
        }
        bindView(position, type, convertView);
        return convertView;
    }
    
    @OverridepubliclonggetItemId(int position) {
        return0; // replace it with your id, if you have stable ids in your// adapter
    }
    
    /** Inflates the correct view accorind the view type **/public View newView(int type, ViewGroup parent) {
    
        if (VIEW_TYPE_4_TEXTS == type) {
            Viewview= inflater.inflate(R.layout.your_layout_with_4_textviews,
                    parent, false);
            view.setTag(newViewHolder4TextViews(view)); // ViewHolder patternreturn view;
        }
    
        // Otherwise its a normal item with VIEW_TYPE_NORMALViewview= inflater
                .inflate(R.layout.your_normal_layout, parent, false);
        view.setTag(newNormalViewHolder(view)); // ViewHolder patternreturn view;
    
    }
    
    /** Bind the data for the specified {@code position} to the {@code view}. */publicvoidbindView(int position, int type, View view) {
    
        if (VIEW_TYPE_4_TEXTS == type) {
            // set the 4 text view valuesViewHolder4TextViewsholder= (ViewHolder4TextViews) view.getTag();
    
            holder.textview1.setText("...");
            holder.textview2.setText("...");
            holder.textview3.setText("...");
            holder.textview4.setText("...");
    
        } else {
            // VIEW_TYPE_NORMALNormalViewHolderholder= (NormalViewHolder) view.getTag();
    
            holder.textview.setText("...");
        }
    
    }
    

    }

Solution 2:

Use a custom adapter for you list and edit the getView function:

publicclassMyListAdapterextendsBaseAdapter {

    [...]

    @Overridepublic View getView(int position, View view, ViewGroup parentView) {
            LayoutInflaterinflater= (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if(position < 1) {
                /* Inflate a layout with 4 textview */
                view = inflater.inflate(R.layout.your_4_textview_layout, parentView, false);

            } else {
                /* Inflate a layout with 1 textview */
                view = inflater.inflate(R.layout.your_1_textview_layout, parentView, false);

            }

            return view;

    }

    [...]

}

Take care about pass Context context to MyListAdapter in constructor like this answer

Solution 3:

You can make changes in your adapter...some thing like this. @position 1: "blank" view will be diplayed @rest positions: "triplistrow" will bw displayed

@Overridepublic View getView(finalint position, View convertView, final ViewGroup parent) 
  {         
      ViewrowView= convertView;
      if(position !=1)
      {
      if (rowView == null) 
      {
          LayoutInflaterinflater= context.getLayoutInflater();
          rowView = inflater.inflate(R.layout.triplistrow, null);
          ViewHolderviewHolder=newViewHolder();

          viewHolder.no = (TextView) rowView.findViewById(R.id.TVTripNo);
          viewHolder.name = (TextView) rowView.findViewById(R.id.TVTripName);
          viewHolder.date = (TextView) rowView.findViewById(R.id.TVTripDate);

          rowView.setTag(viewHolder);
       }
      }
      else
      {
          LayoutInflaterinflater= context.getLayoutInflater();
          rowView = inflater.inflate(R.layout.triplistrow, null);
          ViewHolderviewHolder=newViewHolder();

          rowView.setTag(viewHolder);
      }

      if(position!=1)
      {
      finalViewHolderholder= (ViewHolder) rowView.getTag();  

      holder.no.setText(String.valueOf(position+1));
      holder.name.setText(values.get(position).name);
      holder.date.setText(values.get(position).fromdate + " to " + values.get(position).tilldate);

      }else
      {



      }


    return rowView;
  }

Post a Comment for "Listview: Only One List Item With Multiple Textviews"