现在我尝试使用Data Binding实现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天...... 感谢。
<?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>
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;
}
}
<?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>
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);
}
}
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();
}
}
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;
}
}
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();
}
}
答案 0 :(得分:2)
您尚未将数据与Adapter
绑定,请将其绑定到mAdapter = new MyAdapter(context, mDataSet, new OnListClickedListener() {
@Override
public void OnListClicked(int position) {
// This is your cardView click event, You got the position
// Pass title using Intent to TargetActivity
// Since this code will be in your Fragment
}
});
ViewHolder
更新
onBindViewHolder()
是int值,因此您需要使用String
@Override
public void onBindViewHolder(MainViewHolder holder, int position) {
MainItem status = items.get(position);
holder.bind(status);
}
或
badgeCount