错误:类FirebaseListAdapter <t>中的构造函数FirebaseListAdapter无法应用于给定类型;

时间:2018-04-01 15:00:24

标签: android firebase firebase-realtime-database

  

错误:类FirebaseListAdapter中的构造函数FirebaseListAdapter   不能适用于给定的类型;需要:   找到FirebaseListOptions:   MainActivity,Class,int,DatabaseReference原因:实际   和形式参数列表的长度不同,其中T是一个类型变量:   T扩展了类FirebaseListAdapter中声明的Object

我的代码:

adapter = new FirebaseListAdapter<ChatMessage>(this, ChatMessage.class,
                R.layout.message, 
FirebaseDatabase.getInstance().getReference()) 
{
            @Override
 protected void populateView(View v, ChatMessage model, int position) {

       // Get references to the views of message.xml
               TextView messageText = 
(TextView)v.findViewById(R.id.message_text);
                TextView messageUser = 
(TextView)v.findViewById(R.id.message_user);
                TextView messageTime = 
(TextView)v.findViewById(R.id.message_time);

            // Set their text
            messageText.setText(model.getMessageText());
            messageUser.setText(model.getMessageUser());

            // Format the date before showing it
            messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)",
                    model.getMessageTime()));
        }
    };

      listOfMessages.setAdapter(adapter);
}

我有最新的谷歌服务n存储库。 并导入所有陈述:

import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.database.FirebaseListAdapter;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.firebase.ui.database.FirebaseListOptions;
import com.google.android.gms.tasks.OnCompleteListener;

这是Build.gradle

    //add library
    implementation 'com.android.support:design:25.0.1'
    implementation 'com.firebaseui:firebase-ui:3.3.0'
    implementation 'com.google.firebase:firebase-core:12.0.1'
    implementation 'com.google.firebase:firebase-database:12.0.1'
    implementation 'com.google.firebase:firebase-auth:12.0.1'

    // FirebaseUI for Firebase Realtime Database
    implementation 'com.firebaseui:firebase-ui-database:3.3.0'

帮我解决firebaseadapter上的这个错误。

1 个答案:

答案 0 :(得分:0)

我有完整的解决方案。我现在有脸,我用一些库版本解决了它。我希望这项工作对您也有用。

首先是低于依赖项的用户

implementation 'com.firebase:firebase-client-android:2.5.1'
implementation 'com.firebaseui:firebase-ui:0.3.1' 

导入

import com.firebase.client.Firebase;
import com.firebase.ui.FirebaseListAdapter;

主要代码

public class MainActivity extends AppCompatActivity {

    Firebase rootRef;
    FirebaseListAdapter<ChatMessage> mListAdapter;
    EditText mEditText;
    ListView listView;
    String username;

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

        mEditText = (EditText) findViewById(R.id.message);
        listView = (ListView) findViewById(R.id.list);

        username = getUsername();

        Firebase.setAndroidContext(this);
        rootRef = new Firebase("<Reference url>");

        mListAdapter = new FirebaseListAdapter<com.example.dev_ramiz_1707.testfirebase.ChatMessage>(this, com.example.dev_ramiz_1707.testfirebase.ChatMessage.class,
                android.R.layout.two_line_list_item, rootRef) {
            @Override
            protected void populateView(View v, com.example.dev_ramiz_1707.testfirebase.ChatMessage model, int position) {
                ((TextView)v.findViewById(android.R.id.text1)).setText(model.getName());
                ((TextView)v.findViewById(android.R.id.text2)).setText(model.getText());
            }
        };
        listView.setAdapter(mListAdapter);

    }


    public void onClick(View v) {
        String text = mEditText.getText().toString();
        com.example.dev_ramiz_1707.testfirebase.ChatMessage message = new com.example.dev_ramiz_1707.testfirebase.ChatMessage(username, text);
        rootRef.push().setValue(message);
        mEditText.setText("");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mListAdapter.cleanup();
    }

    public String getUsername() {

        SharedPreferences sharedPref = MainActivity.this.getPreferences(Context.MODE_PRIVATE);
        String Username = sharedPref.getString("username","User-Name");
        return Username;

    }

}

不仅要复制此代码,还要了解其工作原理。

在您的代码中,有一个更新的库,而更新的库中有不包含FirebaseListAdapter的新类。这就是为什么您需要放置单个firebase库的原因。

如果您不知道如何创建参考网址,请遵循以下步骤:-Where can I find my Firebase reference URL in Firebase account?

希望这对您有所帮助。