Android: - 使用自定义ListView无法显示内容

时间:2014-08-06 05:51:08

标签: android

主要活动

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView list;
    /*Take these values from the database*/

    String[] itemName={"Puma","Reebok","Fila","Unite Colors of Benetton","Addidas"};
    String[] itemDescription={"Puma Green Colour Shoes","Reebok Red Colour Shoes","Fila Brown Colour Shoes","Boat Shoes","Addidas black Colour Shoes"};
    Integer[] price={1000,2000,3000,4000,5000};
    Integer[] Discount={10,20,30,40,50};
    Integer[] sizeOption1={6,6,6,6,6};
    Integer[] sizeOption2={7,7,7,7,7};
    Integer[] sizeOption3={8,8,8,8,8};

    Integer[] imageId={R.drawable.shoeimage,R.drawable.shoeimage2,R.drawable.shoeimage3,
            R.drawable.shoeimage4,R.drawable.shoeimage5};
    CustomListItems customListObject;
    customListObject = new CustomListItems(MainActivity.this,itemName,imageId,itemDescription,
            price,Discount,sizeOption1,sizeOption2,sizeOption3);
    list=(ListView)findViewById(R.id.listViewDisplay);
    list.setAdapter(customListObject);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

自定义列表类如下。

public CustomListItems(Activity context,
        String[] itemName, 
        Integer[] imageId, 
        String[] itemDescription, 
        Integer[] price, 
        Integer[] discountValue,
        Integer[] sizeOption1,
        Integer[] sizeOption2, 
        Integer[] sizeOption3)
{
    super(context, R.layout.list_items_display);
    this.context=context;
    this.itemDescription=itemDescription;
    this.imageId=imageId;
    this.itemName=itemName;
    this.price=price;
    this.discountValue=discountValue;
    this.sizeOption1=sizeOption1;
    this.sizeOption2=sizeOption2;
    this.sizeOption3=sizeOption3;



}
@Override
public View getView(int position,View view, ViewGroup parent)
{
    LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView= inflater.inflate(R.layout.list_items_display, null, true);
    ImageView imgView= (ImageView)rowView.findViewById(R.id.list_image);
    TextView txtItemDescription=(TextView) rowView.findViewById(R.id.txtItemDiscription);
    TextView txtItemTitle=(TextView)rowView.findViewById(R.id.txtItemtitle);
    Button btnSizeOption1=(Button)rowView.findViewById(R.id.btnSize1);
    Button btnSizeOption2=(Button)rowView.findViewById(R.id.btnSize2);
    Button btnSizeOption3=(Button)rowView.findViewById(R.id.btnSize3);
    TextView txtPrice=(TextView)rowView.findViewById(R.id.txtPrice);
    TextView txtDiscountValue=(TextView)rowView.findViewById(R.id.txtDiscountValue);
    txtItemTitle.setText("Puma");
    txtItemDescription.setText("Black Color Shoes");
    txtPrice.setText("5000");
    txtDiscountValue.setText("50");
    btnSizeOption1.setText("6");
    btnSizeOption2.setText("7");
    btnSizeOption3.setText("8");
    imgView.setImageResource(imageId[position]);
    return rowView;


}

}

但是没有显示任何内容。我是Android新手。

以下是XML文件

列出项目XML文件

<TextView
    android:id="@+id/txtItemtitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"      
    android:textColor="#040404"
    android:typeface="sans"
    android:textStyle="bold"/>

<TextView
    android:id="@+id/txtItemDiscription"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/txtDiscountValue"
    android:layout_below="@id/txtItemtitle"
    android:layout_marginTop="1dip"
    android:textColor="#343434" />

 <TextView
     android:id="@+id/txtSize"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignBaseline="@+id/btnSize3"
     android:layout_alignBottom="@+id/btnSize3"
     android:layout_toLeftOf="@+id/btnSize3"
     android:text="Size"
     android:textAppearance="?android:attr/textAppearanceMedium" />

 <TextView
     android:id="@+id/txtDiscountValue"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignBottom="@+id/txtPrice"
     android:layout_centerHorizontal="true"

     android:textAppearance="?android:attr/textAppearanceMedium" />

 <TextView
     android:id="@+id/txtPrice"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignLeft="@+id/txtSize"
     android:layout_below="@+id/btnSize1"
     android:layout_marginTop="36dp"

     android:textAppearance="?android:attr/textAppearanceMedium" />

 <ImageView
     android:id="@+id/list_image"
     android:layout_width="20dp"
     android:layout_height="20dp"
     android:layout_alignBottom="@+id/txtItemtitle"
     android:layout_toRightOf="@+id/txtItemtitle"
     android:contentDescription="SampleImage" />

 <Button
     android:id="@+id/btnSize3"
     style="?android:attr/buttonStyleSmall"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_below="@+id/txtItemDiscription"
     android:layout_marginRight="25dp"
     android:layout_marginTop="34dp"
     android:layout_toLeftOf="@+id/txtItemDiscription" />

 <Button
     android:id="@+id/btnSize1"
     style="?android:attr/buttonStyleSmall"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignBottom="@+id/txtSize"
     android:layout_toRightOf="@+id/txtItemDiscription" />

 <Button
     android:id="@+id/btnSize2"
     style="?android:attr/buttonStyleSmall"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignBaseline="@+id/btnSize1"
     android:layout_alignBottom="@+id/btnSize1"
     android:layout_marginLeft="18dp"
     android:layout_toRightOf="@+id/btnSize1" />

