应用程式在将自订检视扩大至ListView时当机

时间:2019-03-25 19:24:28

标签: android listview android-inflate

在此处输入图像描述我想读取一个xml文件并使用arrayadapter在列表中显示其内容

问题在这一行:
convertView = LayoutInflater.from(context).inflate(R.layout.flower_list_item, parent,false);

当我调试应用程序时此错误显示:(((ListView)parent).mSelectedTop =找不到本地变量'parent' 和应用程序停止 任何帮助表示赞赏!

主要活动

 package com.example.jona.jsonxml;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    Button btn_xmlpp,btn_json,btn_xml_jdom;
    ListView listView;

    private List<Flower> flowers;
    private ArrayAdapter<Flower> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_xmlpp = (Button) findViewById(R.id.btn_xml_pp);
        btn_json  = (Button ) findViewById(R.id.btn_json);
        btn_xml_jdom = (Button) findViewById(R.id.btn_xml_jdom);
        listView = ( ListView) findViewById(R.id.listview);

        btn_xmlpp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flowers = new FlowerXmlPullParser(MainActivity.this).parseXml(); 
                Toast.makeText(MainActivity.this,"XmlpullParser : Returned = " + flowers.size() + " item " ,Toast.LENGTH_SHORT).show();
                refreshDisplay();


            }
        });

        btn_xml_jdom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flowers = new FlowerXmlJdomParser(MainActivity.this).parseXml();
                Toast.makeText(MainActivity.this, "XmljdomParser : Retuned " + flowers.size() + " items", Toast.LENGTH_SHORT).show();
                refreshDisplay();

            }
        });

    }

    public void refreshDisplay () {
          if (flowers == null) {
            flowers = new ArrayList<>();
        }
        adapter = new FlowerAdapter(this,flowers);
        listView.setAdapter(adapter);

    }
}

适配器:

package com.example.jona.jsonxml;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class FlowerAdapter extends ArrayAdapter<Flower> {
    private Context context;
    private List<Flower> flowerList;

    public FlowerAdapter(Context context, List<Flower> flowerList) {

        super(context, R.layout.flower_list_item, flowerList);
        this.context = context;
        this.flowerList = flowerList;

    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
         ViewHolder viewholder;
        if (convertView == null) { 

            convertView = LayoutInflater.from(context).inflate(R.layout.flower_list_item, parent,false);
            Log.i("here","no problem");
            viewholder = new ViewHolder(convertView);
            convertView.setTag(viewholder);
        } else {
            viewholder = (ViewHolder) convertView.getTag();
        }
        viewholder.fill(position);
        return convertView;
    }

    public class ViewHolder {
        ImageView flower_image;
        TextView flower_name, flower_category, flower_price;

        public ViewHolder(View view) {
            flower_image = (ImageView) view.findViewById(R.id.flower_image);
            flower_name = ( TextView) view.findViewById(R.id.flower_name);
            flower_price =( TextView) view.findViewById(R.id.flower_price);
            flower_category = (TextView) view.findViewById(R.id.flower_category);
        }


        public void fill(int positon) {
            Flower flower = flowerList.get(positon);
            flower_name.setText(flower.getName());
            flower_price.setText(flower.getPrice() + "$");
            flower_category.setText(flower.getCategory());
            // load image
        }

    }
}

activty_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".MainActivity">


    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="453dp"


        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="80dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </ListView>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_xml_pp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="XML pull parser" />

        <Button
            android:id="@+id/btn_xml_jdom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="xml jdom" />

        <Button
            android:id="@+id/btn_json"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="JSON" />

    </LinearLayout>

</android.support.constraint.ConstraintLayout>

自定义列表:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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:padding="4dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.AppCompatImageView

        android:id="@+id/flower_image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/aloe_vera" />

    <android.support.v7.widget.AppCompatTextView

        android:id="@+id/flower_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"

        android:text="Ameria "
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintLeft_toRightOf="@+id/flower_image"
        app:layout_constraintTop_toTopOf="parent" />
    <android.support.v7.widget.AppCompatTextView
        android:id="@+id/flower_category"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="@id/flower_name"
        app:layout_constraintTop_toBottomOf="@id/flower_name"
        android:text="category"
        android:textSize="18sp"
        android:textStyle="italic"
        android:layout_marginTop="5dp"
        />

    <android.support.v7.widget.AppCompatTextView
        android:id="@+id/flower_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="15.98 $"
        app:layout_constraintRight_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="@id/flower_image"
        android:textColor="@android:color/holo_red_dark"
        android:layout_marginBottom="10dp"
        />


</android.support.constraint.ConstraintLayout>

0 个答案:

没有答案