如何循环浏览HashMap表并使用按钮一次从1行发送数据?

时间:2014-04-29 18:19:52

标签: java android

Noob程序员在这里!我想循环遍历HashMap并将数据从1行发送到按钮单击的方法。也就是说,当我将第一行的数据发送到方法中时按下按钮,再次按下该按钮时,行nr 2中的数据将被发送到方法中,依此类推。

我怎么能这样做,甚至可能吗?

编辑方法代码:

    public void create_dialog_method(String question_param, final boolean identifier) {
    AlertDialog.Builder alert = new AlertDialog.Builder(this);

    alert.setTitle("Title");
    alert.setMessage(question_param);

    alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface dialog, int whichButton) {

          if (identifier)

              showRiskEvaluationDialog();
          else 
              if (identifier == false);

              //<<<<<This is where i need a loop that only picks 1 row each time it's called>>>>
              for (Entry<String, Boolean> entry:  question_map.entrySet()){
                  create_dialog_method(entry.getKey(),entry.getValue());
              }


             Toast.makeText(getApplicationContext(), "Saved",
                   Toast.LENGTH_SHORT).show();

       }
    });

    alert.setNegativeButton("NO", new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface dialog, int whichButton) {

          if (identifier)
              Toast.makeText(getApplicationContext(), "Saved",
                      Toast.LENGTH_SHORT).show();
          else

          showRiskEvaluationDialog();
       }
    });
    alert.show();
 }

我的班级和onCreate();包括带问题的地图和&#34;标识符&#34;布尔值:

public class QuestionsActivity extends Activity {

Map<String, Boolean> question_map = new LinkedHashMap<String, Boolean>();
int counter = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test_layout);

    //false = call showRiskEvaluationDialog() on button NO
    //true  = call showRiskEvaluationDialog() on button YES


    question_map.put("8. Question8?", false);
    question_map.put("7. Question7?", false);
    question_map.put("6. Question6?", false);
    question_map.put("5. Question5?", false);
    question_map.put("4. Question4?", false);
    question_map.put("3. Question3?", false);
    question_map.put("2. Question2?", false);
    question_map.put("1. Question1?", false);

    create_dialog_method("asd2313", false);

    // For loop to extract a question and identifier from the Map
    /**
    for (Entry<String, Boolean> entry : question_map.entrySet()){
       create_dialog_method(entry.getKey(), entry.getValue());
    }//end for
    **/

}//end onCreate()

1 个答案:

答案 0 :(得分:0)

看一下map的keySet()或entrySet()函数。如果您有一个名为myMap的Map,您可以执行以下操作:

for(String key : myMap.keySet()){
String value = myMap.get(key);
writeDataSomewhere(key, value);

}

显然writeDataSomewhere()方法是你的....而不是地图