gpt4 book ai didi

android - 自定义 ListView 不会被填充

转载 作者:行者123 更新时间:2023-11-30 04:36:06 25 4
gpt4 key购买 nike

我的布局分为 4 个部分,每个部分都有相同的布局。 4 个相对布局,每个布局有一个 ImageView 、 TextView 和 ListView 。布局可以被认为是一个 2x2 网格,带有一个 ImageView 、 TextView 和 ListView 的自定义布局。 ListView 由 2 个 TextView 组成。我有一个 ArrayAdapter<> 用于 ListView 。我在传递给适配器的一个数据结构中有 listview 的数据。当我运行应用程序时,布局的 ListView 区域是空白的,其中显示了 ImageView 和 TextView 。我尝试调试该问题,发现未调用适配器的 getView。

布局将如下所示。 application1 和下面的部分是 ListView 应该出现的地方。 http://i.imgur.com/9ifIg.png

主布局文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout android:id="@+id/app1"
android:layout_width="150dip"
android:layout_height="250dip"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">

<ImageView android:id="@+id/app1Img"
android:layout_width="175dip"
android:layout_height="wrap_content"
android:src="@drawable/icon"
android:layout_centerHorizontal="true"
android:layout_below="@id/app1Data"
android:background="@color/blue"/>

<TextView android:id="@+id/app1Heading"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_below="@id/app1Img"
android:paddingLeft="25dip"
android:background="@color/color2"/>
<ListView android:id="@+id/app1Data"
android:layout_width="fill_parent"
android:layout_height="200dip"
android:scrollbars="vertical">
</ListView>

</RelativeLayout>


<RelativeLayout android:id="@+id/app2"
android:layout_width="150dip"
android:layout_height="250dip"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true">

<ImageView android:id="@+id/app2Img"
android:layout_width="175dip"
android:layout_height="wrap_content"
android:src="@drawable/icon"
android:layout_centerHorizontal="true"
android:background="@color/blue"/>

<TextView android:id="@+id/app2Heading"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/app2Img"
android:paddingLeft="25dip"
android:background="@color/color2"/>

<ListView android:id="@+id/app2Data"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layout_below="@id/app2Heading"
android:background="@color/color3">
</ListView>
</RelativeLayout>


<RelativeLayout android:id="@+id/app3"
android:layout_width="150dip"
android:layout_height="250dip"
android:layout_alignParentLeft="true"
android:layout_below="@id/app1">

<ImageView android:id="@+id/app3Img"
android:layout_width="175dip"
android:layout_height="wrap_content"
android:src="@drawable/icon"
android:layout_centerHorizontal="true"
android:background="@color/blue"/>

<TextView android:id="@+id/app3Heading"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/app3Img"
android:paddingLeft="25dip"
android:background="@color/color2"/>

<ListView android:id="@+id/app3Data"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layout_below="@id/app3Heading"
android:background="@color/color3">
</ListView>
</RelativeLayout>
<RelativeLayout android:id="@+id/app4"
android:layout_width="150dip"
android:layout_height="250dip"
android:layout_alignParentRight="true"
android:layout_below="@id/app2">

<ImageView android:id="@+id/app4Img"
android:layout_width="175dip"
android:layout_height="wrap_content"
android:src="@drawable/icon"
android:layout_centerHorizontal="true"
android:background="@color/blue"/>

<TextView android:id="@+id/app4Heading"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/app4Img"
android:paddingLeft="25dip"
android:background="@color/color2"/>

<ListView android:id="@+id/app4Data"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layout_below="@id/app4Heading"
android:background="@color/color3">
</ListView>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>

ListView 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView android:id="@+id/mainText"
android:layout_width="150dip"
android:layout_height="wrap_content"
/>

<TextView android:id="@+id/subscript"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:lines="2"
android:singleLine="false"/>
</LinearLayout>

主要 Activity :

TextView app1Header;
ImageView app1Img;
ListView app1Data;


TextView app2Header;
ImageView app2Img;
ListView app2Data;


TextView app3Header;
ImageView app3Img;
ListView app3Data;


TextView app4Header;
ImageView app4Img;
ListView app4Data;

