2017-06-10 10 views
1

カスタムlistViewを作成しようとしていますが、インストール時にアプリケーションがクラッシュしました。私はアンドロイドモニターをチェックしましたが、それは私には役に立たなかった。私はアンドロイドに新しいです。アンドロイドのカスタムリストビューが機能していません

このコードは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) 
+0

従来のメソッドオーバーロードの場合。それはときどき噛むことができます – peter

答えて

4

あなたはsetText()メソッドにint型を渡しているためです。 intを渡すと、Androidは文字列であるとみなしますresId。それを表示するには、文字列に変換する必要があります。

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

ありがとう、それは働いた。 –

+0

確かに、私はそうするのに7分待たなければなりません。ありがとう –

+0

あなたは私に教えてください、どのようにこれを非常に迅速にキャッチしましたか?私はあなたがアンドロイドモニターでこれを取得したことを意味します。 –

関連する問題