自定义适配器ListView错误

时间:2017-12-14 12:54:53

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

我试图在export default { data () { return { typeButton: 'submit' } }, methods: { checkout(e) { this.typeButton = 'button' } }, } 中列出一些用户详细信息,每行显示4个字符串。

字符串来自Listview

我总是收到一些错误。有关错误原因或任何其他解决方案的任何想法吗?

这是我的代码:

MainActivity

Firebase

这是userslist.xml

public class SearchActivity extends AppCompatActivity {

    private ListView mListView;
    //public ArrayList<String> mUser = new ArrayList<>();
    private String userID;
    private String Lat;
    private String Lng;
    private Button mRefresh;
    private String Name;
    private String Birthday;
    private String Gender;
    private String Email;

    private FirebaseAuth mAuth = FirebaseAuth.getInstance();
    private FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
    private DatabaseReference DatabaseReference = firebaseDatabase.getReference();

    //private DatabaseReference DatabaseReference = firebaseDatabase.getReference().child("Users/");
    //private DatabaseReference CoordenateReference = firebaseDatabase.getReference().child("Coordenate/");

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


        mListView = findViewById(R.id.listViewSearch);

        FirebaseUser user = mAuth.getCurrentUser();
        userID = user.getUid();
        mRefresh = findViewById(R.id.buttonRefresh);


        mRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                DatabaseReference.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        listClean();
                        FirebaseUser user = mAuth.getCurrentUser();
                        userID = user.getUid();
                        Lat = dataSnapshot.child("Users").child(userID).child("Coordenates/Rute/Lat").getValue(String.class);
                        Lng = dataSnapshot.child("Users").child(userID).child("Coordenates/Rute/Lng").getValue(String.class);
                        DataSnapshot dS = dataSnapshot.child("Coordenate").child(Lat).child(Lng);

                        for(DataSnapshot Ds : dS.getChildren()) {
                            Object key = Ds.getKey();
                            String StringKey = String.valueOf(key);
                            getname(StringKey);
                            //mUser.add(getName(StringKey));
                            //mUser.add(Name);
                        }

                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        Toast.makeText(SearchActivity.this,"Error 404",Toast.LENGTH_SHORT).show();

                    }
                });
            }
        });



    }
    private void getname(String ID){


        final String IDfinal = ID;
        Toast.makeText(SearchActivity.this,ID,Toast.LENGTH_SHORT).show();
        DatabaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Name = dataSnapshot.child("Users").child(IDfinal).child("name").getValue(String.class);
                Email = dataSnapshot.child("Users").child(IDfinal).child("email").getValue(String.class);
                Gender = dataSnapshot.child("Users").child(IDfinal).child("gender").getValue(String.class);
                Birthday = dataSnapshot.child("Users").child(IDfinal).child("data_birth").getValue(String.class);

                list();


                Toast.makeText(SearchActivity.this,Name,Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Toast.makeText(SearchActivity.this,"Error 404",Toast.LENGTH_SHORT).show();

            }
        });
    }

    private void list(){
        //final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mUser);
        //mListView.setAdapter(new customAdapter(this,new String[]{Name}));
        //mUser.add(Name);
        //arrayAdapter.notifyDataSetChanged();
        setContentView(R.layout.activity_search);
        List<UserInformation> userInformationsList = new ArrayList<>();
        UserInformation userInformation = new UserInformation(Name,Email,Gender,Birthday);
        userInformationsList.add(userInformation);
        customAdapter customAdapterIntent= new customAdapter((ValueEventListener) this,R.layout.userslist,userInformationsList);
        mListView.setAdapter(customAdapterIntent);
    }
}

这是Userinformation.java

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical">

    <TextView
        android:id="@+id/textNameList"
        android:textSize="25dp"
        android:textAlignment="textStart"
        android:text="Name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="start"
        android:layout_margin="10dp"/>

    <TextView
        android:id="@+id/textEmailList"
        android:textSize="15dp"
        android:textAlignment="textStart"
        android:text="Age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="start"
        android:layout_margin="10dp"/>

    <TextView
        android:id="@+id/textBirthdayList"
        android:textSize="15dp"
        android:textAlignment="textStart"
        android:text="Birthday"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="start"
        android:layout_margin="10dp"/>

    <TextView
        android:id="@+id/textGenderList"
        android:textSize="15dp"
        android:textAlignment="textStart"
        android:text="gender"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="start"
        android:layout_margin="10dp"/>

