我不想在BaseAdapter
中使用布局文件。我想在基本适配器中创建视图,因此我可以为每个listview
调用此适配器。
我的代码不起作用。
这是我的代码:
public class MyAdapter extends BaseAdapter {
private Context context;
private List<PublicObject> objects;
public MyAdapter(Context context, List<PublicObject> objects)
{
this.context = context;
this.objects = objects;
}
@Override
public int getCount() {
return this.objects.size();
}
@Override
public Object getItem(int position) {
return this.objects.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private TextView CreateTextView()
{
TextView txtView = new TextView(this.context);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
txtView.setGravity(View.TEXT_ALIGNMENT_CENTER);
txtView.setLayoutParams(lp);
txtView.setTextSize(14);
txtView.setTextColor(Color.BLACK);
return txtView;
}
protected static class ViewHolder
{
private List<TextView> viewList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null)
{
LinearLayout lnr = new LinearLayout(this.context);
lnr.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lnr.setLayoutParams(lp);
List<TextView> viewList = new ArrayList<>();
for(int i = 0, limit = this.objects.get(0).GetList().size(); i < limit; i++) {
TextView txt = this.CreateTextView();
lnr.addView(txt);
viewList.add(txt);
}
convertView = lnr;
viewHolder = new ViewHolder();
viewHolder.viewList = viewList;
convertView.setTag(viewHolder);
}
else viewHolder = (ViewHolder) convertView.getTag();
List<TextView> views = viewHolder.viewList;
List<String> s = this.objects.get(position).GetList();
for(int i = 0, limit = s.size(); i < limit; i++)
{
TextView txt = views.get(i);
txt.setText(s.get(i));
}
return convertView;
}
}
这是我的模特课,
public class PublicObject {
private List<String> list;
public PublicObject(List<String> list) {
this.list = list;
}
public List<String> GetList() {
return this.list;
}
}
错误
9 16:30:23.668 302-302 / com.example.deryaaxis.searchviewtempalte E / AndroidRuntime:FATAL EXCEPTION:main 处理:com.example.deryaaxis.searchviewtempalte,PID:302 java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法强制转换为android.widget.AbsListView $ LayoutParams 在android.widget.ListView.measureScrapChild(ListView.java:1183) 在android.widget.ListView.measureHeightOfChildren(ListView.java:1265) 在android.widget.ListView.onMeasure(ListView.java:1175) 在android.view.View.measure(View.java:16820) 在android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719) 在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455) 在android.view.View.measure(View.java:16820) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5156) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 在android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135) 在android.view.View.measure(View.java:16820) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5156) 在android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391) 在android.view.View.measure(View.java:16820) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5156) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 在android.view.View.measure(View.java:16820) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5156) 在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:695) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 在android.view.View.measure(View.java:16820) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5156) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 在com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2295) 在android.view.View.measure(View.java:16820) 在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1962) 在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1156) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1341) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1043) 在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5885) 在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:771) 在android.view.Choreographer.doCallbacks(Choreographer.java:574) 在android.view.Choreographer.doFrame(Choreographer.java:544) 在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:757) 在android.os.Handler.handleCallback(Handler.java:733) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:149) 在android.app.ActivityThread.main(ActivityThread.java:5257) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:515) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:794) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610) 在dalvik.system.NativeStart.main(本地方法)
答案 0 :(得分:0)
这里的问题可能是因为您使用LayoutParams for Linear Layout作为textView,然后将该textView放入listView。 您应该将LinearLayout.LayoutParams更改为此
txtView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));