我有一系列的对象。我想在列表视图中显示其元素

时间:2017-01-08 08:55:35

标签: android android-layout listview

我有一系列包含itemname和price的对象购物车。数组来自sqlite数据库正确。我想在列表视图中显示它。但在列表视图中,它只显示包名称而不是项目名称和价格。我认为问题在于这一行“ListAdapter buckysAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,carts);”Main2Activity.java

以下是我的代码,请指导我。这对我来说非常重要

Main2Activity.java

public class Main2Activity extends Activity {
MyDBHandler dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    Intent intent = getIntent();
    final int position = intent.getIntExtra("history",1);
    Log.i("positin","position = "+String.valueOf(position));
    dbHandler = new MyDBHandler(this,null,null,6);
    Cart[] carts = dbHandler.databaseToArray(position);
    for (Cart c : carts)
    Log.i("jhnbvb1","item1 = "+c.getItemname()+" price1 = "+c.getPrice());
    ListAdapter buckysAdapter = new ArrayAdapter<Cart>(this, android.R.layout.simple_list_item_1, carts);
    ListView buckysListView = (ListView) findViewById(R.id.buckysListView2);
    buckysListView.setAdapter(buckysAdapter);
}}

CustomAdapter2.java

public class CustomAdapter2 extends ArrayAdapter<Cart> {
public CustomAdapter2(Context context, Cart[] foods) {
    super(context, R.layout.custom_row2 ,foods);
}

@Override
public Cart getItem(int position) {
    return super.getItem(position);
}

@Override
public int getPosition(Cart item) {
    return super.getPosition(item);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater buckysInflater = LayoutInflater.from(getContext());
    View customView = buckysInflater.inflate(R.layout.custom_row2, parent, false);

    Cart singleFoodItem = getItem(position);
    String name = singleFoodItem.getItemname();
    String price = singleFoodItem.getPrice();
    Log.i("rtyiykj","item2 = "+name+" price2 = "+price);

    TextView buckysText = (TextView) customView.findViewById(R.id.buckysText2);
    TextView buckysText1 = (TextView) customView.findViewById(R.id.buckysText3);

    buckysText.setText(name);
    buckysText1.setText(price);
    return customView;
}}

custom_row2.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">

<TextView
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/buckysText2"
    android:layout_margin="5dp" />
<TextView
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/buckysText3"
    android:layout_margin="5dp" />

activity_main2.xml

<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Main2Activity">

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

cart.java

public class Cart {
private  String itemname,price;
public Cart(String itemname,String price) {
    this.setItemname(itemname);
    this.setPrice(price);
}
public Cart() {}
public void setItemname(String itemname) {
    this.itemname = itemname;
}
public void setPrice(String price) {
    this.price = price;
}
public String getItemname() {
    return itemname;
}
public String getPrice() {
    return price;
}}

1 个答案:

答案 0 :(得分:1)

您使用的是标准ArrayAdapter,而不是您创建的标准CustomAdapter2<Cart> buckysAdapter = new CustomAdapter2<Cart>(this, carts); 。它应该是

public class CustomAdapter2 extends ArrayAdapter<Cart> {
    private final List<Object> foods;

    public CustomAdapter2(Context context, List<Cart> foods) {
        super(context, 0);
        this.foods = foods;
    }

    @Override
    public int getCount() {
        return foods.size();
    }

    @Override
    public Cart getItem(int position) {
        return foods.get(position);
    }

    //your getView

}

应该修改数组适配器本身:

<ListView>
  <ListView.ItemTemplate>
    <DataTemplate>      
        <ViewCell>
            <RelativeLayout>
                <RelativeLayout IsVisible="{Binding Type, Converter={StaticResource converter1}}"></RelativeLayout>
                <RelativeLayout IsVisible="{Binding Type, Converter={StaticResource converter2}}"></RelativeLayout>
            </RelativeLayout>
        </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>