循环创建不同的命名列表

时间:2018-07-12 17:27:03

标签: c# list

我有一个列表A,其中填充了方程式中的双变量。现在,我想创建n个等于我拥有不同名称的方程的列表。

例如,取A中的前6个元素,将它们放到List1,然后A中的下6个元素,将它们放到List2,依此类推。

已经编写了一种方法,可以将等式的数量返回为int。我知道如何创建列表,但是手动执行操作会使我的代码过于混乱。我想知道如何在循环中创建列表。 (第一个列表是List1,第二个列表是List2 ...)

1 个答案:

答案 0 :(得分:0)

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    empid = (TextView) findViewById(R.id.empid);
    emppsd = (TextView) findViewById(R.id.emppsd);
    Button btn = (Button) findViewById(R.id.btn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            userLogin();
        }
    });

}

private void userLogin() {
    //first getting the values
    final String emp_id = empid.getText().toString();
    final String password = emppsd.getText().toString();

    //validating inputs
    if (TextUtils.isEmpty(emp_id)) {
        empid.setError("Please enter your username");
        emppsd.requestFocus();
        return;
    }

    if (TextUtils.isEmpty(password)) {
        emppsd.setError("Please enter your password");
        emppsd.requestFocus();
        return;
    }
    class UserLogin extends AsyncTask<Void, Void, String> {

        ProgressBar progressBar;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressBar = (ProgressBar) findViewById(R.id.progressBar);
            progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            progressBar.setVisibility(View.GONE);


            try {
                //converting response to json object
                JSONObject obj = new JSONObject(s);



                //if no error in response
                if (!obj.getBoolean("error")) {

                    Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();

                    //getting the user from the response
                    JSONObject userJson = obj.getJSONObject("user");


                    //creating a new user object
                    User user = new User(
                            userJson.getString("emp_name"),
                            userJson.getString("designation"),
                            userJson.getString("phn no"),
                            userJson.getString("empid")
                    );


                    //storing the user in shared preferences
                    SharedPrefManager.getInstance(getApplicationContext()).userLogin(user);

                    //starting the main activity
                    startActivity(new Intent(getApplicationContext(),MainActivity.class));
                  Toast.makeText(login.this,"your credential is correct",Toast.LENGTH_LONG).show();

                } else {
                    Toast.makeText(getApplicationContext(), "Invalid username or password", Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        protected String doInBackground(Void... voids) {
            //creating request handler object
            RequestHandler requestHandler = new RequestHandler();

            //creating request parameters
            HashMap<String, String> params = new HashMap<>();
            params.put("emp_id", emp_id);
            params.put("password", password);

            //returing the response
            return requestHandler.sendPostRequest(URLs.URL_LOGIN, params);
        }
    }

    UserLogin ul = new UserLogin();
    ul.execute();
}