【问题标题】:cannot fetch values from firebase database无法从 firebase 数据库中获取值
【发布时间】:2018-04-16 11:40:52
【问题描述】:

主要活动.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);

        }
    });


}

}

适配器类

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 数据库中并检索到应用程序的列表视图中 当用户单击列表视图时,他被定向到有多个进度条的活动 我在进度条上添加了一个点击监听器,所以当用户点击进度条时,该选项的 val 在数据库中会增加。因此,当不同的用户对同一个投票进行投票时,会从数据库中获取值并添加当前用户的值以显示获胜者,但问题是进度条的值1 从数据库中获取值,但其他两个保持进度条每次用户单击其他两个进度条(即 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;
}
}

【问题讨论】:

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


【解决方案1】:

代码: Activity_3.java

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

这些是要进行的更改

【讨论】:

    【解决方案2】:
      //Read from the database
    
            myRef.addValueEventListener(new ValueEventListener() 
    
                     {
    
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    // This method is called once with the initial value and again
                    // whenever data at this location is updated.
                    String value = dataSnapshot.getValue(String.class);
                    Log.d(TAG, "Value is: " + value);
                }
    
                @Override
                public void onCancelled(DatabaseError error) {
                    // Failed to read value
                    Log.w(TAG, "Failed to read value.", error.toException());
                }
            });
    

    【讨论】:

      猜你喜欢
      • 2017-10-13
      • 2018-11-30
      • 2021-10-25
      • 2021-04-28
      • 1970-01-01
      • 1970-01-01
      • 2017-07-02
      • 2019-09-30
      • 1970-01-01
      相关资源
      最近更新 更多