android中的自定义listView不起作用

时间:2017-06-10 19:30:16

标签: android listview

我正在尝试创建自定义listView但是tge应用程序在安装时崩溃了。我检查了android显示器,但它对我没用。我是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)

1 个答案:

答案 0 :(得分:4)

那是因为您正在将int传递给setText()方法。传递int时,Android认为它是字符串resId。您必须将其转换为字符串才能显示它:

total_entries.setText(this.total_entries[position]+"");
        total_expenditure.setText(this.total_expenditure[position]+"");