我正在製作一個帶有Firebase的android應用程序,並且在數據庫中寫入數據正在工作,但是通過讀取我在數據庫中寫入的數據我卡住了對ArrayAdapter,Firebase沒有太多經驗,甚至對Android也不太瞭解)。ArrayAdapter沒有從Firebase讀取數據(我的應用程序關閉)
當我從手機打開這個包含Firebase閱讀內容的片段(注:寫入數據在另一個片段中)時,我的應用程序關閉。不確定它是否與 有關 - 我如何實現和使用ArrayAdapter - 我如何實現Firebase讀取 - 或者我已更新到Android Studio(最新版本SDK 26.0.0) 或其他任何原因不見。
我得到一些錯誤: - 我得到的錯誤下面我用下面的代碼(configurations.all ...)修復。我從Firebase添加依賴關係時遇到了這個錯誤。
Error:Execution failed for task ':app:processDebugManifest'.
> Manifest merger failed : Attribute meta-data#[email protected] 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.inflate方法我在調試器f.mIsNewlyAdded得到 - 找不到局部變量「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;
}
}
你真棒!我的兩個最大的問題之一是固定的(這是3天)。現在我不明白爲什麼我無法從Firebase讀取 - 這一行代碼再次停止我的應用程序:MyFlinder myFlinder = dataSnapshot.getValue(MyFlinder.class); – Emma
我很高興聽到,謝謝:) –