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>