gpt4 book ai didi

java - 无法通过数据绑定(bind)在 Fragment 上使用 RecyclerView

转载 作者:行者123 更新时间:2023-11-29 19:18:13 24 4
gpt4 key购买 nike

现在我尝试使用数据绑定(bind)实现 RecyclerView。并且这个 RecyclerView 实例附加在 Fragment 中。

我有关于 this web page 的引用,此代码有效,但我的无效。我不知道为什么我有错误。此外,我的应用程序没有收到任何错误消息。它只是不起作用。

也许,这可能是一个错误信息,但我无法调试这个问题

E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.azurewebsites.irrationnelle.testandroidstudio, PID: 3828
android.content.res.Resources$NotFoundException: String resource ID #0xa
at android.content.res.Resources.getText(Resources.java:335)
at android.widget.TextView.setText(TextView.java:4555)
at net.azurewebsites.irrationnelle.testandroidstudio.databinding.MainRecyclerItemsBinding.executeBindings(MainRecyclerItemsBinding.java:113)
at android.databinding.ViewDataBinding.executeBindingsInternal(ViewDataBinding.java:379)
at android.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:351)
at android.databinding.ViewDataBinding$6.run(ViewDataBinding.java:178)
at android.databinding.ViewDataBinding$7.doFrame(ViewDataBinding.java:251)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:872)
at android.view.Choreographer.doCallbacks(Choreographer.java:686)
at android.view.Choreographer.doFrame(Choreographer.java:618)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

下面是我的代码。

我需要你的帮助,我花了大约 3 天的时间来解决这个问题......谢谢。

main_recycler_items.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>

<variable
name="item"
type="net.azurewebsites.irrationnelle.testandroidstudio.vo.MainItem"/>
</data>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/band_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
android:gravity="center_vertical"
android:padding="10dp"
android:text="@{item.bandName}" />

<TextView
android:id="@+id/music_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@{item.bandCount}"
android:textAlignment="center" />

<ImageView
android:id="@+id/scroll_button"
android:layout_width="3dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_weight="1"
android:src="@drawable/ic_keyboard_arrow_down_black_36dp" />

</LinearLayout>
</layout>

/vo/MainItem.java

package net.azurewebsites.irrationnelle.testandroidstudio.vo;

public class MainItem {
private int bandCount;
private String bandName;

public MainItem(int bandCount, String bandName) {
this.bandCount = bandCount;
this.bandName = bandName;
}

public int getBandCount() {
return bandCount;
}

public void setBandCount(int bandCount) {
this.bandCount = bandCount;
}

public String getBandName() {
return bandName;
}

public void setBandName(String bandName) {
this.bandName = bandName;
}
}

fragment _main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_recylcer_view"
android:scrollbars="vertical">

</android.support.v7.widget.RecyclerView>

</LinearLayout>
</layout>

MainViewHolder.java

package net.azurewebsites.irrationnelle.testandroidstudio;

import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import net.azurewebsites.irrationnelle.testandroidstudio.databinding.MainRecyclerItemsBinding;
import net.azurewebsites.irrationnelle.testandroidstudio.vo.MainItem;

public class MainViewHolder extends RecyclerView.ViewHolder {
private MainRecyclerItemsBinding binding;

public MainViewHolder(View itemView) {
super(itemView);
binding = DataBindingUtil.bind(itemView);
}

public void bind(MainItem item) {
binding.setItem(item);
}
}

MainRecyclerViewAdapter.java

package net.azurewebsites.irrationnelle.testandroidstudio;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import net.azurewebsites.irrationnelle.testandroidstudio.vo.MainItem;

import java.util.List;

public class MainRecyclerViewAdapter extends RecyclerView.Adapter<MainViewHolder> {
private List<MainItem> items;

public MainRecyclerViewAdapter(List<MainItem> items) {
this.items = items;
}

@Override
public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View statusContainer = inflater.inflate(R.layout.main_recycler_items, parent, false);
return new MainViewHolder(statusContainer);
}

@Override
public void onBindViewHolder(MainViewHolder holder, int position) {
MainItem status = items.get(position);
holder.bind(status);
}

@Override
public int getItemCount() {
return items.size();
}
}

主要 fragment .java

package net.azurewebsites.irrationnelle.testandroidstudio;

import android.app.Fragment;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import net.azurewebsites.irrationnelle.testandroidstudio.databinding.FragmentMainBinding;
import net.azurewebsites.irrationnelle.testandroidstudio.databinding.MainRecyclerItemsBinding;
import net.azurewebsites.irrationnelle.testandroidstudio.vo.MainItem;

import java.util.ArrayList;
import java.util.List;

public class MainFragments extends Fragment {
private FragmentMainBinding binding;

private RecyclerView recyclerView;
private MainRecyclerViewAdapter adapter;
private List<MainItem> items;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_main, container, false);
View view = binding.getRoot();

recyclerView = binding.mainRecylcerView;
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));

items = new ArrayList<>();

items.add(new MainItem(10, "Les Hiers"));
items.add(new MainItem(5, "Zakuro"));

adapter = new MainRecyclerViewAdapter(items);

recyclerView.setAdapter(adapter);

return view;
}
}

主 Activity .java

package net.azurewebsites.irrationnelle.testandroidstudio;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.getbase.floatingactionbutton.FloatingActionButton;

import net.azurewebsites.irrationnelle.testandroidstudio.databinding.ActivityMainBinding;
import net.azurewebsites.irrationnelle.testandroidstudio.databinding.FabTestBinding;

public class MainActivity extends AppCompatActivity {
private ActivityMainBinding activityMainBinding;

private TextView mTextMessage;

private MainFragments mainFragments;

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
}
return false;
}

};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);

mainFragments = new MainFragments();

FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.main_fragment, mainFragments);
fragmentTransaction.commit();

}

}

最佳答案

你还没有用ViewHolder绑定(bind)数据,在onBindViewHolder()中绑定(bind)它

@Override
public void onBindViewHolder(MainViewHolder holder, int position) {
MainItem status = items.get(position);

holder.bind(status);
}

更新

badgeCount 是int值,需要和String拼接

android:text="@{`` + item.bandCount}"

android:text="@{String.valueOf(item.bandCount)}"

关于java - 无法通过数据绑定(bind)在 Fragment 上使用 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42992769/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com