1

I'm trying to add a custom view that I made to an adapter of mine. I'm trying to do this to add a view to merge adapter by commonsware.

https://github.com/commonsguy/cwac-merge

This is my code of me trying to inflate the layout:

    LayoutInflater inflater = getLayoutInflater();
    LinearLayout row = (LinearLayout) inflater.inflate(R.layout.row,
            null);


        TextView headerOne = (TextView) row
                .findViewById(R.id.titleTextView);
        TextView headerTwo = (TextView) row
                .findViewById(R.id.titleTextView);
        headerOne.setText("One");
        headerTwo.setText("Two");

        mergeAdapter.addView(headerOne);
        mergeAdapter.addAdapter(myArrayAdapter);

        mergeAdapter.addView(headerTwo);
        mergeAdapter.addAdapter(myOtherArrayAdapter);

This is my xml.

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:layout_weight="1"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/titleTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif-condensed"
        android:text="message text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/text_gray"
        android:textStyle="bold" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="#000000" />
</LinearLayout>

Update: Forgot to talk about what my "problem" is. First, is that I've never inflated a layout before so I'm unsure of how it works/whats the proper way to inflate. Second, I get this error:

01-20 23:18:46.427: E/AndroidRuntime(24810): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

EDIT:

My imports:

import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.commonsware.cwac.merge.MergeAdapter;
import com.google.gson.JsonObject;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;

import com.eghdk.myapp.R;
import com.eghdk.myapp.util.AppUtil;
import com.eghdk.myapp.util.DatabaseHelper;

public class MyActivity extends SherlockListActivity {
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
EGHDK
  • 17,818
  • 45
  • 129
  • 204
  • 1
    What's the problem? Both headers show `"One"`? You have to inflate two different row variables. You're reusing the same `row` View for both Headers. – A--C Jan 21 '14 at 04:27
  • @A--C updated my question with my "problem" Sorry! – EGHDK Jan 21 '14 at 04:32
  • What I pointed is another problem once you figure this out. For this `CCE`, I see in the *Merge Demo* that the `Button` is created with the Activity Context; it is not inflated. Does doing that work? – A--C Jan 21 '14 at 04:48
  • I spoke to the creator of the library and he said that inflating and adding the view should work. – EGHDK Jan 21 '14 at 05:01

6 Answers6

3

Use two layout resources, not one. Do not inflate a layout, then attempt to take pieces of that layout and use them separately.

Or, to put it another way, the value you pass to addView() needs to be either:

  • the direct result from inflate(), or
  • something you create in Java using constructors
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
1

try this...

    LayoutInflater inflater = getLayoutInflater();

    LinearLayout row = (LinearLayout) inflater.inflate(R.layout.row, null);
    TextView headerOne = (TextView) row.findViewById(R.id.titleTextView);
    headerOne.setText("One");
    ViewParent parent = row.getParent();
    if(parent != null && parent instanceof ViewGroup) {
        ((ViewGroup)parent).removeView(row);
    }
    AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
    row.setLayoutParams(layoutParams);

    mergeAdapter.addView(row);
    mergeAdapter.addAdapter(myArrayAdapter);

    row = (LinearLayout) inflater.inflate(R.layout.row, null);
    TextView headerTwo = (TextView) row.findViewById(R.id.titleTextView);
    headerTwo.setText("Two");
    parent = row.getParent();
    if(parent != null && parent instanceof ViewGroup) {
        ((ViewGroup)parent).removeView(row);
    }
    layoutParams = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
    row.setLayoutParams(layoutParams);

    mergeAdapter.addView(row);
    mergeAdapter.addAdapter(myOtherArrayAdapter);
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
  • 01-20 23:50:13.699: E/AndroidRuntime(29425): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams – EGHDK Jan 21 '14 at 04:52
  • @EGHDK You should add the instance of LinearLayout to the merge adapter, not TextView as TextView is child of LinearLayout and it has LinearLayout.LayoutParams... – Gopal Gopi Jan 21 '14 at 04:55
0

Change your below line

 LinearLayout row = (LinearLayout) inflater.inflate(R.layout.row,
        null);

to

 View row = inflater.inflate(R.layout.row,
        null);
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • Tried this and got: 01-20 23:42:45.505: E/AndroidRuntime(28738): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams – EGHDK Jan 21 '14 at 04:43
  • Please post some more code. @EGHDK The code which you have posted is not sufficient to catch your issue. – GrIsHu Jan 21 '14 at 04:45
  • I'm including my include statements now – EGHDK Jan 21 '14 at 04:53
  • @EGHDK What to do with your import statements ? – GrIsHu Jan 21 '14 at 04:59
0

I think, you didn't add the view to parent

try

yourParent.addView(row);

Where yourParent is the layout where you need to attach this row

Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64
0

Try to remove following line.

LinearLayout row = (LinearLayout) inflater.inflate(R.layout.row, null);

Instead this put following code.

View row = inflater.inflate(R.layout.row, null);
Jitesh Dalsaniya
  • 1,917
  • 3
  • 20
  • 36
  • What is the difference between your answer and [this answer, on this exact same question?](http://stackoverflow.com/a/21249293/1815485) – A--C Jan 21 '14 at 04:52
0

Please follow good practices while inflating layout in android http://www.doubleencore.com/2013/05/layout-inflation-as-intended/

http://www.piwai.info/android-adapter-good-practices/

Deniz
  • 12,332
  • 10
  • 44
  • 62