Android Studio ListView 自定义适配器无数据显示

标签 android listview arraylist custom-adapter

我正在尝试列出包含从 SQLite 数据库中提取的 Shop 类型的 ArrayList 中的列的行。因此,我有一个自定义适配器。

但是,没有显示任何数据。没有错误消息或异常。

我使用过调试器(我使用的是 Android Studio)。我知道没有调用覆盖的 getView 方法(未触发此方法中的断点)。构造函数中的断点被触发,预期的数据在 ArrayList 中。

ListView 已经为其他方法工作(我已经从一个简单的列表发展到一个简单的列表,其中数据在一个字符串数组中确认,在对现有数据的其他测试中,例如一个对话框显示数据,到一个简单的列表使用Shop类型的ArrayList,列出的是元素而不是数据。

行 View XML 可能存在问题。

如果有人告诉我关于上述问题的代码(如下所示)有什么问题,我将不胜感激。即为什么没有显示任何内容以及如何更正代码(如果时间允许)。

我想确认一下,我查看了很多与此相关的帖子并尝试了各种选项,您可以在代码中看到一些选项。

这是我认为相关的 MainShopActivity 的摘录(完整代码放在最后,因为它很丑陋并且有很多代码被注释掉了)。

    ShopListArrayAdapter adapter = new ShopListArrayAdapter(this,shoplist);
    //listview.setAdapter(adapter);
    setListAdapter(adapter);

这是 ShopListArrayAdapter

package mjt.shopper;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;

/**
 * Created by Mike092015 on 1/02/2016.
 */
public class ShopListArrayAdapter extends ArrayAdapter<ArrayList<Shop>> {
    private final Context context;
    private final ArrayList<Shop> shoplist;

    public ShopListArrayAdapter(Context context, ArrayList<Shop> shoplist) {
         super(context, R.layout.activity_shop_list_entry);
        this.context = context;
        this.shoplist = shoplist;
     }

    @Override
    public View getView(int position, View convertview, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.activity_shop_list_entry, parent, false);

        TextView textViewShopName = (TextView) rowView.findViewById(R.id.shop_name_entry);
        TextView textViewShopOrder = (TextView) rowView.findViewById(R.id.shop_order_entry);
        TextView textviewShopStreet = (TextView) rowView.findViewById(R.id.shop_street_entry);

        textViewShopName.setText(shoplist.get(position).getShopName());
         textViewShopOrder.setText(shoplist.get(position).getShopOrderAsString());
        textviewShopStreet.setText(shoplist.get(position).getShopstreet());
        return rowView;
    }
}

这是 ListeView XML activity_shop_list(ps 我尝试删除了四个按钮,结果是一样的。)

<?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: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="mjt.shopper.ShopListActivity">

<Button
    android:id="@+id/aslhbtn01"
    android:layout_width="@dimen/standard_button_width"
    android:layout_height="@dimen/standard_button_height"
    android:text="@string/standarddonebutton"
    android:textStyle="bold"
    android:onClick="doneWithShops"/>
<Button
    android:id="@+id/aslhbtn02"
    android:layout_width="@dimen/standard_button_width"
    android:layout_height="@dimen/standard_button_height"
    android:layout_toEndOf="@id/aslhbtn01"
    android:layout_toRightOf="@id/aslhbtn01"
    android:text="@string/standardaddbuttontext"
    android:textStyle="bold"
    android:onClick="addShop"
    />

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/aslhbtn01"/>

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@android:id/list">

    <Button
        android:id="@+id/aslfbtn01"
        android:layout_width="@dimen/standard_button_width"
        android:layout_height="@dimen/standard_button_height"
        android:text="@string/standarddonebutton"
        android:textStyle="bold"
        android:onClick="doneWithShops"/>
    <Button
        android:id="@+id/aslfbtn02"
        android:layout_width="@dimen/standard_button_width"
        android:layout_height="@dimen/standard_button_height"
        android:layout_toEndOf="@id/aslfbtn01"
        android:layout_toRightOf="@id/aslfbtn01"
        android:text="@string/standardaddbuttontext"
        android:textStyle="bold"
        android:onClick="addShop"/>

</RelativeLayout>

这是 RowView XML activity_shop_list_entry

<?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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".ShopListEntryActivity">
<TextView
    android:id="@+id/shop_name_entry"
    android:layout_width="100sp"
    android:layout_height="30sp"
    android:textStyle="bold" />
<TextView
    android:id="@+id/shop_order_entry"
    android:layout_width="50sp"
    android:layout_height="30sp" />
<TextView
    android:id="@+id/shop_street_entry"
    android:layout_width="300sp"
    android:layout_height="30sp" />
</LinearLayout>

最后是ShopListActivity的完整代码

    package mjt.shopper;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import java.util.ArrayList;