但是列表项视图中只有一个列表。代码如下所示

<ListView
    android:id="@+id/listViewDisplay"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    >
</ListView>

请你检查。

主要活动如下所示

<Button
    android:id="@+id/btnSignIn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="128dp"
    android:text="@string/btnSignIn" />

<TextView
    android:id="@+id/txtWelcomeMessage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="38dp"
    android:text="Welcome to shopping Cart"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/btnContinueShopping"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="37dp"
    android:text="@string/btnContinueShopping" />

<Button
    android:id="@+id/btnSignUp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/btnSignIn"
    android:layout_below="@+id/btnSignIn"
    android:layout_marginTop="30dp"
    android:text="@string/btnSignUp" />

我在这里使用ArrayAdapter,log cat在从getView()获取时显示了一些高度问题。

PFB the log cat。

08-16 04:32:47.433:E / AndroidRuntime(1991):致命异常:主要 08-16 04:32:47.433:E / AndroidRuntime(1991):流程:com.example.onlineshop,PID:1991 08-16 04:32:47.433:E / AndroidRuntime(1991):java.lang.NullPointerException 08-16 04:32:47.433:E / AndroidRuntime(1991):at com.example.onlineshop.CustomListItems.getView(CustomListItems.java:53) 08-16 04:32:47.433:E / AndroidRuntime(1991):在android.widget.AbsListView.obtainView(AbsListView.java:2263) 08-16 04:32:47.433:E / AndroidRuntime(1991):在android.widget.ListView.measureHeightOfChildren(ListView.java:1263) 08-16 04:32:47.433:E / AndroidRuntime(1991):在android.widget.ListView.onMeasure(ListView.java:1175) 08-16 04:32:47.433:E / AndroidRuntime(1991):at android.view.View.measure(View.java:16458) 08-16 04:32:47.433:E / AndroidRuntime(1991):在android.widget.RelativeLayout.measureChild(RelativeLayout.java:689) 08-16 04:32:47.433:E / AndroidRuntime(1991):在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473) 08-16 04:32:47.433:E / AndroidRuntime(1991):at android.view.View.measure(View.java:16458) 08-16 04:32:47.433:E / AndroidRuntime(1991):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 08-16 04:32:47.433:E / AndroidRuntime(1991):在android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 08-16 04:32:47.433:E / AndroidRuntime(1991):at android.view.View.measure(View.java:16458) 08-16 04:32:47.433:E / AndroidRuntime(1991):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 08-16 04:32:47.433:E / AndroidRuntime(1991):at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327) 08-16 04:32:47.433:E / AndroidRuntime(1991):at android.view.View.measure(View.java:16458) 08-16 04:32:47.433:E / AndroidRuntime(1991):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 08-16 04:32:47.433:E / AndroidRuntime(1991):在android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 08-16 04:32:47.433:E / AndroidRuntime(1991):at com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2289) 08-16 04:32:47.433:E / AndroidRuntime(1991):at android.view.View.measure(View.java:16458) 08-16 04:32:47.433:E / AndroidRuntime(1991):在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1914) 08-16 04:32:47.433:E / AndroidRuntime(1991):在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1111) 08-16 04:32:47.433:E / AndroidRuntime(1991):在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1293) 08-16 04:32:47.433:E / AndroidRuntime(1991):在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998) 08-16 04:32:47.433:E / AndroidRuntime(1991):在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5582) 08-16 04:32:47.433:E / AndroidRuntime(1991):在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:749) 08-16 04:32:47.433:E / AndroidRuntime(1991):在android.view.Choreographer.doCallbacks(Choreographer.java:562) 08-16 04:32:47.433:E / AndroidRuntime(1991):在android.view.Choreographer.doFrame(Choreographer.java:532) 08-16 04:32:47.433:E / AndroidRuntime(1991):在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:735) 08-16 04:32:47.433:E / AndroidRuntime(1991):在android.os.Handler.handleCallback(Handler.java:733) 08-16 04:32:47.433:E / AndroidRuntime(1991):在android.os.Handler.dispatchMessage(Handler.java:95) 08-16 04:32:47.433:E / AndroidRuntime(1991):在android.os.Looper.loop(Looper.java:137) 08-16 04:32:47.433:E / AndroidRuntime(1991):在android.app.ActivityThread.main(ActivityThread.java:4998) 08-16 04:32:47.433:E / AndroidRuntime(1991):at java.lang.reflect.Method.invokeNative(Native Method) 08-16 04:32:47.433:E / AndroidRuntime(1991):at java.lang.reflect.Method.invoke(Method.java:515)