ApplicationData applicationData = _allApplicationData.get(0);
app1Header.setText(applicationData.mHeadlineText);
ArrayList<DashboardListItem> application1ListItems = new ArrayList<DashboardListItem>();
DashboardListItem application1ListItem1 = new DashboardListItem();
DashboardListItem application1ListItem2 = new DashboardListItem();
application1ListItem1.mainText = getApplicationMainTextForRow(0, applicationData.mApplicationName, _organization);
application1ListItem1.subscript = getApplicationSubscriptForRow(0, applicationData.mApplicationName, _organization);
application1ListItems.add(application1ListItem1);
application1ListItem2.mainText = getApplicationMainTextForRow(1, applicationData.mApplicationName, _organization);
application1ListItem2.subscript = getApplicationSubscriptForRow(1, applicationData.mApplicationName, _organization);
application1ListItems.add(application1ListItem2);
DashboardListAdapter application1Adapter = new DashboardListAdapter(getApplicationContext(), R.layout.dashboardlist, application1ListItems);
app1Data.setAdapter(application1Adapter);

applicationData = _allApplicationData.get(1);
app2Header.setText(applicationData.mHeadlineText);
ArrayList<DashboardListItem> application2ListItems = new ArrayList<DashboardListItem>();
DashboardListItem application2ListItem = new DashboardListItem();
application2ListItem.mainText = getApplicationMainTextForRow(0, applicationData.mApplicationName, _organization);
application2ListItem.subscript = getApplicationSubscriptForRow(0, applicationData.mApplicationName, _organization);
application2ListItems.add(application2ListItem);
DashboardListAdapter application2Adapter = new DashboardListAdapter(getApplicationContext(), R.layout.dashboardlist, application2ListItems);
app2Data.setAdapter(application2Adapter);

applicationData = _allApplicationData.get(2);
app3Header.setText(applicationData.mHeadlineText);
ArrayList<DashboardListItem> application3ListItems = new ArrayList<DashboardListItem>();
DashboardListItem application3ListItem = new DashboardListItem();
application3ListItem.mainText = getApplicationMainTextForRow(0, applicationData.mApplicationName, _organization);
application3ListItem.subscript = getApplicationSubscriptForRow(0, applicationData.mApplicationName, _organization);
application3ListItems.add(application3ListItem);
DashboardListAdapter application3Adapter = new DashboardListAdapter(getApplicationContext(), R.layout.dashboardlist, application3ListItems);
app3Data.setAdapter(application3Adapter);

applicationData = _allApplicationData.get(3);
app4Header.setText(applicationData.mHeadlineText);
ArrayList<DashboardListItem> application4ListItems = new ArrayList<DashboardListItem>();
DashboardListItem application4ListItem = new DashboardListItem();
application4ListItem.mainText = getApplicationMainTextForRow(0, applicationData.mApplicationName, _organization);
application4ListItem.subscript = getApplicationSubscriptForRow(0, applicationData.mApplicationName, _organization);
application4ListItems.add(application4ListItem);
DashboardListAdapter application4Adapter = new DashboardListAdapter(getApplicationContext(), R.layout.dashboardlist, application4ListItems);
app4Data.setAdapter(application4Adapter);

适配器文件:

public class DashboardListAdapter extends ArrayAdapter<DashboardListItem>{

private Context mContext;
private ArrayList<DashboardListItem> dashBoardData;
int resource;

public DashboardListAdapter(Context context, int textViewResourceId,
ArrayList<DashboardListItem> objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
mContext = context;
resource = textViewResourceId;
dashBoardData = objects;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}

@Override
public DashboardListItem getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
if(convertView == null){
convertView = new LinearLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater vi;
vi = (LayoutInflater) getContext().getSystemService(inflater);
v = vi.inflate(resource,null);
}
//DashboardListItem appData = dashBoardData.get(position);
DashboardListItem listItem = dashBoardData.get(position);
TextView mainText = (TextView) v.findViewById(R.id.mainText);
TextView subscript = (TextView)v.findViewById(R.id.subscript);

mainText.setText(listItem.mainText);
subscript.setText(listItem.subscript);
return convertView;
}

我在编译或运行应用程序时没有收到任何错误。只是 getView() 没有被调用。要传递给适配器的数据数组列表是手动填充和传递的,以确保数据确实被填充。我已从日志中检查数据不为空/空。

我被这个严重困住了。我尝试了很多搜索,但仍然无法找到为什么没有调用 getView() 的原因。

最佳答案

我找到了问题的解决方案。这是我的错误。在适配器 getview 中,我犯了 2 个错误。一个是使用我传递给构造函数的资源来扩充 View ,而不是使用 R 直接引用布局。第二个是我将它扩充以查看 v,因为我正在返回转换 View 。无论如何,感谢您的时间和帮助。

关于android - 自定义 ListView 不会被填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6822287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com