I have linked properly the List-Adapter
and the other list-item
components.
It didn't getting the android:id
of activity_main_cake_items.xml
and giving this error.
Cannot resolve symbol 'listviewCakes'
This is my files :
MainCakeItems.java
package com.example.dessertshop;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import com.example.dessertshop.databinding.ActivityMainBinding;
import java.util.ArrayList;
public class MainCakeItems extends AppCompatActivity {
ActivityMainBinding binding;
ListAdapterCakes listAdapterCakes;
ArrayList<ListDataCakes> dataCakesArrayList = new ArrayList<>();
ListDataCakes listDataCakes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
// setContentView(R.layout.activity_main_cake_items);
setContentView(binding.getRoot());
int[] imageCakeList = {R.drawable.vanilla_cake, R.drawable.chocolate_cake, R.drawable.strawberry_cake};
int[] idCakeList = {R.string.vanillaCakeId, R.string.chocolateCakeId, R.string.strawberryCakeId};
int[] titleCakeList = {R.string.vanillaCakeTitle, R.string.chocolateCakeTitle, R.string.strawberryCakeTitle};
int[] detailsCakeList = {R.string.vanillaCakeDetails, R.string.chocolateCakeDetails, R.string.strawberryCakeDetails};
String[] titleList = {"Vanilla Cake", "Chocolate Cake", "Strawberry Cake"};
String[] idList = {"01", "02", "03"};
for (int i = 0; i < imageCakeList.length; i++) {
listDataCakes = new ListDataCakes(titleList[i],idList[i], imageCakeList[i],idCakeList[i], titleCakeList[i], detailsCakeList[i]);
dataCakesArrayList.add(listDataCakes);
}
listAdapterCakes = new ListAdapterCakes(MainCakeItems.this,dataCakesArrayList);
binding.listviewCakes.setAdapter(listAdapterCakes); // ERROR
binding.listviewCakes.setClickable(true); // ERROR
binding.listviewCakes.setOnItemClickListener(new AdapterView.OnItemClickListener() { // ERROR
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){
Intent intent = new Intent(MainCakeItems.this, DetailsCakes.class);
intent.putExtra("image", imageCakeList[i]);
intent.putExtra("id", idCakeList[i]);
intent.putExtra("title", titleCakeList[i]);
intent.putExtra("details", detailsCakeList[i]);
intent.putExtra("title", titleList[i]);
intent.putExtra("id", idList[i]);
startActivity(intent);
}
});
}
}
activity_main_cake_items.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
android:background="@drawable/background_image"
tools:context=".MainCakeItems">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listviewCakes" // Here it should get the value of the variable from
android:scrollbars="vertical"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="12dp"
tools:listitem="@layout/list_item_cakes"
android:divider="@android:color/transparent"
android:dividerHeight="10.0sp">
</ListView>
</androidx.constraintlayout.widget.ConstraintLayout>
ListAdapterCakes.java
package com.example.dessertshop;
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 java.util.ArrayList;
public class ListAdapterCakes extends ArrayAdapter<ListDataCakes> {
public ListAdapterCakes(@NonNull Context context, ArrayList<ListDataCakes> dataCakesArrayList) {
super(context, R.layout.list_item_cakes, dataCakesArrayList);
}
@NonNull
@Override
public View getView(int position, @Nullable View view, @NonNull ViewGroup parent) {
ListDataCakes listDataCakes = getItem(position);
if (view == null) {
view = LayoutInflater.from(getContext()).inflate(R.layout.list_item_cakes, parent, false);
}
ImageView listImageCakes = view.findViewById(R.id.listImageCakes);
TextView listTitleCakes = view.findViewById(R.id.listTitleCakes);
// TextView listDetails = view.findViewById(R.id.listCakeDetails);
TextView listCakesId = view.findViewById(R.id.listCakesId);
listImageCakes.setImageResource(listDataCakes.image);
listTitleCakes.setText(listDataCakes.title);
// listDetails.setText(listDataCakes.details);
listCakesId.setText(listDataCakes.id);
return view;
}
}
Since you named the layout file activity_main_cake_items.xml
then your binding name will be ActivityMainCakeItemsBinding
instead of ActivityMainBinding
.
So:
public class MainCakeItems extends AppCompatActivity {
ActivityMainCakeItemsBinding binding;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainCakeItemsBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
...
}