如果有人可以在这里帮忙的话。

3 个答案:

答案 0 :(得分:0)

您的自定义适配器实际上没有任何项目。您没有覆盖getCount()并且没有将任何对象传递给super构造函数(假设超类是例如ArrayAdapter),因此超类实现无法报告适配器中正确数量的项目。

答案 1 :(得分:0)

你扩展哪个适配器?

将你的getView更改为:

@Override
public View getView(int position,View view, ViewGroup parent)
{
    LayoutInflater inflater = getLayoutInflater();
            View rowView= inflater.inflate(R.layout.list_items_display, parent, false);
    ImageView imgView= (ImageView)rowView.findViewById(R.id.list_image);
    TextView txtItemDescription=(TextView)  
    rowView.findViewById(R.id.txtItemDiscription);
    TextView txtItemTitle=(TextView)rowView.findViewById(R.id.txtItemtitle);
    Button btnSizeOption1=(Button)rowView.findViewById(R.id.btnSize1);
    Button btnSizeOption2=(Button)rowView.findViewById(R.id.btnSize2);
    Button btnSizeOption3=(Button)rowView.findViewById(R.id.btnSize3);
    TextView txtPrice=(TextView)rowView.findViewById(R.id.txtPrice);
    TextView txtDiscountValue=(TextView)rowView.findViewById(R.id.txtDiscountValue);
    txtItemTitle.setText("Puma");
    txtItemDescription.setText("Black Color Shoes");
    txtPrice.setText("5000");
    txtDiscountValue.setText("50");
    btnSizeOption1.setText("6");
    btnSizeOption2.setText("7");
    btnSizeOption3.setText("8");
    imgView.setImageResource(imageId[position]);
   return rowView;

}
 最好使用viewHolder以获得更好的性能 这项工作对我来说:

public class Main extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView list;
    /* Take these values from the database */

    String[] itemName = { "Puma", "Reebok", "Fila",
            "Unite Colors of Benetton", "Addidas" };
    String[] itemDescription = { "Puma Green Colour Shoes",
            "Reebok Red Colour Shoes", "Fila Brown Colour Shoes",
            "Boat Shoes", "Addidas black Colour Shoes" };
    Integer[] price = { 1000, 2000, 3000, 4000, 5000 };
    Integer[] Discount = { 10, 20, 30, 40, 50 };
    Integer[] sizeOption1 = { 6, 6, 6, 6, 6 };
    Integer[] sizeOption2 = { 7, 7, 7, 7, 7 };
    Integer[] sizeOption3 = { 8, 8, 8, 8, 8 };

    Integer[] imageId = { R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher };
    final CustomListItems customListObject;
    customListObject = new CustomListItems(Main.this, itemName, imageId,
            itemDescription, price, Discount, sizeOption1, sizeOption2,
            sizeOption3);
    list = (ListView) findViewById(R.id.listViewDisplay);
    list.setAdapter(customListObject);


    }
}  

customAdapter.class

