有人可以告诉我我在创建自定义列表视图时犯了什么错误,因为使用自定义适配器时它不显示任何数据。虽然它可以在简单的arrayadapter上运行,但我不知道它是在我定制的适配器中存在一些错误吗?
这是我的主要xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SubCategories"
android:weightSum="10">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:gravity="center"
android:layout_weight="1">
<TextView
android:gravity="center"
android:id="@+id/suitablecategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Services"
android:textStyle="bold"
android:textAppearance="@android:style/TextAppearance.Large" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:gravity="center"
android:layout_weight="1">
<Spinner
android:id="@+id/Spin"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:gravity="center"
android:layout_weight="8">
<ListView
android:id="@+id/SubListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
</LinearLayout>
这是我的行项目Xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/CustomListItemName"
android:layout_gravity="center"
android:hint="Name"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.Medium"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:hint="Enter Price"
android:layout_gravity="center"
android:id="@+id/CustomListItemPrice"
android:inputType="number"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
这是我定制的适配器
package application.fyp.com.myfypapplication;
import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class PriceListAdapter extends ArrayAdapter<String> {
private Context context;
private List<String> items;
public PriceListAdapter(Context context, List<String> list) {
super(context, R.layout.custom_price_listview,R.id.CustomListItemName,list);
this.context = context;
this.items = list;
}
@Override
public int getCount() {
return 0;
}
@Nullable
@Override
public String getItem(int position) {
return super.getItem(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.custom_price_listview, parent,false);
TextView name = convertView.findViewById(R.id.CustomListItemName);
EditText price = convertView.findViewById(R.id.CustomListItemPrice);
String it = items.get(position);
name.setText(it);
return convertView;
}
}
这是我的主要课程,我在其中使用适配器初始化和设置列表视图
package application.fyp.com.myfypapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class SubCategories extends AppCompatActivity {
private Spinner spinner;
private ListView lv;
private Button addsc;
private PriceListAdapter priceadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub_categories);
spinner=(Spinner)findViewById(R.id.Spin);
lv = findViewById(R.id.SubListView);
List<String> subCategoryList = new ArrayList<>();
subCategoryList.add("A");
subCategoryList.add("B");
subCategoryList.add("C");
subCategoryList.add("D");
priceadapter = new PriceListAdapter(SubCategories.this, subCategoryList);
lv.setAdapter(priceadapter);
}
答案 0 :(得分:1)
在适配器中,您必须更改以下内容:
@Override
public int getCount() {
// return 0; // This is what you have now
return this.items.size(); // This is what it must be
}
getCount方法旨在返回列表视图中的项目数。