ArrayAdapter 未从 Firebase 读取数据(我的应用已关闭)



我正在使用Firebase制作一个Android应用程序,并且在数据库中写入数据可以正常工作,但是在读取我在数据库中写入的数据时,我陷入了困境(我对ArrayAdapter,Firebase没有太多经验,甚至对Android也没有太多经验(。

当我从手机打开这个包含从 Firebase 读取的片段时(注意:写入数据在另一个片段中(,我的应用程序关闭了。不确定是否与 - 我如何实现和使用阵列适配器 - 我如何实现火基阅读 - 或者我已经更新到安卓工作室(最新版本SDK 26.0.0( - 或任何其他我看不到的原因。

我得到一些错误: - 我得到下面的错误,我在下面用代码修复了(配置.all...当我从Firebase添加依赖项时,我遇到了此错误。

Error:Execution failed for task ':app:processDebugManifest'.
> Manifest merger failed : Attribute meta-data#android.support.VERSION@value value=(26.0.0) from [com.android.support:design:26.0.0] AndroidManifest.xml:28:13-35
is also present at [com.android.support:cardview-v7:25.4.0] AndroidManifest.xml:25:13-35 value=(25.4.0).
Suggestion: add 'tools:replace="android:value"' to <meta-data> element at AndroidManifest.xml:26:9-28:38 to override.
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
if (!requested.name.startsWith("multidex")) {
details.useVersion '25.3.0'
}
}
}
}
  • 当我在调试模式下运行时,我只是传递了我在调试器中获得的 inflater.inflow 方法 f.mIsNewlyAdd - 找不到局部变量"f"( - 不确定这是否正常?
  • 有时我也会在返回 v 后收到空指针错误。它可能与上述内容有关。

所以这是我在片段中需要显示读取数据的代码:

public class NewFlinderFragment extends Fragment {
private DatabaseReference mMyFlindersDatabaseReference;
private ChildEventListener mChildEventListener;
private MyFlinderAdapter mMyFlinderAdapter;
private ListView mMyFlinderListView;
private MyFlinder myFlinder;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.new_flinder, container, false);
mMyFlinderListView = (ListView) v.findViewById(R.id.my_flinders_list_view);
//instantiate the reference to MyFlinders in Firebase
mMyFlindersDatabaseReference = MainActivity.mFirebaseDatabase.getReference().child("MyFlinders");
List<MyFlinder> myFlindersList = new ArrayList<>();
mMyFlinderAdapter = new MyFlinderAdapter(getContext(), R.layout.my_flinder, myFlindersList);
MyFlinder myFlinder = new MyFlinder(1,"flinderName",100,1,"Url");
mMyFlinderAdapter.add(myFlinder);

mMyFlinderListView.setAdapter(mMyFlinderAdapter);
mChildEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Toast.makeText(getActivity(), "Boo", Toast.LENGTH_SHORT).show();
Toast.makeText(getActivity(), "difszmfcresg", Toast.LENGTH_SHORT).show();
MyFlinder myFlinder = dataSnapshot.getValue(MyFlinder.class);
mMyFlinderAdapter.add(myFlinder);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
mMyFlindersDatabaseReference.addChildEventListener(mChildEventListener);
return v;
}
}

我在 MainActivity 中为整个应用程序初始化的数据库(再次注意,写入数据库是有效的,并且位于此处未列出的另一个片段中(。

public class MainActivity extends AppCompatActivity  {
public static FirebaseDatabase mFirebaseDatabase;
// variables for authentication with Firebase
public static final int RC_SIGN_IN = 1;
static FirebaseAuth mFirebaseAuth;
static FirebaseAuth.AuthStateListener mAuthStateListener;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFirebaseDatabase = FirebaseDatabase.getInstance();
//instantiate the authentication from Firebase
mFirebaseAuth = FirebaseAuth.getInstance();
.....

这是包含列表视图的膨胀布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="24dp"
android:paddingTop="24dp"
android:text="@string/choose_a_flinder_to_grow"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="24sp"
android:textStyle="bold" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="@+id/my_flinders_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@android:color/transparent"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"
tools:listitem="@layout/my_flinder" />
</LinearLayout>
</LinearLayout>

这是列表视图 (my_flinder( 的各个部分之一:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_flinder_linear_layout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="24dp"
android:gravity="center">
<ImageView
android:id="@+id/my_flinder_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/ic_no_text_white"
android:scaleType="centerInside" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical">
<TextView
android:id="@+id/my_flinder_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/Blue"
android:paddingStart="18dp"
android:paddingTop="20dp"
android:paddingEnd="6dp"
android:paddingBottom="6dp"
android:textSize="20dp"/>
<TextView
android:id="@+id/my_flinder_points"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:paddingStart="18dp"
android:paddingTop="6dp"
android:paddingEnd="6dp"
android:paddingBottom="6dp"
android:textSize="20dp"/>
</LinearLayout>
<TextView
android:id="@+id/my_flinder_money"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/Pink"
android:paddingStart="20dp"
android:paddingTop="20dp"
android:paddingEnd="6dp"
android:paddingBottom="6dp"
android:textSize="20dp"/>
</LinearLayout>

这是我的MyFlinderAdapter类:

public class MyFlinderAdapter extends ArrayAdapter<MyFlinder>{
public MyFlinderAdapter (Context context, int resource, List<MyFlinder> objects){
super(context, resource, objects);
}
@Override
public View getView (int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.my_flinder, parent, false);
}
ImageView myFlinderImage = (ImageView) convertView.findViewById(R.id.my_flinder_image);
TextView myFlinderName = (TextView) convertView.findViewById(R.id.my_flinder_name);
TextView myFlinderPoints = (TextView) convertView.findViewById(R.id.my_flinder_points);
TextView myFlinderMoney = (TextView) convertView.findViewById(R.id.my_flinder_money);
MyFlinder myFlinder = getItem(position);
myFlinderImage.setVisibility(View.VISIBLE);
myFlinderName.setVisibility(View.VISIBLE);
myFlinderPoints.setVisibility(View.VISIBLE);
myFlinderMoney.setVisibility(View.VISIBLE);
myFlinderName.setText(myFlinder.getFlinderName());
myFlinderPoints.setText(myFlinder.getFlinderRequiredPoints());
myFlinderMoney.setText((int) myFlinder.getFlinderMoneyValue());
return convertView;
}
}

myFlinderMoney.setText((int) myFlinder.getFlinderMoneyValue());
该代码段可能会导致问题。由于您提供的int,setText 方法假定您给出int是资源 ID。因此,它会查找该资源,但找不到。你应该把字符串放进去。因此,它给出以下错误:

07-29 14:27:42.552 4631 4631 E AndroidRuntime: android.content.res.Resources$NotFoundException: 字符串资源 ID #0x64

希望对您有所帮助!

相关内容

最新更新