1

i have array of objects carts containing itemname and price. Array is coming correctly from sqlite database. i want to display it in a listview. But in a listview it is only showing the package name not itemname and price. I think problem is in this line "ListAdapter buckysAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, carts); "of Main2Activity.java

Below is my code please guide me.It is very important for me

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 Answer 1

1

You use standard ArrayAdapter instead of that one created by you. It should be

CustomAdapter2<Cart> buckysAdapter = new CustomAdapter2<Cart>(this, carts);

And array adapter itself should be modified:

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

}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much Sir! it worked by slight editing in the code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.