0

我想添加一個自定義視圖,我對我的適配器。我試圖這樣做,以添加一個視圖,以通過commonsware合併適配器。試圖膨脹佈局在活動不工作

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

這是我的我的代碼試圖誇大佈局:

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); 

這是我的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> 

更新:忘了,說說我的 「問題」 是什麼。首先,我之前從未誇大佈局,所以我不確定它是如何工作的,以及膨脹的正確方法。其次,我得到這個錯誤:

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

編輯:

我進口:

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 { 
+1

什麼問題?兩個標題都顯示「一個」?你必須膨脹兩個不同的行變量。你正在重複使用相同的「行」視圖的兩個頭。 –

+0

@ A - C用我的「問題」更新了我的問題抱歉! – EGHDK

+0

一旦你弄明白了,我指出的另一個問題。對於這個'CCE',我在* Merge Demo *中看到'Button'是用Activity Context創建的;它沒有膨脹。這樣做有效嗎? –

回答

0

更改您的以下行

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

View row = inflater.inflate(R.layout.row, 
     null); 
+0

試過這個,並得到:01-20 23:42:45.505:E/AndroidRuntime(28738):java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams不能轉換爲android.widget.AbsListView $ LayoutParams – EGHDK

+0

請張貼一些更多的代碼。 @EGHDK您發佈的代碼不足以解決您的問題。 – GrIsHu

+0

我現在包括我的包括陳述 – EGHDK

0

我想,你沒有添加viewparent

嘗試

yourParent.addView(row);

哪裏yourParent是你需要附上這個row

0

試試這個佈局...

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); 
+0

01-20 23:50:13.699:E/AndroidRuntime(29425):java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams不能轉換爲android.widget.AbsListView $ LayoutParams – EGHDK

+0

@EGHDK您應該添加實例LinearLayout到合併適配器,而不是TextView,因爲TextView是LinearLayout的子項,它具有LinearLayout.LayoutParams ... –

+0

@EGHDK請參閱我編輯的答案... –

0

嘗試刪除以下l國家統計局。

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

取而代之的是下面的代碼。

View row = inflater.inflate(R.layout.row, null); 
+0

答案與[此答案在這個完全相同之間有什麼區別問題?](http://stackoverflow.com/a/21249293/1815485) –

2

使用兩級佈局的資源,而不是一個請遵守良好做法。不要膨脹佈局,然後嘗試分割佈局並單獨使用它們。

或者,換一種方式,價值傳遞給addView()需要爲下列之一:

  • inflate(),或
  • 東西直接結果你在Java中創建使用構造
+0

啊哈。得到它的工作。謝謝。 – EGHDK