我正在尝试添加我对我的适配器所做的自定义视图。我正在尝试通过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 {
答案 0 :(得分:3)
使用两个布局资源,而不是一个。不要给布局充气,然后尝试拍摄那些布局并单独使用它们。
或者,换句话说,传递给addView()
的值必须是:
inflate()
或答案 1 :(得分:0)
更改以下内容
LinearLayout row = (LinearLayout) inflater.inflate(R.layout.row,
null);
到
View row = inflater.inflate(R.layout.row,
null);
答案 2 :(得分:0)
我认为您没有将view
添加到parent
试
yourParent.addView(row);
yourParent
是您需要附加此row
答案 3 :(得分: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);
答案 4 :(得分:0)
尝试删除以下行。
LinearLayout row = (LinearLayout) inflater.inflate(R.layout.row, null);
而是放下以下代码。
View row = inflater.inflate(R.layout.row, null);
答案 5 :(得分:0)