LIstView适配器不显示自定义适配器的内容

时间:2016-04-10 06:37:57

标签: android listview android-arrayadapter listadapter

我在ArrayList上创建了Data对象shopObjects。我仔细检查他们是否满满的。不是空的,并试图创建一个适配器。这是我第一次这样做,列表是空的。老实说,我不确定我做错了什么。

**CustomListAdapter.java**
    public class CustomListAdapter extends ArrayAdapter<shopClass> {
    ArrayList<shopClass> shopObjects;
    private final int resource;

    public CustomListAdapter(Context context, ArrayList<shopClass> PshopObjects, int resource) {
        super(context, R.layout.list_row, PshopObjects);
        shopObjects = PshopObjects;
        this.resource = resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // We need to get the best view (re-used if possible) and then
        // retrieve its corresponding ViewHolder, which optimizes lookup efficiency
        View view = getWorkingView(convertView);
        ViewHolder viewHolder = getViewHolder(view);
        shopClass shop = getItem(position);

        if (shopObjects == null) {
            System.out.println("it is null on CustomListarrya");
        }

        // Setting the title view is straightforward
        viewHolder.titleView.setText(shop.getName());
        viewHolder.subTitleView.setText(shop.getDescription());

//        TextView title = (TextView) alertView.findViewById(R.id.title);
//        TextView description = (TextView) alertView.findViewById(R.id.description);
//        LinearLayout bkgImage = (LinearLayout) alertView.findViewById(R.id.bkgImage);
//
//        // getting movie data for the row
//
//        // bkg image
////        bkgImage.setBackground();
//
//        // title
//        title.setText(shop.getName());
//
//        // description
//        description.setText(shop.getDescription());
        return view;
    }

    private View getWorkingView(final View convertView) {
        // The workingView is basically just the convertView re-used if possible
        // or inflated new if not possible
        View workingView = null;

        if(null == convertView) {
            final Context context = getContext();
            final LayoutInflater inflater = (LayoutInflater)context.getSystemService
                    (Context.LAYOUT_INFLATER_SERVICE);

            workingView = inflater.inflate(resource, null);
        } else {
            workingView = convertView;
        }

        return workingView;
    }

    private ViewHolder getViewHolder(final View workingView) {
        // The viewHolder allows us to avoid re-looking up view references
        // Since views are recycled, these references will never change
        final Object tag = workingView.getTag();
        ViewHolder viewHolder = null;


        if(null == tag || !(tag instanceof ViewHolder)) {
            viewHolder = new ViewHolder();

            viewHolder.titleView = (TextView) workingView.findViewById(R.id.title);
            viewHolder.subTitleView = (TextView) workingView.findViewById(R.id.description);
            //viewHolder.imageView = (ImageView) workingView.findViewById(R.id.news_entry_icon);

            workingView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) tag;
        }

        return viewHolder;
    }

    /**
     * ViewHolder allows us to avoid re-looking up view references
     * Since views are recycled, these references will never change
     */
    private static class ViewHolder {
        public TextView titleView;
        public TextView subTitleView;
        //public ImageView imageView;
    }

}

ShopActivity.java

public class ShopActivity extends AppCompatActivity {
    ListView shopList;
    CustomListAdapter adapter;
    private ArrayList<shopClass> shopObjects;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);

        shopList = (ListView) findViewById(R.id.list);
        ShopData start = new ShopData();

        shopObjects = start.getShopData();
        adapter = new CustomListAdapter(this, shopObjects, R.layout.list_row);
        for (int i = 0; i<2; i++){
            System.out.println(shopObjects.get(i).toString());
            adapter.add(shopObjects.get(i));
        }
        shopList.setAdapter(adapter);


    }

Activity_shop.xml

<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="com.csthack.beinnovative.destination_brooklyn.ShopActivity"
    tools:showIn="@layout/activity_shop">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="match_parent">

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_row_selector" />
    </LinearLayout>
</LinearLayout>

listrow.xml

<?xml version="1.0" encoding="utf-8"?>
<!--Style for the list-->

