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

时间:2016-02-01 08:00:21

标签: android listview arraylist custom-adapter

我试图列出包含从使用Shop类型填充的ArrayList中的列的行,这些列是从SQLite数据库中提取的。因此,我有一个自定义适配器。

但是,没有数据显示。没有错误消息或例外。

我已经使用过调试器(我使用的是Android Studio)。我知道重写的getView方法没有被调用(此方法中的断点未被触发)。触发了构造函数中的断点,并且期望的数据位于ArrayList中。

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

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

}

2 个答案:

答案 0 :(得分:5)

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

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中没有任何要显示的内容,因此不会调用ergo getView

修改

更改

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

public class ShopListArrayAdapter extends ArrayAdapter<Shop> {

答案 1 :(得分:2)

ShopListAdapter

public int getCount () {
    return shoplist.size();    
}

您必须告知您的适配器列表中的项目数。因为您在没有列表(super(context, R.layout.activity_shop_list_entry);)的情况下调用超级构造函数,所以您的适配器不知道其项目。