I'm trying to create a list view with the data from the Firebase real time database.For some reason I am keep getting a red line under this code.Hope you can let me know what is my mistake here. Thank you :). I have attached my layout file and also my app gradle codes.
(this,String.class,android.R.layout.simple_list_item_1,databaseReference)
Below here is the complete code
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl("https://vsem-inventory.firebaseio.com/");
FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(this,String.class,android.R.layout.simple_list_item_1,databaseReference) {
#Override
protected void populateView(#NonNull View v, #NonNull String model, int position) {
TextView textView = v.findViewById(android.R.id.text1);
}
};
mListView.setAdapter(firebaseListAdapter);
content_main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainMenu">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp" />
</RelativeLayout>
App gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.user.vseminventory"
minSdkVersion 19
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.firebase:firebase-auth:16.0.5'
implementation 'com.google.firebase:firebase-database:16.0.5'
implementation 'com.google.firebase:firebase-storage:16.0.5'
implementation 'com.android.support:design:28.0.0'
implementation 'com.firebase:firebase-client-android:2.3.1'
implementation 'com.firebaseui:firebase-ui-database:4.3.0'
}
apply plugin: 'com.google.gms.google-services'
com.google.gms.googleservices.GoogleServicesPlugin.config.disableVersionCheck = true
It seems you are using the new Firebaseui (version 3.0+), the constructor of the FirebaseListAdapter was changed.
First, you need to configure the adapter by using the class FirebaseListOptions, then you can create an instance of FirebaseListAdapter.
Check this code from the docs:
FirebaseListOptions<Chat> options = new FirebaseListOptions.Builder<Chat>()
.setQuery(query, Chat.class)
.build();
FirebaseListAdapter<Chat> adapter = new FirebaseListAdapter<Chat>(options) {
#Override
protected void populateView(View v, Chat model, int position) {
// Bind the Chat to the view
// ...
}
};
As you can see the FirebaseListAdapter takes a parameter of type FirebaseListOptions
More info here:
https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md#using-firebaseui-to-populate-a-listview
You can see the source code here:
https://github.com/firebase/FirebaseUI-Android/blob/master/database/src/main/java/com/firebase/ui/database/FirebaseListAdapter.java
Related
I want to DataBindingUtil class in java but I cannot import this. It seems to have no DataBindingUtil class even android.databinding.DataBindingUtil in my project. I have tried a reinstall but it not solve my problem. This is my code:
import android.databinding.DataBindingUtil;//cannot import
public class DeviceFragment extends Fragment{
private DeviceViewModel deviceViewModel;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
//cannot use it
DeviceFragmentBinding binding = DataBindingUtil.setContentView(this, R.layout.device_fragment);
And my gradle file (I have enabled viewBinding):
apply plugin: 'com.android.application'
android {
viewBinding {
enabled = true
}
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.aqiapp"
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
//custom
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.navigation:navigation-fragment:2.2.2'
implementation 'androidx.navigation:navigation-ui:2.2.2'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
//custom
implementation 'com.squareup.retrofit2:retrofit:2.8.2'
implementation 'com.squareup.retrofit2:converter-gson:2.8.2'
implementation 'com.google.dagger:dagger:2.27'
annotationProcessor 'com.google.dagger:dagger-compiler:2.27'
implementation 'com.squareup.okhttp3:logging-interceptor:3.8.0'
}
You should enable dataBinding instead of viewBinding try this
android {
...
dataBinding {
enabled = true
}
}
I faced same issue when upgarded to SDK 31, and the reason was databinding is no more under android package but androidx
It worked after
import android.databinding.DataBindingUtil;
was changed to
import androidx.databinding.DataBindingUtil;
Hello I am facing getvibrantcolor(int) palette cannot be applied to () error after migrating code to Androidx before migrating it was working good. please help me to resolve this issue.
following is my code where i am initializing and using Palette with Bitmap -
Bitmap photo = setupPhoto(getIntent().getIntExtra("photo", R.drawable.xyz));
colorize(photo);
private void colorize(Bitmap photo) {
Palette palette = Palette.generate(photo);
applyPalette(palette);
}
private void applyPalette(Palette palette) {
getWindow().setBackgroundDrawable(new ColorDrawable(palette.getDarkMutedColor().getRgb()));
TextView titleView = findViewById(R.id.title);
titleView.setTextColor(palette.getVibrantColor().getRgb());
TextView descriptionView = findViewById(R.id.description);
descriptionView.setTextColor(palette.getLightVibrantColor().getRgb());
colorRipple(R.id.info, palette.getDarkMutedColor().getRgb(),
palette.getDarkVibrantColor().getRgb());
colorRipple(R.id.star, palette.getMutedColor().getRgb(),
palette.getVibrantColor().getRgb());
View infoView = findViewById(R.id.information_container);
infoView.setBackgroundColor(palette.getLightMutedColor().getRgb());
AnimatedPathView star = findViewById(R.id.star_container);
star.setFillColor(palette.getVibrantColor().getRgb());
star.setStrokeColor(palette.getLightVibrantColor().getRgb());
}
Following is error image -
following is app build.gridle
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.xxx.xxx.info"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.gms:play-services-location:17.0.0'
implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'com.google.android.gms:play-services-identity:17.0.0'
implementation 'androidx.palette:palette:1.0.0'
}
As per the documentation:
public int getVibrantColor (int defaultColor)
So you need to pass in a default color that should be returned if no vibrant color is available:
// Use whatever default color you want
int defaultColor = getResources().getColor(android.R.color.black);
titleView.setTextColor(palette.getVibrantColor(defaultColor));
I followed vogella article: http://www.vogella.com/tutorials/Dagger/article.html
I did everything step by step as they suggested. My build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "test.daggertest"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:28.0.0'
implementation 'com.google.dagger:dagger:2.15'
implementation 'com.google.dagger:dagger-android:2.15'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.15'
annotationProcessor 'com.google.dagger:dagger-compiler:2.15'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
MyApplication class:
public class MyApplication extends Application implements HasActivityInjector
{
#Inject
DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;
#Override
public void onCreate()
{
super.onCreate();
DaggerMyApplicationComponent.create().inject(this);
}
#Override
public DispatchingAndroidInjector<Activity> activityInjector()
{
return dispatchingAndroidInjector;
}
}
Then I did Rebuild project, and still it throws error DaggerMyApplicationComponent is unresolved.
Am I missing something or their article is outdated? Any ideas?
I have looked some answer but related to firebase and live data not compatible or similar. My simple test application have nothing to do with firebase.
This is my gradle file in module level:
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "room.exercise.com.myapplication"
minSdkVersion 19
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:recyclerview-v7:27.1.1'
//Design for room persistance storage
implementation 'android.arch.persistence.room:runtime:1.0.0'
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0'
/////
//Design for live data
implementation "android.arch.lifecycle:extensions:1.0.0"
annotationProcessor "android.arch.lifecycle:compiler:1.0.0"
//
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
This application just a simple test of Room and Live data which I load the data from database to recyclerView.. I know the Room database is working coz this was my first test.
The gradle file was sync without problem. The error occurred when I compile and run the application.
Program type already present: android.arch.lifecycle.LiveData$LifecycleBoundObserver
Message{kind=ERROR, text=Program type already present: android.arch.lifecycle.LiveData$LifecycleBoundObserver, sources=[Unknown source file], tool name=Optional.of(D8)}
Code:
public class RoomActivity extends AppCompatActivity implements View.OnLongClickListener {
private StudentListViewModel viewModel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_room);
//RoomDatabase database = RoomDatabase.getDatabase(RoomActivity.this);
//RoomDataInitializer.AddSampleDataAsync(database);
//List<StudentEntity> students = database.studentDAO().getAllStudents();
//Log.w("Room Activity", "Count = " + students.size());
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
final RecyclerViewAdapter adapter = new RecyclerViewAdapter(new ArrayList<StudentEntity>(), this);
recyclerView.setAdapter(adapter);
viewModel = ViewModelProviders.of(this).get(StudentListViewModel.class);
viewModel.getStudentList().observe(RoomActivity.this, new Observer<List<StudentEntity>>()
{
#Override
public void onChanged(#Nullable List<StudentEntity> studentEntities) {
adapter.addItems(studentEntities);
}
});
}
#Override
public boolean onLongClick(View v) {
return false;
}
}
Just try this:
implementation "android.arch.lifecycle:extensions:1.1.1"
annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
implementation 'android.arch.persistence.room:runtime:1.1.1'
annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'
instead of your versions.
I don't have explainable explanation for this solution, yet, I'll try.
This type of error occurs when one class is defined/brought in by 2 or more dependencies. (In your case it is LifecycleBoundObserver).
And this type of errors occur runtime only. They don't happen during gradle build/sync.
If this type of errors are occurring in firebase/android dependencies, mostly they maybe resolved by upgrading the version. (Or there might be an issue registered in issue tracker for the same.). In other cases, you might have to exclude modules/groups from dependencies bringing in redundant classes.
I have a layout file called activity_suggestions. I am using databinding in it. Hence the file ActivitySuggestionsBinding got generated. The project compiles successfully. But when I try to run the project, I get this error
e: error: cannot access ActivitySuggestionsBinding
I am using android studio 3.1.2 with kotlin version 1.4.1. Any help will be appreciated
Edit
Pasting my module level build.gradle and app level build.gradle
Module Build.gradle
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android {
dataBinding {
enabled = true
}
..
}
dependencies{
..
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "com.google.dagger:dagger:$rootProject.daggerVersion"
implementation "com.google.dagger:dagger-android:$rootProject.daggerVersion"
implementation "com.google.dagger:dagger-android-support:$rootProject.daggerVersion"
kapt "com.google.dagger:dagger-android-processor:$rootProject.daggerVersion"
kapt "com.google.dagger:dagger-compiler:$rootProject.daggerVersion"
provided 'javax.annotation:jsr250-api:1.0'
implementation "android.arch.lifecycle:runtime:$rootProject.archVersion"
implementation "android.arch.lifecycle:extensions:$rootProject.archVersion"
annotationProcessor "android.arch.lifecycle:compiler:$rootProject.archVersion"
kapt "com.android.databinding:compiler:3.1.2"
..
}
App build.gradle
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android{
dataBinding{
enabled = true
}
..
}
dependencies{
compile project(':module')
kapt "com.google.dagger:dagger-android-processor:$rootProject.daggerVersion"
kapt "com.google.dagger:dagger-compiler:$rootProject.daggerVersion"
kapt "com.android.databinding:compiler:3.1.2"
..
}
This is the activity where I am accessing ActivitySuggestionsBinding. This compiles without any error.
class SuggestionsActivityScreen : BaseActivity() {
var binding : ActivitySuggestionsBinding? = null
override fun onCreate(savedInstanceState: Bundle?) {
AndroidInjection.inject(this)
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this,R.layout.activity_suggestions)
binding?.model = SuggestionActivityViewModel()
}
}
On compiling the base module (app), this is the error I get
error: cannot access ActivitySuggestionsBinding
class file for com.dom.comp.databinding.ActivitySuggestionsBinding not found
Consult the following stack trace for details.
com.sun.tools.javac.code.Symbol$CompletionFailure: class file for com.dom.comp.databinding.ActivitySuggestionsBinding not found
This is my activity_suggestions.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:card_view="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="model"
type="com.dom.domp.SuggestionActivityViewModel"/>
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:focusableInTouchMode="true"
android:padding="#dimen/step1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="#{model.namedString}"/>
</RelativeLayout>
</layout>
And I have tried clean, invalidate cache. These don't solve the problem.
In this demo it is work and try this way..
add only databinding enable code into app level gradle file like below code..
dataBinding {
enabled = true
}
below code is my app level gradle file ..
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.example.adruser.rafdemo"
minSdkVersion 23
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dataBinding {
enabled = true
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:design:27.1.1'
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:cardview-v7:27.1.1'
implementation 'com.google.android.gms:play-services-maps:11.6.0'
implementation 'com.google.android.gms:play-services-location:11.6.0'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.intuit.sdp:sdp-android:1.0.4'
implementation 'com.github.bumptech.glide:glide:4.7.1'
}
repositories {
mavenCentral()
}
then after in project level gradle.properties file add below line..
android.databinding.enableV2=true
make user_layout.xml file like below code..
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="user"
type="com.example.adruser.rafdemo.model.User"/>
</data>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text='#{user.name}'/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:id="#+id/ulTvName"
android:text="#{user.dob}"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:id="#+id/ulTvAge"
android:text="#{Integer.toString(user.age)}"
app:layout_constraintTop_toBottomOf="#+id/ulTvName"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:id="#+id/ulTvDob"
android:text="#{user.dob ?? user.expDob}"
app:layout_constraintTop_toBottomOf="#+id/ulTvAge"
/>
</android.support.constraint.ConstraintLayout>
</layout>
then after make User.java pojo class like below code.. you can make .kt class pojo also
public class User {
public String name,dob,expDob;
public int age;
public User(){}
public User(String name, String dob,String expDob, int age) {
this.name = name;
this.dob = dob;
this.age = age;
this.expDob=expDob;
}
public static String display(){
return "rajesh";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
then finnaly make DatabindingActivity.kt class for binding ..
class DatabindingActivity :AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
var binding:UserLayoutBinding=DataBindingUtil.setContentView(this,R.layout.user_layout)
val user = User("karan", null, "25/06/1994", 24)
binding.user=user
}
}
Add following in you app build.gradle
kapt "com.android.databinding:compiler:$android_plugin_version"
apply plugin: 'kotlin-kapt' // This one at top where plugin belong to
This will do the trick.
$android_plugin_version is version of com.android.tools.build:gradle in application build.gradle
Also, add this to your module build.gradle
android {
/// Existing Code
kapt {
generateStubs = true
}
}
I had similar issue.
I was getting error
error: cannot access RepeatPaymentFragmentBinding
class file for RepeatPaymentFragmentBinding not found
Consult the following stack trace for details.
com.sun.tools.javac.code.Symbol$CompletionFailure: class file for my.package.databinding.RepeatPaymentFragmentBinding not found
Spent a couple of days to figure out that the problem is not with GDB but with Dagger2. It turned out, Dagger doesn't support using generated classes in code it generates.
So, I had such class in my :library module:
class RepeatPaymentFragment: BaseFragment<RepeatPaymentFragmentBinding>() {
...
}
And the dagger component for it were located in :app module. So the two annotation processors interfere each other.
After I removed generic from my fragments it compiles fine.
So if you have such error, try to look if you try to inject into (or provide) class which is generated by another annotation proccessor, or inherits from such class, or have generic with such class.