public class ShopListActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shop_list);
    int shopcount = 0;


    // Need to access the shopper database
    ShopperDBHelper shopperdb = new ShopperDBHelper(this, null, null,1);

    // Check to see if we should have action buttons at top and bottom of the list
    // if so then make the upper buttons invisible. Just in case if not then make
    // them visible.
    shopcount = shopperdb.numberOfShops();
    if(shopcount < 10) {
        findViewById(R.id.aslhbtn01).setVisibility(View.INVISIBLE);
        findViewById(R.id.aslhbtn02).setVisibility(View.INVISIBLE);
    } else {
        findViewById(R.id.aslhbtn01).setVisibility(View.VISIBLE);
        findViewById(R.id.aslhbtn02).setVisibility(View.VISIBLE);
    }

    // Call the getShops method setting an ArrayList with the returned ArrayList (list of Shop objects)
    final ArrayList<Shop> shoplist = shopperdb.getShops();

    //Testing ie display number of shops via both the numberOfShops method and via the ArrayList size method
    //     according to the ArrayList returned from the getShops method.
    //          numberOfShops method returns integer count
    //          getShops method returns an ArrayList of the Shop object per row, this containing all the data
    String r;   // initialise the string that is to be displayed

    // Testing Add the number of shops extracted (both count methods as described above)
    r = "Shop List Array contains " + shoplist.size() + " Elements.\nNumber of Shops returns "+shopcount+"\n";

    // Alternative/check simplelistview compliant array
    final String[] values = new String[shoplist.size()];
    int ix = 0;

    // Testing report on the contents of the returned list of Shops, also populate the simplelistview array
    for(Shop shop : shoplist) {
        values[ix++] = shop.getShopName();
        r=r+"Shop Name="+shop.getShopName()+
                " Order="+shop.getShopOrderAsString()+
                " Street="+shop.getShopstreet()+
                " City="+shop.getShopCity()+
                " State="+shop.getShopstate()+
                " Notes="+shop.getShopNotes()+
                " DBID="+shop.getShopId()+
                "\n\n";
    }
    // Testing report shop list according to the simplelistview array
    r = r+"\n\nExtract Shop names are:";
    for(int i=0;i < shoplist.size(); i++ ) {
        r=r+"\n     "+values[i];
    }

    // Build the simplelistview (ie just one column, the shop name)
    /*WORKS but now trying arraylist adapter
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,values);
    final ListView listview = (ListView) findViewById(R.id.aslv01);
    // Set up the onclick listener per list item
    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext(),"You Clicked on List Item #"+position+" Shop="+values[position]+". ALtMthd Shop="+shoplist.get(position).getShopName(),Toast.LENGTH_LONG).show();
        }
    });
    listview.setAdapter(adapter);
    */


    ShopListArrayAdapter adapter = new ShopListArrayAdapter(this,shoplist);
    //listview.setAdapter(adapter);
    setListAdapter(adapter);

    // sort of works shows the id of the shoplist element but for all shops
    //ArrayAdapter<Shop> adadpter = new ArrayAdapter<Shop>(this,android.R.layout.simple_list_item_1,shoplist);
    //listview.setAdapter(adapter);


    AlertDialog.Builder okdialog = new AlertDialog.Builder(this);
    okdialog.setMessage(r);
    okdialog.setCancelable(true);
    okdialog.setPositiveButton("OK",new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    okdialog.show();

}


public void doneWithShops(View view) { this.finish(); }
public void addShop(View view) {
    ShopperDBHelper shopperdb = new ShopperDBHelper(this, null, null, 1);
    final ArrayList<Shop> shoplist = shopperdb.getShops();

    Intent intent = new Intent(this,ShopAddActivity.class);
    startActivity(intent);
    final String[] values = new String[shoplist.size()];
    int ix = 0;
    for(Shop shop : shoplist) {
        values[ix++] = shop.getShopName();
    }
    //ArrayAdapter<String> adapter = new ArrayAdapter<String>(view.getContext(), android.R.layout.simple_list_item_1,values);
    //@@@@@@@@ final ListView listview = (ListView) findViewById(R.id.aslv01);
    //final ListView listview = (ListView) findViewById(R.id.aslv01);
}

最佳答案

您没有将数据集提交给父类。变化

public ShopListArrayAdapter(Context context, ArrayList<Shop> shoplist) {
      super(context, R.layout.activity_shop_list_entry);

public ShopListArrayAdapter(Context context, ArrayList<Shop> shoplist) {
 super(context, R.layout.activity_shop_list_entry, shoplist);

当然 shoplist 必须包含条目,否则什么也不会显示。在调用setAdapter Android 调用ArrayAdapter.getCount 后,检查是否需要调用getView。由于您既没有向父类显式提供数据集,也没有覆盖 getCount,因此从 Android POV 来看,您没有任何可显示的内容,因此 getView 没有被调用。

编辑

改变

public class ShopListArrayAdapter extends ArrayAdapter<ArrayList<Shop>> {

public class ShopListArrayAdapter extends ArrayAdapter<Shop> {

关于Android Studio ListView 自定义适配器无数据显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35125531/

相关文章:

java - Android解析base64Binary pgm到Bitmap

android - 在 AlertDialog 中重用 fragment

android - 如何避免使用 AudioRecord 进行自动增益控制?

android - 如何从 ListView 中获取所有选中的项目?

java - 我的 ArrayList<NameValuePair> 显示 NameValuePair 无法解析

java - 另一个将文本文件放入 Arraylist 的方法

android - 如何在 firemonkey Delphi XE 8 的 Android 通知栏(如 Facebook 等)中使用外部大图像?

android - 在 ListView 中默认选择一行并突出显示所选行 Android

android - 检测 ListView(或 ScrollView)内的滚动位置

java列表无法添加类然后更改原始类而不更改列表中的副本