为BottomNavigationView的片段添加一个难以处理的列表视图

时间:2017-07-26 05:02:26

标签: android listview android-fragments firebase firebase-realtime-database

我试图在BottonNavigationView使用的片段内显示一个难以处理的列表视图(以及其他内容)。我所做的是,创建一个包含BottomNavigationView的主活动,以及包含其他视图的片段。 listView应该连接到firebase数据库。文件区域的代码如下:

JobList.java

parSapply(cl = NULL, X, FUN, ..., simplify = TRUE,
          USE.NAMES = TRUE)

MainActivity.java

package com.example.sherwin.todo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class JobList extends Fragment {

public JobList() {
}
public static JobList newInstance() {
    JobList fragment = new JobList();

    return fragment;
}

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

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView =  inflater.inflate(R.layout.fragment_job_list, container, false);

    // Create a new Adapter
    final ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
            android.R.layout.simple_list_item_1, android.R.id.text1);

    // Get ListView object from xml
    final ListView listView = (ListView)rootView.findViewById(R.id.listView);

    // Assign adapter to ListView
    listView.setAdapter(adapter);

    // Connect to the Firebase database
    FirebaseDatabase database = FirebaseDatabase.getInstance();

    // Get a reference to the todoItems child items it the database
    final DatabaseReference myRef = database.getReference("JOBS");

    // Assign a listener to detect changes to the child items
    // of the database reference.
    myRef.addChildEventListener(new ChildEventListener() {

        // This function is called once for each child that exists
        // when the listener is added. Then it is called
        // each time a new child is added.
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {

            String value = dataSnapshot.getKey();
            adapter.add(value);
        }

        // This function is called each time a child item is removed.
        public void onChildRemoved(DataSnapshot dataSnapshot) {
            String value = dataSnapshot.getKey();
            adapter.remove(value);
        }

        // The following functions are also required in ChildEventListener implementations.
        public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
        }

        public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
        }

        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            Log.w("TAG:", "Failed to read value.", error.toException());
        }
    });

   ///to send to next page
    Button nextPage = (Button) rootView.findViewById(R.id.addNewJob);

    // Capture button clicks
    nextPage.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            // Start NewActivity.class
            Intent myIntent = new Intent(getContext(),
                    JobForm.class);
            startActivity(myIntent);
        }
    });

    return rootView;
}

}

fragment_job_list.xml

package com.example.name.todo;

import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


private TextView mTextMessage;

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

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment selectedFragment = null;
        switch (item.getItemId()) {
            case R.id.navigation_home:
                selectedFragment = HomePage.newInstance();
                break;
            case R.id.navigation_job:
                selectedFragment = JobList.newInstance();
                break;
            case R.id.navigation_machine:
                selectedFragment = MachinePage.newInstance();
                break;
            case R.id.navigation_mail:
                selectedFragment = MessagePage.newInstance();
                break;
            case R.id.navigation_resources:
                selectedFragment = ResourcePage.newInstance();
                break;
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.content, selectedFragment);
        transaction.commit();
        return true;
    }

};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}

}

logcat吐出以下内容:

<?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:orientation="vertical">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2.8">

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_above="@+id/addNewJob"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"></ListView>

<Button
    android:id="@+id/addNewJob"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="@string/add_job" />

</RelativeLayout>

</LinearLayout>

我不知道该怎么做,任何帮助都会受到赞赏。

感谢

1 个答案:

答案 0 :(得分:0)

@JohnC说的话。更新了清单以反映JobList.java不是活动