我試圖讓我自己的基礎適配器動態顯示ListFragment中的RadioButtons,代碼崩潰,我不知道究竟是什麼原因,但是當我嘗試在ListView上設置適配器時,會發生這種情況。知道我做錯了什麼?
的XML單選按鈕:
如何創建自定義基礎適配器?
<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rbTipo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
ListView的XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
適配器的類別:
public class TipoAdapter extends BaseAdapter {
private ArrayList<Tipo> tipos;
private Context context;
public TipoAdapter(Context context,ArrayList<Tipo> tipos) {
this.tipos = tipos;
this.context = context;
}
@Override
public int getCount() {
return tipos.size();
}
@Override
public Tipo getItem(int i) {
return tipos.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if(view == null){
view= LayoutInflater.from(context).inflate(R.layout.item_tipo_trilha,viewGroup,false);
holder= new ViewHolder();
holder.rb=(RadioButton) view.findViewById(R.id.rbTipo);
view.setTag(holder);
}else{
holder=(ViewHolder)view.getTag();
}
Tipo b = getItem(i);
holder.rb.setText(b.tipNome);
holder.rb.setTag(i);
return view;
}
private static class ViewHolder{
RadioButton rb;
public ViewHolder() {
}
}
的ListFragment類別:
public class TiposFragment extends ListFragment {
private ArrayList<Tipo> tipos;
private TipoAdapter tipoAdapter;
private int selecionado;
private ListView listView;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onListItemClick(ListView lv, View v, int position, long id) {
super.onListItemClick(lv, v, position, id);
selecionado = tipoAdapter.getItem(position).tipCod;
for(int i=0;i<lv.getCount();i++){
if(i!=position){
RadioButton rb= (RadioButton) lv.getChildAt(i);
rb.setChecked(false);
}
}
Toast.makeText(getContext(), lv.getChildAt(position).toString(), Toast.LENGTH_SHORT).show();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tipo_trilha, container, false);
listView=(ListView) view.findViewById(android.R.id.list);
try {
Bundle bundle = getArguments();
if (bundle != null) {
tipos = bundle.getParcelableArrayList("tipos");
}
} catch (Exception e) {
Toast.makeText(getContext(), e.toString(), Toast.LENGTH_LONG).show();
}
if (tipos != null) {
try {
tipoAdapter = new TipoAdapter(getContext(), tipos);
listView.setAdapter(tipoAdapter);
setListAdapter(tipoAdapter);
// Toast.makeText(getContext(),String.valueOf(listaTipos.getCount()),Toast.LENGTH_LONG).show();
} catch (Exception e) {
// Toast.makeText(getContext(), e.toString(), Toast.LENGTH_LONG).show();
Log.d("TAG",e.toString());
Toast.makeText(getContext(),e.toString(),Toast.LENGTH_LONG).show();
}
}
return view;
}
的logcat的堆棧跟蹤:
05-09 10:23:03.309 909-909/bdes.dcc.joinville.udesc.br.biketrilhas E/AndroidRuntime: FATAL EXCEPTION: main
Process: bdes.dcc.joinville.udesc.br.biketrilhas, PID: 909
java.lang.ClassCastException: java.lang.Integer cannot be cast to br.udesc.joinville.dcc.bdes.adapter.TipoAdapter$ViewHolder
at br.udesc.joinville.dcc.bdes.adapter.TipoAdapter.getView(TipoAdapter.java:52)
at android.widget.AbsListView.obtainView(AbsListView.java:2396)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1290)
at android.widget.ListView.onMeasure(ListView.java:1202)
at android.view.View.measure(View.java:17834)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:698)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:482)
at android.view.View.measure(View.java:17834)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5725)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17834)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5725)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17834)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5725)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:17834)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5725)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17834)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5725)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17834)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5725)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17834)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5725)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2848)
at android.view.View.measure(View.java:17834)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2030)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1174)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1395)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1062)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5873)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5753)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
不要說它沒有從logcat發佈堆棧跟蹤崩潰。 –