尝试使用自定义列表适配器时应用程序崩溃,无法找到错误

时间:2016-04-06 08:59:54

标签: android listview android-arrayadapter

我正在尝试创建我创建的简单对象列表 对象是ShopListItem,这是类的代码:

    public class ShopListItem {
       private String name;
       private double price;

       public ShopListItem(String name , double price)
       {
          this.name = name;
          this.price = price;
       }

       public double getPrice() {
          return price;
       }

       public String getName() {
          return name;
       }
   }

这是ArrayAdatpter类:

public class shopItemAdapter extends ArrayAdapter<ShopListItem> {

   public shopItemAdapter(Context context, ArrayList<ShopListItem> items){
     super(context, 0, items);
   }

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

     ShopListItem item = getItem(position);

     if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.shop_item_row, parent, false);
     }

     TextView name = (TextView) convertView.findViewById(R.id.itemName);
     TextView price = (TextView) convertView.findViewById(R.id.itemPrice);

     name.setText(item.getName());
     price.setText(String.valueOf(item.getPrice()));

     return convertView;
   }


}

这是我的活动,应该显示这个列表:

    public class check extends Activity {
       ListView items = (ListView)findViewById(R.id.items);

       @Override
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_q);
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);


          ShopListItem item1 = new ShopListItem("milk" , 5.9);
          ShopListItem item2 = new ShopListItem("bamba" , 4);

          ArrayList<ShopListItem> list = new ArrayList<ShopListItem>();
          list.add(item1);
          list.add(item2);
          shopItemAdapter adapter = new shopItemAdapter(this , list);
          items.setAdapter(adapter);
      }

  }

当用户点击按钮时,我会从其他活动中打开活动,这是点击方法:

public void openList(View view){

   Intent intent = new Intent(HomeActivity.this , check.class);
   this.startActivity(intent);
}

shop_item_row.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFF9F9F9"
    android:paddingRight="10dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/itemName"
        android:layout_weight="0.61"
        android:layout_margin="5dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/itemPrice"
        android:layout_margin="5dp" />
</LinearLayout>

和content_check.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="atoa.roomates.QA"
    tools:showIn="@layout/activity_q">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/items"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>
谢谢你!

3 个答案:

答案 0 :(得分:1)

您正在onCreate()之前访问视图ListView items = (ListView)findViewById(R.id.items);。所以在setContentView()之后在onCreate方法中移动该行。

答案 1 :(得分:0)

public class shopItemAdapter extends ArrayAdapter<ShopListItem> {
private Context mContext;
private List<ShopListItem> mListShop;

public shopItemAdapter(Context context, int resource, ArrayList<ShopListItem> items){
super(context, resource, items);
this.mContext= context;
this.mListShop = items;
}
 @Override
public int getCount() {
    return mListShop.size();
}

@Override
public ShopListItem getItem(int position) {
    return mListShop.get(position);
}


public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;  
if (view == null) {
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.shop_item_row, null);
        holder = new ViewHolder();
        holder.mTextPrice = (TextView) view.findViewById(R.id.itemPrice);
        holder.mTextName = (TextView) view.findViewById(R.id.itemName);

        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }
ShopListItem item = getItem(position);
holder.mTextName.setText(item.getName());
holder.mTextPrice.setText(item.getPrice());

return view;
}

public class ViewHolder {
    public TextView mTextPrice;
    public TextView mTextName;
}
}

在您的活动中

public class check extends Activity {
ListView items;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_q);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    items = (ListView)findViewById(R.id.items);


    ShopListItem item1 = new ShopListItem("milk" , 5.9);
    ShopListItem item2 = new ShopListItem("bamba" , 4);

    ArrayList<ShopListItem> list = new ArrayList<ShopListItem>();
    list.add(item1);
    list.add(item2);
    shopItemAdapter adapter = new shopItemAdapter(this , R.layout.shop_item_row, list);
    items.setAdapter(adapter);
    }
    }

试试!!!!

答案 2 :(得分:0)

在setContentView而不是top中初始化列表 喜欢以下

ListView items;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_demo);
        items = (ListView) findViewById(R.id.items);
改变后这项工作很好。