Skip to content Skip to sidebar Skip to footer

Listview Rows With Different Layouts

What I am trying to achieve is to have different layouts for each row. The code in my ArrayAdapter is as follows: @Override public int getCount() { return contentList.s

Solution 1:

That NullPointerException occurs because you didn't implement the getItemViewType. Right now you return the first type of row layout no matter what and this is a layout that doesn't have the *txtView_test* TextView). Your code should be something like this:

// some fieldspublicstaticfinalintFIRST_TYPE=0;
publicstaticfinalintSECOND_TYPE=1;

@OverridepublicintgetItemViewType(int position) {
     MyContentsitem= getItem(position);
     if (item.testBoolean()) {
          return FIRST_TYPE;
     } else {
          return SECOND_TYPE; 
     } 
}

@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
    finalMyContentsentry= content.get(position);
    Viewrow= convertView;
    LayoutInflaterinflater=null;
    inttype= getItemViewType(position);
    boolean_haschild_= entry.testBoolean();
    if (row == null) {
        if (type == FIRST_TYPE) {
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater
                    .inflate(R.layout.contents_layout, null);
        } else {
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.test_layout, null);
        }
    } 
    if (type == FIRST_TYPE) {
        TextViewtxtView= (TextView) row
                .findViewById(R.id.txtView);
        //.....
    } else {
        ((TextView) row.findViewById(R.id.txtView_test)) //**LOE**
                .setText("test: " + position); //**LOE**
    }

Solution 2:

Use,

LayoutInflaterinflater= (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    Viewrow= inflater.inflate(R.layout.test_layout, parent, false);

May it solves your problem.

Solution 3:

Try This :::

convertView = getLayoutInflater().inflate(R.layout.my_row_layout, null);


TextViewtxtName= (TextView)convertView.findViewById(R.id.txtMyTextView);

txtName.setText("Hi,,,,. Hows You..."); 

Post a Comment for "Listview Rows With Different Layouts"