android - 如何实现在完成下一个按钮操作时从 arraylist 获取并显示字符串

标签 android button text view arraylist

我是 Android 新手..

我在显示下一个问题时在 Nextbutton 中遇到问题...

在我第一次设置文本时,我得到了正确的问题并匹配了四个选项答案。我需要的是..

我有一个用于显示下一个问题和答案的下一个按钮。当单击下一个按钮时,我可以获得下一个问题的下四个选项。但是在我单击下一个它显示四次后,我无法获得下一个问题下一个问题...但答案是正确的。

我做错了什么?我缺少什么?

任何帮助将不胜感激......提前非常感谢......

      protected String doInBackground(String... args) {
    List<NameValuePair> params = new ArrayList<NameValuePair>();
        System.out.println("cool");
    params.add(new BasicNameValuePair("tid", tid));
    json = jsonParser.makeHttpRequest(url_get_quesurl, "GET", params);
    System.out.println("ques value got");
    Log.d("All Groups: ", json.toString());
        System.out.println("question");
    try {
    int success = json.getInt(TAG_SUCCESS);
    System.out.println("Success");
    if (success == 1) {
    System.out.println("Success");
    groups = json.getJSONArray(TAG_GROUP);
    System.out.println("Result Success+++"+groups);
    for (int i = 0; i < groups.length();i++) {
    JSONObject c = groups.getJSONObject(i);
          String question = c.getString(TAG_QUES);
      System.out.println("Checking ::"+question);
    ques1.add(question);
    String answer = c.getString(TAG_ANSW);
    System.out.println("Checking ::"+answer);
    answ1.add(answer);
           }
        } else {
            showAlert();
        }
    } catch (JSONException e) {
        System.out.println("Error "+e.toString());
    }
    return null;
}  
   protected void onPostExecute(String file_url) {
    pDialog.dismiss();
      ques1=new ArrayList<String>(new ArrayList<String>(ques1));
            //  j=0;
            TextView txtque = (TextView) findViewById(R.id.que_txt); 
            txtque.setText(ques1.get(j));
    answ1=new ArrayList<String>(new ArrayList<String>(answ1));
            btn_practice1.setText(answ1.get(0));
            btn_practice2.setText(answ1.get(1));
            btn_practice3.setText(answ1.get(2));
            btn_practice4.setText(answ1.get(3));
         btn_practicerg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) { 
                    RadioButton radioButton = (RadioButton) findViewById(checkedId);
                   // Toast.makeText(Question.this, "" + radioButton.getText(), 2000).show(); 
                    TextView txtRadio = (TextView) findViewById(R.id.rdtxt); 
                    txtRadio.setText("" + radioButton.getText());
                }
    });
        Button nextBtn = (Button) findViewById(R.id.nxt_btn);
        nextBtn.setOnClickListener(new Button.OnClickListener(){
          public void onClick(View v){  
          j++;
        TextView txtque = (TextView) findViewById(R.id.que_txt); 
        txtque.setText(ques1.get(j)); 
        k++;
        btn_practice1.setText(answ1.get((k*4)+0));
        btn_practice2.setText(answ1.get((k*4)+1));
        btn_practice3.setText(answ1.get((k*4)+2));
        btn_practice4.setText(answ1.get((k*4)+3));
         }
       });
}

最佳答案

问题是您将答案存储在 answ1 数组中,当时四个空字符串添加到 ques1 数组中,因此您在四次按下按钮后将得到下一个问题....

为此维护两个索引,一个用于问题,另一个用于答案。

关于android - 如何实现在完成下一个按钮操作时从 arraylist 获取并显示字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14624800/

相关文章:

单击按钮时 Android 应用程序崩溃

c# - 场景统一变化时,两个游戏对象之间保持链接

带有下拉列表和箭头的 WPF 按钮

javascript - 尝试使用 filter() 从节点获取文本,但返回一个对象?

java - 更改 Android 警报对话框中当前选定项目的颜色

java - 如何将 ImageView 传递到另一个布局并将其保存在布局内?

Android:限制用户选择自动完成建议以外的选择?

android - 为android中的按钮设置透明度

html - 限制CSS中的文本区域

java - 为 Java/Groovy 导入文本文件处理库?