public class CustomListItems extends ArrayAdapter<String> {
private Activity context;
private String[] itemDescription;
private Integer[] imageId;
private String[] itemName;
private Integer[] price;
private Integer[] discountValue;
private Integer[] sizeOption1;
private Integer[] sizeOption2;
private Integer[] sizeOption3;

public CustomListItems(Activity context, String[] itemName,
        Integer[] imageId, String[] itemDescription, Integer[] price,
        Integer[] discountValue, Integer[] sizeOption1,
        Integer[] sizeOption2, Integer[] sizeOption3) {
    super(context, R.layout.list_items_display);
    this.context = context;
    this.itemDescription = itemDescription;
    this.imageId = imageId;
    this.itemName = itemName;
    this.price = price;
    this.discountValue = discountValue;
    this.sizeOption1 = sizeOption1;
    this.sizeOption2 = sizeOption2;
    this.sizeOption3 = sizeOption3;

}


@Override
public int getCount() {
    // TODO Auto-generated method stub
    return discountValue.length;
}

@Override
public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater
            .inflate(R.layout.list_items_display, null, true);
    ImageView imgView = (ImageView) rowView.findViewById(R.id.list_image);
    TextView txtItemDescription = (TextView) rowView
            .findViewById(R.id.txtItemDiscription);
    TextView txtItemTitle = (TextView) rowView
            .findViewById(R.id.txtItemtitle);
    Button btnSizeOption1 = (Button) rowView.findViewById(R.id.btnSize1);
    Button btnSizeOption2 = (Button) rowView.findViewById(R.id.btnSize2);
    Button btnSizeOption3 = (Button) rowView.findViewById(R.id.btnSize3);
    TextView txtPrice = (TextView) rowView.findViewById(R.id.txtPrice);
    TextView txtDiscountValue = (TextView) rowView
            .findViewById(R.id.txtDiscountValue);
    txtItemTitle.setText("Puma");
    txtItemDescription.setText("Black Color Shoes");
    txtPrice.setText("5000");
    txtDiscountValue.setText("50");
    btnSizeOption1.setText("6");
    btnSizeOption2.setText("7");
    btnSizeOption3.setText("8");
    imgView.setImageResource(imageId[position]);
    return rowView;

   }

}  

和main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.mhp.customimagebtn"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/ic_launcher" >

<ListView
    android:id="@+id/listViewDisplay"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</ListView>
</RelativeLayout>  

和list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:descendantFocusability="blocksDescendants"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#fff" >

<TextView
android:id="@+id/txtItemtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"      
android:textColor="#040404"
android:typeface="sans"
android:textStyle="bold"/>

<TextView
android:id="@+id/txtItemDiscription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/txtDiscountValue"
android:layout_below="@id/txtItemtitle"
android:layout_marginTop="1dip"
android:textColor="#343434" />

<TextView
 android:id="@+id/txtSize"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignBaseline="@+id/btnSize3"
 android:layout_alignBottom="@+id/btnSize3"
 android:layout_toLeftOf="@+id/btnSize3"
 android:text="Size"
 android:textAppearance="?android:attr/textAppearanceMedium" />

 <TextView
 android:id="@+id/txtDiscountValue"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignBottom="@+id/txtPrice"
 android:layout_centerHorizontal="true"

 android:textAppearance="?android:attr/textAppearanceMedium" />

 <TextView
 android:id="@+id/txtPrice"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignLeft="@+id/txtSize"
 android:layout_below="@+id/btnSize1"
 android:layout_marginTop="36dp"

 android:textAppearance="?android:attr/textAppearanceMedium" />

 <ImageView
 android:id="@+id/list_image"
 android:layout_width="20dp"
 android:layout_height="20dp"
 android:layout_alignBottom="@+id/txtItemtitle"
 android:layout_toRightOf="@+id/txtItemtitle"
 android:contentDescription="SampleImage" />

 <Button
 android:id="@+id/btnSize3"
 style="?android:attr/buttonStyleSmall"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/txtItemDiscription"
 android:layout_marginRight="25dp"
 android:layout_marginTop="34dp"
 android:layout_toLeftOf="@+id/txtItemDiscription" />

 <Button
 android:id="@+id/btnSize1"
 style="?android:attr/buttonStyleSmall"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignBottom="@+id/txtSize"
 android:layout_toRightOf="@+id/txtItemDiscription" />

 <Button
 android:id="@+id/btnSize2"
 style="?android:attr/buttonStyleSmall"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignBaseline="@+id/btnSize1"
 android:layout_alignBottom="@+id/btnSize1"
 android:layout_marginLeft="18dp"
 android:layout_toRightOf="@+id/btnSize1" />
</RelativeLayout>  

结果

enter image description here

答案 2 :(得分:0)

使用 BaseAdapter 扩展您的CustomListItems类

public CustomListItems extends BaseAdapter { ... }