-1

Data is easily been added to the firebase database but fetch in the list view.please help activity_1(Main Activity)

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();
            }

        }


   }

addpoll_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" />

this are all files,please check and reply these are all details,please comment back if you anthing else,as i am new to android.

ColdFire
  • 6,764
  • 6
  • 35
  • 51
Raj K
  • 41
  • 7

1 Answers1

0

You're constantly cleaning the list here:

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

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

    addpoll_2List.add(poll);
}

Try moving the clear out of the for loop:

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

    addpoll_2List.add(poll);
}

EDIT:

Firebase needs an empty (default) constructor to be able to parse POJOs, add this to your model class:

public addpoll_2() {

}
ColdFire
  • 6,764
  • 6
  • 35
  • 51
Levi Moreira
  • 11,917
  • 4
  • 32
  • 46
  • hie sir, still not displaying any objects – Raj K Apr 10 '18 at 16:42
  • could you post your database structure? – Levi Moreira Apr 10 '18 at 16:44
  • sorry i am complete noob here, please understand if any mistakes. – Raj K Apr 10 '18 at 16:53
  • Could you post a screenshot of you database structure? – Levi Moreira Apr 10 '18 at 17:14
  • hie sir can you tell me how to open a new activity by clicking on the listview and then display the content of that particular source from firebase database – Raj K Apr 10 '18 at 17:33
  • you add a click listener to the listview, see this article: https://android--code.blogspot.com.br/2015/08/android-listview-item-click.html. This question tells how to open another activity https://stackoverflow.com/questions/4186021/how-to-start-new-activity-on-button-click. – Levi Moreira Apr 10 '18 at 18:18
  • All you have to do is to pass the ide of the clicked item, receive it in the next activity and query firebase again for the data of the given item – Levi Moreira Apr 10 '18 at 18:18