gpt4 book ai didi

android - 我有一系列的对象。我想在 ListView 中显示它的元素

转载 作者:行者123 更新时间:2023-11-30 00:47:39 25 4
gpt4 key购买 nike

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

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

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>

购物车.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;
}}

最佳答案

您使用标准的 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

}

关于android - 我有一系列的对象。我想在 ListView 中显示它的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41531015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com