gpt4 book ai didi

android - 自定义ListView不显示

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

有人能告诉我我在制作自定义 ListView 时犯了什么错误吗,因为它在使用自定义适配器时不显示任何数据。虽然它在一个简单的数组适配器上工作,但我不明白我定制的适配器是否有错误??

这是我的主要 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;
}

}

这是我的主类,我在其中使用适配器初始化和设置 ListView

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);



}

最佳答案

在您的适配器中,您必须更改以下内容:

@Override
public int getCount() {
// return 0; // This is what you have now
return this.items.size(); // This is what it must be
}

getCount 方法旨在返回 ListView 中的项目数。

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

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