<android.support.percent.PercentRelativeLayout
    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" >
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/list_row_selector"
        app:layout_heightPercent="33%"
        app:layout_widthPercent="100%">

        <!-- Background Image -->
        <LinearLayout
            android:id="@+id/bkgImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/coffe"
            android:orientation="vertical"
            android:alpha="0.65"/>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="bottom|start"
            android:layout_alignParentBottom="true"
            android:paddingLeft="10dp">

            <!-- Shop Title -->
            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Test"
                android:textSize="@dimen/title"
                android:textStyle="bold"
                android:textColor="@color/genre"
                android:alpha="1.0"/>

            <!-- Description -->
            <TextView
                android:id="@+id/description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/title"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="10dp"
                android:text="blah blah blahg blah blah blah "
                android:textColor="@color/genre"
                android:textSize="@dimen/genre" />

        </LinearLayout>
    </RelativeLayout>
</android.support.percent.PercentRelativeLayout>

我已经查看了其他教程,但它们似乎都在使用相同的逻辑并尝试了3但仍然没有内容。

记录:

 04-10 03:12:44.384 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@b7bc795
04-10 03:12:44.388 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@cc190aa
04-10 03:12:44.391 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@2a697e4
04-10 03:12:44.393 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@18e544d
04-10 03:12:44.395 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@7d78b02
04-10 03:12:44.397 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@b7bc795
04-10 03:12:44.398 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@cc190aa
04-10 03:12:44.399 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@b7bc795
04-10 03:12:44.400 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@cc190aa
04-10 03:12:44.401 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@2a697e4
04-10 03:12:44.402 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@18e544d
04-10 03:12:44.403 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@7d78b02
04-10 03:12:44.404 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@b7bc795
04-10 03:12:44.404 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@cc190aa
04-10 03:12:44.431 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@b7bc795
04-10 03:12:44.432 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@cc190aa
04-10 03:12:44.433 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@2a697e4
04-10 03:12:44.434 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@18e544d
04-10 03:12:44.435 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@7d78b02
04-10 03:12:44.436 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@b7bc795
04-10 03:12:44.437 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@cc190aa
04-10 03:12:44.438 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@b7bc795
04-10 03:12:44.439 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@cc190aa
04-10 03:12:44.440 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@2a697e4
04-10 03:12:44.441 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@18e544d
04-10 03:12:44.441 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@7d78b02
04-10 03:12:44.442 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@b7bc795
04-10 03:12:44.443 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@cc190aa
04-10 03:12:44.445 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@b7bc795
04-10 03:12:44.448 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@cc190aa
04-10 03:12:44.451 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@2a697e4
04-10 03:12:44.454 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@18e544d
04-10 03:12:44.456 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@7d78b02
04-10 03:12:44.459 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@b7bc795
04-10 03:12:44.462 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@cc190aa
04-10 03:12:44.484 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@b7bc795
04-10 03:12:44.485 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@cc190aa
04-10 03:12:44.486 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@2a697e4
04-10 03:12:44.487 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@18e544d
04-10 03:12:44.488 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@7d78b02
04-10 03:12:44.489 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@b7bc795
04-10 03:12:44.490 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@cc190aa
04-10 03:12:44.491 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@b7bc795
04-10 03:12:44.492 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@cc190aa
04-10 03:12:44.492 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@2a697e4
04-10 03:12:44.493 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@18e544d
04-10 03:12:44.494 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@7d78b02
04-10 03:12:44.495 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@b7bc795
04-10 03:12:44.496 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@cc190aa
04-10 03:12:44.521 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@b7bc795
04-10 03:12:44.523 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@cc190aa
04-10 03:12:44.524 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@2a697e4
04-10 03:12:44.525 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@18e544d
04-10 03:12:44.526 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@7d78b02
04-10 03:12:44.527 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@b7bc795
04-10 03:12:44.528 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@cc190aa
04-10 03:12:44.529 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@b7bc795
04-10 03:12:44.530 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@cc190aa
04-10 03:12:44.531 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@2a697e4
04-10 03:12:44.531 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@18e544d
04-10 03:12:44.532 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@7d78b02
04-10 03:12:44.533 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@b7bc795
04-10 03:12:44.534 14556-14556/com.csthack.beinnovative.destination_brooklyn I/System.out: Item Position: com.csthack.beinnovative.destination_brooklyn.shopClass@cc190aa

2 个答案:

答案 0 :(得分:0)

也许问题是因为ListView的身高不正确。尝试将android:layout_heightwrap_content更改为match_parent

答案 1 :(得分:0)

万一有人遇到类似的问题,问题是<android.support.percent.PercentRelativeLayout架构在使用不同的架构后无法正常工作,没有问题。