-2

i want to make a listView with listItem and adapter

my application keeps stopping (when run the app )how can i slove this problem

how can i solve this problem

This is my mainActivity page

and show me that there no errors

package com.example.listview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle; import android.widget.ListView;

import com.example.listview.Adapters.productAdapter; import com.example.listview.model.product;

import java.util.ArrayList; import java.util.List;

public class MainActivity extends AppCompatActivity {

List<product>productList;
ListView listView;
product produ;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

      listView=(ListView) findViewById(R.id.lisView);
      productList=new ArrayList<product>();


    String []names={"spoon","dish","couch","furniture"};
    String []prices={"8 $","27 $","893 $","1243 $"};

    int []images={R.drawable.spoons,R.drawable.pic1,R.drawable.pic2,R.drawable.furniture};


    for(int i=0;i< names.length;i++)
    {
        produ=new product(names[i],prices[i],images[i]);
        productList.add(produ);
    }
   productAdapter adapter=new productAdapter(getApplicationContext(),productList);
    listView.setAdapter(adapter);

}
}

** This is productAdapter Class page**

package com.example.listview.Adapters;

import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView;

import androidx.annotation.NonNull; import androidx.annotation.Nullable;

import com.example.listview.R; import com.example.listview.model.product;

import java.util.List;

public class productAdapter extends ArrayAdapter<product> { private  Context context ; private  List<product>productList;    
public productAdapter(Context context, List<product>products){
        super(context, R.layout.list_item,products);
        this.context=context;
        this.productList=products;
    }
@NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent ){ product productname=getItem(position);

if(convertView==null) { convertView= LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false); }
ImageView img=(ImageView)convertView.findViewById(R.id.imageview);
TextView name=(TextView) convertView.findViewById(R.id.productName);
TextView pri=(TextView) convertView.findViewById(R.id.price);
img.setImageResource(productname.getImage());
name.setText(productname.getName());
pri.setText(productname.getPrice());

return convertView;

}
} 

This is product Class

package com.example.listview.model;

import java.util.jar.Attributes;

public class product {
    String Name;
    String Price;
    int Image;


    public product(String name, String price, int image) {
        Name = name;
        Price = price;
        Image = image;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getPrice() {
        return Price;
    }

    public void setPrice(String price) {
        Price = price;
    }

    public int getImage() {
        return Image;
    }

    public void setImage(int image) {
        Image = image;
    }
}

MainActivity Layout

<LinearLayout 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" tools:context=".MainActivity" android:orientation="vertical">

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

ListItem xml file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:background="#FFDFDF" android:orientation="vertical"> <androidx.cardview.widget.CardView android:id="@+id/card" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:elevation="8dp" app:cardCornerRadius="8dp" \> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_gravity="center" >        
<TextView
            android:layout_marginLeft="120dp"
            android:id="@+id/productName"
            style="@style/TextAppearance.AppCompat.Body1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Product Name"
            android:textSize="8pt"
            android:layout_marginTop="12dp"
            android:layout_marginBottom="12dp"
            android:textStyle="bold"
            />
        <ImageView
            android:id="@+id/imageview"
            android:layout_width="match_parent"
            android:layout_height="210dp"
            android:src="@drawable/pic1" />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/price"
                android:text="Price: $"
                android:textSize="20sp"
                android:textStyle="bold"
                android:layout_marginLeft="40dp"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="16dp"
                android:textColor="#4CAF50"
                />
            <ImageView
                android:id="@+id/fav"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/baseline_favorite_24"
                android:layout_marginLeft="180dp"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="16dp"
                />

            <ImageView
                android:id="@+id/cart"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/baseline_shopping_cart_24"
                android:layout_marginLeft="14dp"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="16dp"
                />





        </LinearLayout>


    </LinearLayout>




</androidx.cardview.widget.CardView>
  • 1
    Please edit the question and describe your attempts to debug. Also, what do you mean by "application keeps stopping" ? Does it abort? Is there an error message? If there is, please edit the question to include an exact copy of the error message. Use copy-and-paste. Or, does it seem to freeze? If it freezes, does it use CPU time? Have you tried following code execution by running in a debugger? – Old Dog Programmer Aug 31 '23 at 22:31
  • 5
    You should look into logcat, in Android Studio you can filter logcat with package:mine is:crash, this should show callstack of your crashes. – marcinj Aug 31 '23 at 22:34
  • 1
    It's very difficult to debug a crash without a stack trace. See [Unfortunately MyApp has stopped. How can I solve this?](/q/23353173) for Android-specific advice, and [What is a stack trace, and how can I use it to debug my application errors?](/q/3988788) for advice on what to do once you have the stack trace. If you still need help, edit your question to include the **complete stack trace**, as well as **which line of your code** the stack trace points to. – Ryan M Sep 01 '23 at 01:40

0 Answers0