1

I have implemented these codes.

activity_quotes.xml

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <data>

        <variable
            name="quote"
            type="e.com.learningprojects.ImplementAndroidArchitecture.QuotesModel"/

    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ImplementAndroidArchitecture.QuotesActivity">

        <TextView
            android:id="@+id/quotes"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="137dp"
            android:layout_marginEnd="16dp"
            android:text="@{quote.quotes"
            android:textColor="@color/black"
            android:textSize="25sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/author"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="24dp"
            android:text="@{quote.author}"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/quotes" />

        <Button
            android:id="@+id/next_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="32dp"
            android:layout_marginEnd="32dp"
            android:text="@string/next"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/author" />

        <Button
            android:id="@+id/previous_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="32dp"
            android:text="@string/previous"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/next_btn" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

QuoteActivity.java

package e.com.learningprojects.ImplementAndroidArchitecture;

import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import e.com.learningprojects.R;
import e.com.learningprojects.databinding.ActivityQuotesBinding;

public class QuotesActivity extends AppCompatActivity {

    MainViewModel mainViewModel;
    TextView text, author;
    Button nextBtn, previousBtn;
    ActivityQuotesBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // setContentView(R.layout.activity_quotes);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_quotes);
        mainViewModel = new ViewModelProvider(this, new ViewModelFactory(this)).get(MainViewModel.class);

        text = findViewById(R.id.quotes);
        author = findViewById(R.id.author);
        nextBtn = findViewById(R.id.next_btn);
        previousBtn = findViewById(R.id.previous_btn);
        mainViewModel.fetchQuotes();
        // setQuotes(mainViewModel.fetchQuotes());
        nextBtn.setOnClickListener(v -> setQuotes(mainViewModel.nextQuotes()));
        previousBtn.setOnClickListener(v -> setQuotes(mainViewModel.previousQuotes()));

    }

    public void setQuotes(QuotesModel quotes) {
//        text.setText(quotes.quotes);
//        author.setText(quotes.author);

//       binding.quotes.setText(quotes.quotes);
//       binding.author.setText(quotes.author);
        
        binding.setQuote(quotes);

    }
}

QuoteModel.java

package e.com.learningprojects.ImplementAndroidArchitecture;

public class QuotesModel {
    public  String quotes, author;


    public QuotesModel(String quotes, String author) {
        this.quotes = quotes;
        this.author = author;
    }

    public String getQuotes() {
        return quotes;
    }

    public String getAuthor() {
        return author;
    }
}

I am implementing data-binding. I have implemented everything for data binding to work and initialized all views from layout but this is still giving this error: cannot find symbol class ImplementAndroidArchitecture

error

cannot find symbol class ImplementAndroidArchitecture

import e.com.learningprojects.ImplementAndroidArchitecture;
                             ^
  symbol:   class ImplementAndroidArchitecture
  location: package e.com.learningpro`enter code here`jects

package ImplementAndroidArchitecture does not exist

 protected ImplementAndroidArchitecture.QuotesModel mQuote;
                                 

2 Answers2

0

replace ActivityQuotesBinding binding; with QuotesBinding binding; then

binding=QuotesBinding.inflate(LayoutInflater);
setContentView(binding.root);
Saurabh Dhage
  • 1,478
  • 5
  • 17
  • 31
0

I can point out two issues with your code.

  1. You need to complete the assignment expression below with "}"

    android:text="@{quote.quotes"

  2. Change the package name "ImplementAndroidArchitecture" to lower case. Look here https://stackoverflow.com/a/12534322/5980764 for further explanation.

Maximus
  • 136
  • 1
  • 4