无法从firebase数据库中获取值

时间:2018-04-16 11:40:53

标签: android firebase listview firebase-realtime-database progress-bar

主要活动.java

public class activity_3 extends AppCompatActivity {

   TextView question,option_1,option_2,option_3,description,winnner;
  NumberProgressBar option_progress1, option_progress2,option_progress3;
 int val_1;
 int val_2;
 int val_3;
     DatabaseReference Polldata_3;
    String optionOne;
   String optionTwo;
   String optionThree;

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


    final String que =  getIntent().getExtras().getString("que");
    final String des =  getIntent().getExtras().getString("des");
     optionOne =  getIntent().getExtras().getString("option1");
     optionTwo =  getIntent().getExtras().getString("option2");
     optionThree =  getIntent().getExtras().getString("option3");
    final String id_user   =  getIntent().getExtras().getString("id");
    val_1 = getIntent().getExtras().getInt("val1");
    val_2 = getIntent().getExtras().getInt("val2");
    val_2 = getIntent().getExtras().getInt("val3");
    option_progress1 = (NumberProgressBar) findViewById(R.id.option1_progressbar);
    option_progress2 = (NumberProgressBar) findViewById(R.id.option2_progressbar);
    option_progress3 = (NumberProgressBar) findViewById(R.id.option3_progressbar);

    Polldata_3 = FirebaseDatabase.getInstance().getReference("POll").child("poll_3");

    final DatabaseReference answsersave = Polldata_3.child(id_user);

    question = (TextView) findViewById(R.id.question_showpoll);
    option_1 = (TextView) findViewById(R.id.option_1);
    option_2 = (TextView) findViewById(R.id.option_2);
    option_3 = (TextView) findViewById(R.id.option_3);

    description  = (TextView) findViewById(R.id.description_user_3);
    winnner   = (TextView) findViewById(R.id.winner);


    option_1.setText(optionOne);
    option_2.setText(optionTwo);
    option_3.setText(optionThree);
    question.setText(que);
    description.setText(des);


    option_progress1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            option_progress1.setProgress(val_1+1);
            option_progress1.setEnabled(false);
            option_progress2.setEnabled(false);
            option_progress3.setEnabled(false);
            val_1++;


            answsersave.child("option_1_value").setValue(val_1);
            //winnerdeclare();

        }
    });
    option_progress2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            option_progress2.setProgress(val_2+1);
            option_progress1.setEnabled(false);
            option_progress2.setEnabled(false);
            option_progress3.setEnabled(false);
            val_2++;
            answsersave.child("option_2_value").setValue(val_2);
          //  winnerdeclare();

        }
    });

    option_progress3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            option_progress3.setProgress(val_3+1);
            option_progress1.setEnabled(false);
            option_progress2.setEnabled(false);
            option_progress3.setEnabled(false);
            val_3++;
          //  winnerdeclare();
            answsersave.child("option_3_value").setValue(val_3);

        }
    });


}

}

ADAPTER CLASS

public class listview_3 extends AppCompatActivity {


ListView listviewpoll3;
private DatabaseReference Poll_data_3;
List<addpoll_3> addpoll_3List;

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

    listviewpoll3 = (ListView) findViewById(R.id.poll_listview_3);
    Poll_data_3 = FirebaseDatabase.getInstance().getReference("POll").child("poll_3");

    addpoll_3List = new ArrayList<>();


    listviewpoll3.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
            Intent intent = new Intent(listview_3.this, activity_3.class);
            addpoll_3 poll = addpoll_3List.get(position);
            final String optionone = poll.getOption_1();
            final String optiontwo = poll.getOption_2();
            final String optionthree = poll.getOption_3();
            final String id_user = poll.getId();
            final int value_1 = poll.getOption_1_value();
            final int value_2 = poll.getOption_2_value();
            final int value_3 = poll.getOption_3_value();
            final String question = poll.getQuestion();
            final String desp = poll.getDescription();

            intent.putExtra("option1",optionone);
            intent.putExtra("option2",optiontwo);
            intent.putExtra("option3",optionthree);
            intent.putExtra("id",id_user);
            intent.putExtra("val1",value_1);
            intent.putExtra("val2",value_2);
            intent.putExtra("val3",value_3);
            intent.putExtra("que",question);
            intent.putExtra("descp",desp);



            startActivity(intent);
        }
    });

        }

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

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

            addpoll_3List.clear();

            for(DataSnapshot pollSnapshot: dataSnapshot.getChildren())
            {

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

                addpoll_3List.add(poll);
            }

            poll_list_3 adapter =  new poll_list_3(listview_3.this,addpoll_3List);

            listviewpoll3.setAdapter(adapter);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}
}

列出类

public class poll_list_3 extends ArrayAdapter<addpoll_3> {

private Activity context;
private List<addpoll_3> addpoll_3List;

public poll_list_3(Activity context, List<addpoll_3> addpoll_3List) {
    super(context, R.layout.list_layout, addpoll_3List);
    this.context = context;
    this.addpoll_3List = addpoll_3List;





}

@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);
    TextView textViewName = (TextView) viewitem.findViewById(R.id.tv);
    TextView textViewDesp = (TextView) viewitem.findViewById(R.id.tv1);
    final addpoll_3 poll1 = addpoll_3List.get(position);


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

    return viewitem;



}
}

我正在创建一个轮询应用程序,用户可以在其中创建一个轮询,然后将其存储在firebase数据库中并检索到应用程序的列表视图中 当用户点击列表视图时,他将被定向到有多个进度栏的活动 我在进度条上添加了一个ON-click监听器,因此当用户单击进度条时,该选项的val在数据库中会增加。因此,当不同的用户对同一个轮询进行投票时,将获取数据库中的值,并添加显示获胜者的当前用户的值,但问题是progressbar1的值从数据库获取值,但其他两个保持进度条每次用户点击另外两个进度条(即2和3)时,值从0开始。  请帮忙

addpoll_3.java

public class addpoll_3 {


String id;
String question;
String description;
String option_1;
String option_2;
String option_3;
int option_1_value;
int option_2_value;
int option_3_value;

public  addpoll_3(){}



public addpoll_3(String id, String question, String description, String option_1, String option_2, String option_3, int option_1_value, int option_2_value, int option_3_value) {
    this.id = id;
    this.question = question;
    this.description = description;
    this.option_1 = option_1;
    this.option_2 = option_2;
    this.option_3 = option_3;
    this.option_1_value = option_1_value;
    this.option_2_value = option_2_value;
    this.option_3_value = option_3_value;
}

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

public String getOption_3() {
    return option_3;
}

public int getOption_1_value() {
    return option_1_value;
}

public int getOption_2_value() {
    return option_2_value;
}

public int getOption_3_value() {
    return option_3_value;
}
}

2 个答案:

答案 0 :(得分:1)

代码: Activity_3.java

val_1 = getIntent().getExtras().getInt("val1"); 
val_2 = getIntent().getExtras().getInt("val2");
val_3 = getIntent().getExtras().getInt("val3"); 

这些是要做的改变

答案 1 :(得分:0)

telnet