2017-08-24 49 views
0

我想要完成的是取一个数组,将它排序,拉出前5个结果并将它们显示在一个小窗口中。建议我尝试使用前面提到的方法来完成此操作,如下面的代码所示。我对Android还是比较陌生,这是我第一次尝试使用StringBuffer和AlertDialog。Android:传递数组到StringBuffer到AlertDialog

我的错误是我做了一些错误的初始化lastScoreMessage变量或我正确地传递它。我所知道的是,即使我在我的onClick开关案例中专门调用了它,它给了我一个错误,说它从来没有使用过(在lastFive方法中),并且无法在onClick switch语句中解析符号lastScoreMessage,我试图用它。

这里的任何帮助,非常感谢。

public class EditTextButtons extends AppCompatActivity implements View.OnClickListener { 

    DBHandler db; 

    Calendar c = Calendar.getInstance (); 
    Date d = c.getTime (); 

    EditText etName; 
    Button btnAdd; 
    Button btnLastFive; 

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

     db = new DBHandler (this); 

     etName = (EditText) findViewById (R.id.etName); 
     btnAdd = (Button) findViewById (R.id.btnAdd); 
     btnLastFive = (Button) findViewById (R.id.btnLastFive); 

     // set listeners 
     btnAdd.setOnClickListener (this); 
     btnLastFive.setOnClickListener (this); 

    } 

    @Override 
    public void onClick(View v) { 
     switch (v.getId ()) { 
      case R.id.btnAdd: 
       insertIntoDB (); 
       Intent i = new Intent (getApplicationContext (), Snake.class); 
       startActivity (i); 
       break; 
      case R.id.btnLastFive: 
       lastFive(); 
       showMessage ("Last 5 Scores", lastScoreMessage); 
       break; 
      default: 
       break; 
     } 
    } 


    protected void insertIntoDB() { 
     ContentValues cv = new ContentValues (); 
     cv.put (db.USERNAME, etName.getText ().toString ()); 
    } 

    protected void lastFive() { 
     int listsize = 5; 
     List<User> userlist = new ArrayList<User> (); 
     userlist = db.convertDatabaseToList (); 
     Collections.sort (userlist, User.Comparators._id); 

     StringBuffer buffer = new StringBuffer(); 
     String lastScoreMessage; 

     for (int i = 0; i < listsize; i++) { 
      User temp = userlist.get(i); 

      String username = temp.getName(); 
      int score = temp.getScore(); 

      buffer.append(username + ": " + score + "\n"); 
     } 

     lastScoreMessage = buffer.toString(); 
    } 


    protected void showMessage(String title, String message) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setCancelable(true); 
     builder.setTitle(title); 
     builder.setMessage(message); 
     builder.show(); 
    } 
} 
+1

看起来'lastScoreMessage'不是'onClick'可以访问的方法的变量。您需要将其声明为一个类变量,它只能在方法lastFive中访问,并在此之后不做任何事情。 @Pavneet_Singh击败了我。 :-) – escapesequence

回答

1

只需在类中而不是方法中创建lastScoreMessage。问题是变量的范围只是最高的方法。首先在类中创建变量。然后像你一样用方法存储价值。

+0

我现在感觉很傻,很明显!非常感谢! –

0

你可以声明lastScoreMessage为类变量的的影响它在lastFive()方法或修改lastFive()方法返回您传递给showMessage()方法

String message = lastFive(); 
showMessage ("Last 5 Scores", message); 

串并设置lastFive()方法像这样

protected String lastFive() { 
    int listsize = 5; 
    List<User> userlist = new ArrayList<User> (); 
    userlist = db.convertDatabaseToList (); 
    Collections.sort (userlist, User.Comparators._id); 

    StringBuffer buffer = new StringBuffer(); 
    String lastScoreMessage; 

    for (int i = 0; i < listsize; i++) { 
     User temp = userlist.get(i); 

     String username = temp.getName(); 
     int score = temp.getScore(); 

     buffer.append(username + ": " + score + "\n"); 
    } 

    lastScoreMessage = buffer.toString(); 

    return lastScoreMessage 
}