私はFirebaseでアンドロイドアプリケーションを作成しています。データベースにデータを書き込んでいますが、データベースに書き込んだデータを読み込んでいますArrayAdapterやFirebaseの経験はあまりありませんし、Androidではそれほど多くはありません)。ArrayAdapterがFirebaseからデータを読み込んでいません(私のアプリケーションが閉じます)
Firebaseからの読み込みを含むこの断片(メモ:データを書き込むことは別の断片にあります)は、私のアプリが閉じます。それは、 に関連するかどうかわからない - どのように実装し、ArrayAdapterを使用する - Firebaseを実装する方法 - または私がAndroidスタジオ(最新バージョン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'
}
}
}
}
- 私がデバッグモードで実行し、私はちょうど私が私のデバッガf.mIsNewlyAddedで取得inflater.inflate方法を渡す - (ローカル変数「F」を見つけることができません - これが正常であるかどうかわからない ?
- 時々私はまた、戻りVの後にnullポインタエラーを取得することは上記にリンクすることができる
だからここには、読み出したデータを表示する必要があるフラグメントで私のコードです:。。
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;
}
}
あなたは素晴らしいです!私の2つの大きな問題の一つは修正されています(これは3日間でした)。今、私はFirebaseから読み込めない理由を理解していません - このコード行は私のアプリケーションをやり直します:MyFlinder myFlinder = dataSnapshot.getValue(MyFlinder.class); – Emma
私はそれを聞いてうれしいですありがとう:) –