这是适配器

public class UserInformation {


    private static String Name;
    private static String Gender;
    private static String Email;
    private static String Birthday;

    public UserInformation(String Name,String Gender,String Email,String Birthday){
        this.Birthday=Birthday;
        this.Email=Email;
        this.Gender=Gender;
        this.Name=Name;
    }

    public static String getName(){return Name;}
    public static String getEmail(){return Email;}
    public static String getGender(){return Gender;}
    public static String getBirthday(){return Birthday;}

}

这是错误

public class customAdapter extends ArrayAdapter<UserInformation> {




    public customAdapter(ValueEventListener context, int layoutResource, 
    List<UserInformation> userInformationsList) {
        super((Context) context, layoutResource, userInformationsList);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = convertView;

        if (view == null) {
            LayoutInflater layoutInflater = LayoutInflater.from(getContext());
            view = layoutInflater.inflate(R.layout.userslist, null);
        }

        UserInformation userInformation = getItem(position);

        if (userInformation != null) {
            TextView Name = (TextView) view.findViewById(R.id.textNameList);
            //name.setText(name[position]);
            TextView email = (TextView) view.findViewById(R.id.textEmailList);
            //email.setText(Email[position]);
            TextView gender = (TextView) view.findViewById(R.id.textGenderList);
            //gender.setText(Gender[position]);
            TextView birthday = (TextView) view.findViewById(R.id.textBirthdayList);
            //birthday.setText(Birthday[position]);

            if (Name != null) {
                Name.setText(UserInformation.getName());
            }
            if (email != null) {
                email.setText(UserInformation.getEmail());
            }
            if (gender != null) {
                gender.setText(UserInformation.getGender());
            }
            if (birthday != null) {
                birthday.setText(UserInformation.getBirthday());
            }
        }

        return view;
    }
}

1 个答案:

答案 0 :(得分:3)

您需要传递Context而不是ValueEventListener,因此请将适配器更改为

public customAdapter(/*ValueEventListener*/ Context context, int layoutResource, 
List<UserInformation> userInformationsList) {
    super(context, layoutResource, userInformationsList);

}

并删除演员阵容

 customAdapter customAdapterIntent= new customAdapter(/*(ValueEventListener)*/ this,R.layout.userslist,userInformationsList);

context或适配器需要layoutinflater才能在您拨打getContext()时访问资源

并从setContentView(R.layout.activity_search);方法删除list,你这样做了两次,所以不需要

Thank so much, I did as you said and It works but it only list the last user's information

您需要获取所有用户并将其添加到list中,因为当前您将最后一个用户的信息存储为字符串值,并使用这些值创建单个用户对象的列表。

// outside onCreate
List<UserInformation> users = new ArrayList<>();


mRefresh.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        DatabaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                listClean();
                FirebaseUser user = mAuth.getCurrentUser();
                userID = user.getUid();
                Lat = dataSnapshot.child("Users").child(userID).child("Coordenates/Rute/Lat").getValue(String.class);
                Lng = dataSnapshot.child("Users").child(userID).child("Coordenates/Rute/Lng").getValue(String.class);
                DataSnapshot dS = dataSnapshot.child("Coordenate").child(Lat).child(Lng);

                for(DataSnapshot Ds : dS.getChildren()) {
                    Object key = Ds.getKey();
                    String StringKey = String.valueOf(key);

                    // fetch the user Object based on key and add it to list
                    users.add(lidataSnapshot.child("Users").child(StringKey).getValue(UserInformation.class));
                }
                 // now display list with all user information 
                 customAdapter customAdapterIntent= new customAdapter(SearchActivity.this,
                                                                      R.layout.userslist,
                                                                      users);
                 mListView.setAdapter(customAdapterIntent);

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Toast.makeText(SearchActivity.this,"Error 404",Toast.LENGTH_SHORT).show();

            }
        });
    }
});