gpt4 book ai didi

android - android 中的自定义 listView 不起作用

转载 作者:行者123 更新时间:2023-11-29 15:39:03 25 4
gpt4 key购买 nike

<分区>

我正在尝试制作自定义 listView,但安装时应用程序崩溃了。我检查了 android 监视器,但它对我没有帮助。我是安卓新手。

这段代码在MainActivity中:

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

String[] name = {"Nishant", "Raj kumar", "Golu"};
Integer[] total = {1204, 2411, 1001};
Integer[] entries = {12, 15, 24};

ListView listView = (ListView)findViewById(R.id.list_view);

CustomAdapter adapter = new CustomAdapter(this, name, total, entries);

listView.setAdapter(adapter);

此代码在 CustomAdapter 中:

public class CustomAdapter extends ArrayAdapter<String>{

private final Activity context;
private final String[] name;
private final Integer[] total_expenditure;
private final Integer[] total_entries;

public CustomAdapter(Activity context, String[] name, Integer[] expenditure, Integer[] entries){
super(context, R.layout.list_row, name);
this.context = context;
this.name = name;
this.total_entries = entries;
this.total_expenditure = expenditure;
}

@Override
public View getView(int position, View v, ViewGroup parent){
LayoutInflater inflater = context.getLayoutInflater();
View view = inflater.inflate(R.layout.list_row, null, true);

TextView name = (TextView)view.findViewById(R.id.person_name);
TextView total_expenditure = (TextView)view.findViewById(R.id.person_total_expenditure);
TextView total_entries = (TextView)view.findViewById(R.id.person_total_entries);

name.setText(this.name[position]);
total_entries.setText(this.total_entries[position]);
total_expenditure.setText(this.total_expenditure[position]);

return view;
}
}

这是List_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_add_people"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.nishant.kitchenbook.AddPeople">


<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/person_name"
android:text="name kumar"
android:textColor="@android:color/black"
android:layout_alignParentTop="true"
android:textSize="25sp"
android:textStyle="normal|bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:text="Total : "
android:id="@+id/total"
android:textStyle="normal|bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/person_total_expenditure"
android:text="1255"
android:textColor="@android:color/holo_red_dark"
android:layout_toEndOf="@id/total"
android:layout_alignParentBottom="true"
android:textStyle="normal|bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Entries : "
android:id="@+id/entries"
android:textStyle="normal|bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/person_total_entries"
android:text="100"
android:textColor="@color/colorPrimaryDark"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>

这是 Android Monitor 的输出:

06-11 00:44:52.504 10569-10569/com.example.nishant.kitchenbook D/AndroidRuntime: Shutting down VM
06-11 00:44:52.505 10569-10569/com.example.nishant.kitchenbook E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.nishant.kitchenbook, PID: 10569
android.content.res.Resources$NotFoundException: String resource ID #0xc
at android.content.res.Resources.getText(Resources.java:328)
at android.content.res.MiuiResources.getText(MiuiResources.java:123)
at android.widget.TextView.setText(TextView.java:4432)
at com.example.nishant.kitchenbook.CustomAdapter.getView(CustomAdapter.java:35)
at android.widget.AbsListView.obtainView(AbsListView.java:2346)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1281)
at android.widget.ListView.onMeasure(ListView.java:1188)
at android.view.View.measure(View.java:18811)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:716)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:462)

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