列表视图不显示来自firebase数据库的任何内容

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了列表视图不显示来自firebase数据库的任何内容相关的知识,希望对你有一定的参考价值。

数据很容易添加到firebase数据库中,但在列表视图中获取。请帮助activity_1(主要活动)

public class activity_1 extends AppCompatActivity {
    private Button b1, b2,b3,submit,list;
    private LinearLayout l1,l2,l3;
    private DatabaseReference Poll_data,Poll_data_3,Poll_data_4;
    private EditText question,desp,edit_option1,edit_option2,edit_option3,edit_option4;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_1);
        b1 = (Button) findViewById(R.id.poll_butn2);
        b2 = (Button) findViewById(R.id.poll_butn3);
        b3 = (Button) findViewById(R.id.poll_butn4);
        submit = (Button) findViewById(R.id.poll_submit_button);
        list = (Button)  findViewById(R.id.poll_submit_button2);

        Poll_data = FirebaseDatabase.getInstance().getReference("POll").child("poll_2");
        Poll_data_3 = FirebaseDatabase.getInstance().getReference("POll").child("poll_3");
        Poll_data_4 = FirebaseDatabase.getInstance().getReference("POll").child("poll_4");

        question     = (EditText) findViewById(R.id.poll_editque);
        edit_option1 = (EditText) findViewById(R.id.option1edit);
        desp         = (EditText) findViewById(R.id.descp_edit);
        edit_option2 = (EditText) findViewById(R.id.option2edit);
        edit_option3 = (EditText) findViewById(R.id.option3edit);
        edit_option4 = (EditText) findViewById(R.id.option4edit);


        l1 = (LinearLayout) findViewById(R.id.poll_option1layout);
        l2 = (LinearLayout) findViewById(R.id.poll_option2layout);
        l3 = (LinearLayout) findViewById(R.id.poll_option3layout);






        l1.setVisibility(l1.INVISIBLE);
        l2.setVisibility(l1.INVISIBLE);
        l3.setVisibility(l1.INVISIBLE);


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

                l1.setVisibility(View.VISIBLE);
                l2.setVisibility(View.GONE);
                l3.setVisibility(View.GONE);
            }
        });
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                l1.setVisibility(View.VISIBLE);
                l2.setVisibility(View.VISIBLE);
                l3.setVisibility(View.GONE);}
        });
        b3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                l1.setVisibility(View.VISIBLE);
                l2.setVisibility(View.VISIBLE);
                l3.setVisibility(View.VISIBLE);}
        });
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

             validate();
            }
        });
        list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            startActivity( new Intent(activity_1.this,listview_activity.class));


            }
        });


    }

        private void validate() {

            String que = question.getText().toString().trim();
            String des = desp.getText().toString().trim();
            String option1_2 = edit_option1.getText().toString().trim();
            String option2_2 = edit_option2.getText().toString().trim();
            String option3_2 = edit_option3.getText().toString().trim();
            String option4_2 = edit_option4.getText().toString().trim();


            if (!TextUtils.isEmpty(que)) {
                if (!TextUtils.isEmpty(option1_2)) {

                    if (!TextUtils.isEmpty(option2_2)) {

                        if (TextUtils.isEmpty(option4_2)) {

                            if (TextUtils.isEmpty(option3_2)) {
                                String id = Poll_data.push().getKey();
                                addpoll_2 poll2 = new addpoll_2(id, que, des, option1_2, option2_2);
                                Poll_data.child(id).setValue(poll2);
                                Toast.makeText(this, "added", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(this, "option 3 working", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(this, "option 4 working", Toast.LENGTH_SHORT).show();
                        }

                    } else {
                        Toast.makeText(this, "option 2 enter", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(this, "enter option1", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(this, "question compulsory", Toast.LENGTH_SHORT).show();
            }

        }


   }

add poll_2.Java

public class addpoll_2 {
String id;
String question;
String description;
String option_1;
String option_2;
public addpoll_2(String id, String question, String description, String 
option_1, String option_2) {
    this.id = id;
    this.question = question;
    this.description = description;
    this.option_1 = option_1;
    this.option_2 = option_2;
}

public String getId() {
    return id;
}

public String getQuestion() {
    return question;
}

public String getDescription() {
    return description;
}

public String getOption_1() {
    return option_1;
}

public String getOption_2() {
    return option_2;
}
}

ListView_activity.Java

public class listview_activity extends AppCompatActivity {


ListView listviewpoll;
private DatabaseReference Poll_data;
List<addpoll_2> addpoll_2List;

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

    listviewpoll = (ListView) findViewById(R.id.poll_listview);
    Poll_data = FirebaseDatabase.getInstance().getReference("POLL");

    addpoll_2List = new ArrayList<>();
}

@Override
protected void onStart() {
    super.onStart();

    Poll_data.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for(DataSnapshot pollSnapshot: dataSnapshot.getChildren())
            {
                addpoll_2List.clear();

                addpoll_2 poll = pollSnapshot.getValue(addpoll_2.class);

                addpoll_2List.add(poll);
            }

            poll_list adapter =  new 
poll_list(listview_activity.this,addpoll_2List);

            listviewpoll.setAdapter(adapter);



        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}
}

poll_list.Java

public class poll_list extends ArrayAdapter<addpoll_2> {


private Activity context;
private List<addpoll_2> addpoll_2List ;

public  poll_list(Activity context,List<addpoll_2>addpoll_2List){
    super(context ,R.layout.list_layout,addpoll_2List);
    this.context =context;
    this.addpoll_2List = addpoll_2List;

}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull 
ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();


    View viewItem = inflater.inflate(R.layout.list_layout,null,true);

    TextView textViewName = (TextView) viewItem.findViewById(R.id.tv);
    TextView textViewDesp = (TextView) viewItem.findViewById(R.id.tv1);

    addpoll_2 poll = addpoll_2List.get(position);


    textViewName.setText(poll.getQuestion());
    textViewDesp.setText(poll.getDescription());


    return viewItem;


  }
}

list_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearview"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin">
<TextView
    android:id="@+id/tv"
    android:textSize="25dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<TextView
    android:id="@+id/tv1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

这是所有文件,请检查并回复这些都是详细信息,如果你反对其他,请回复,因为我是android的新手。

答案

你一直在清理清单:

for (DataSnapshot pollSnapshot: dataSnapshot.getChildren()) {
    addpoll_2List.clear();

    addpoll_2 poll = pollSnapshot.getValue(addpoll_2.class);

    addpoll_2List.add(poll);
}

尝试将清除移出for循环:

addpoll_2List.clear();
for (DataSnapshot pollSnapshot: dataSnapshot.getChildren()) {
    addpoll_2 poll = pollSnapshot.getValue(addpoll_2.class);

    addpoll_2List.add(poll);
}

编辑:

Firebase需要一个空(默认)构造函数才能解析POJO,将其添加到模型类中:

public addpoll_2() {

}

以上是关于列表视图不显示来自firebase数据库的任何内容的主要内容,如果未能解决你的问题,请参考以下文章

在列表视图中显示 firebase 子对象

Firebase Analytics 调试视图不显示任何内容

swift:来自firebase的照片未显示在表格视图中

添加数据时,Firebase 列表视图未实时显示

setText 不显示来自 Firebase 的数据

获得 250 多个项目,但自定义列表视图不显示任何内容