Single EditText如何在android中将多个edittext值存储到数据库中

时间:2014-09-23 12:52:03

标签: android

您好如何获取学生姓名包含n个值,我想将所有值存储在一个数组中。因为我想获得所有n个学生姓名值。

因为我创建了两个数组如果我正在运行它给出了不幸的错误。我想存储存储到数据库的所有学生名称。

java文件:

public class Attend extends Activity implements OnClickListener {

    String data = "";
    TableLayout tl;
    TableRow tr;
    TextView studentName;
    EditText Firstname,stRoll1;
    CheckBox present;
    String[] class_name, section_name;

    EditText[] studentNames= new EditText[500];
    EditText[] rollNos= new EditText[500];
    int j=0;

    @SuppressLint("NewApi")
    String student_name[],roll_no;

    Button save;
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tl = (TableLayout) findViewById(R.id.maintable);

         save = (Button)findViewById(R.id.save); 
         studentNames[j]  = (EditText) findViewById(R.id.studentname );
         studentNames[j].setVisibility(View.GONE);
         stRoll1 = (EditText) findViewById(R.id.rollnumber ); 
         stRoll1.setVisibility(View.GONE);
         save.setOnClickListener(new View.OnClickListener() {

            @TargetApi(Build.VERSION_CODES.GINGERBREAD)
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //save.setText("Record Saved Sucessfully");
                student_name[j] = studentNames[j].getText().toString();
                System.out.println(student_name[j]);
                roll_no = stRoll1.getText().toString();
                System.out.println(roll_no);

                Toast.makeText(
                        Attend.this,
                        "Result : " + "\nStudentName : " +student_name
                        + "\nRollno : " + roll_no,

                        Toast.LENGTH_SHORT).show();
                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

                nameValuePairs.add(new BasicNameValuePair("student_name",student_name[j]));
                nameValuePairs.add(new BasicNameValuePair("roll_no",roll_no));
               // nameValuePairs.add(new BasicNameValuePair("f3",v3));

                StrictMode.setThreadPolicy(policy); 
                final String queryString = "student_name=" + student_name+ "&roll_no=" + roll_no;
                data = DatabaseUtility.executeQueryPhp("save",queryString);
                System.out.println(data);
            }


        });

        String class_id = getIntent().getStringExtra("class_id");
        String section_id = getIntent().getStringExtra("section_id");
        Toast.makeText(
                this,
                "Result : " + "\nclassName : " + class_id + "\nSectionName : "
                        + section_id,

                Toast.LENGTH_SHORT).show();

        final String queryString = "class_id=" + class_id + "&section_id="
                + section_id;
        /*String result = DatabaseUtility.executeQueryPhp("getStudent",
                queryString);*/

        new Thread(new Runnable() {
            public void run() {
                data = DatabaseUtility.executeQueryPhp("getStudent",queryString);
                System.out.println(data);

                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        ArrayList<StudentDetails> users = parseJSON(data);
                        addData(users);
                    }
                });

            }
        }).start();

    }


    public ArrayList<StudentDetails> parseJSON(String result) {
        ArrayList<StudentDetails> users = new ArrayList<StudentDetails>();
        try {
            JSONArray jArray = new JSONArray(result);
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json_data = jArray.getJSONObject(i);
                StudentDetails user = new StudentDetails();
                // user.setId(json_data.getInt("class_name"));
                // user.setClass_id(json_data.getInt("section_name"));
                // user.setSection_id(json_data.getInt("section_id"));
                user.setStudentname(json_data.getString("first_name"));
                user.setRollno(json_data.getString("roll_no"));
                users.add(user);
            }
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());
        }
        return users;
    }

    void addHeader() {

        tr = new TableRow(this);

        studentName = new TextView(this);


        studentName.setText("StudentName");

        studentName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        studentName.setPadding(5, 5, 5, 5);
        studentName.setBackgroundColor(Color.RED);
        LinearLayout Ll = new LinearLayout(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        params.setMargins(5, 5, 5, 5);

        Ll.addView(studentName, params);
        tr.addView((View) Ll);

        TextView RollNo = new TextView(this);
        RollNo.setText("RollNo");
        RollNo.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        RollNo.setPadding(5, 5, 5, 5);
        RollNo.setBackgroundColor(Color.RED);
        Ll = new LinearLayout(this);
        params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 5, 5);

        Ll.addView(RollNo, params);
        tr.addView((View) Ll);
        TextView Present = new TextView(this);
        Present.setText("Present");
        Present.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        Present.setPadding(5, 5, 5, 5);
        Present.setBackgroundColor(Color.RED);
        Ll = new LinearLayout(this);
        params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 5, 5);

        Ll.addView(Present, params);
        tr.addView((View) Ll);

        tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));



    }

    @SuppressWarnings({ "rawtypes" })
    public void addData(ArrayList<StudentDetails> users) {

        addHeader();

        for (Iterator i = users.iterator(); i.hasNext();) {

            StudentDetails p = (StudentDetails) i.next();

            /** Create a TableRow dynamically **/
            tr = new TableRow(this);

            /** Creating a TextView to add to the row **/
            studentNames[j] = new EditText(this);
            studentNames[j].setKeyListener(null);

            /*int[] ids = new int[]{R.id.studentname,R.id.rollnumber};//and so on

            for(intE id : ids){
                //EditText t = (EditText) findViewById(id);
                st=studentNames.add(Firstname.getText().toString());
            }
*/       


            studentNames[j].setText(p.getStudentname());
            studentNames[j].setId(p.getId());
            studentNames[j].setLayoutParams(new LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            studentNames[j].setPadding(5, 5, 5, 5);

            LinearLayout Ll = new LinearLayout(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
            params.setMargins(5, 2, 2, 2);

            Ll.addView(studentNames[j], params);
            tr.addView((View) Ll);

            stRoll1= new EditText(this);

            //stva = n.add(stRoll1.getText().toString());

            stRoll1.setKeyListener(null);
            stRoll1.setText(p.getRollno());
            stRoll1.setLayoutParams(new LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            stRoll1.setPadding(5, 5, 5, 5);

            Ll = new LinearLayout(this);
            params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 3, 3, 3);

            Ll.addView(stRoll1, params);
            tr.addView((View) Ll);


            CheckBox present = new CheckBox(this);

            present.setLayoutParams(new LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            present.setPadding(5, 5, 5, 5);

            Ll = new LinearLayout(this);
            params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 3, 3, 3);

            Ll.addView(present, params);
            tr.addView((View) Ll);

            tl.addView(tr, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

logcat

09-23 08:52:20.964: E/AndroidRuntime(2633): java.lang.NullPointerException
09-23 08:52:20.964: E/AndroidRuntime(2633):     at com.rushda.attendance.Attend$1.onClick(Attend.java:85)
09-23 08:52:20.964: E/AndroidRuntime(2633):     at android.view.View.performClick(View.java:4438)
09-23 08:52:20.964: E/AndroidRuntime(2633):     at android.view.View$PerformClick.run(View.java:18422)
09-23 08:52:20.964: E/AndroidRuntime(2633):     at android.os.Handler.handleCallback(Handler.java:733)
09-23 08:52:20.964: E/AndroidRuntime(2633):     at android.os.Handler.dispatchMessage(Handler.java:95)
09-23 08:52:20.964: E/AndroidRuntime(2633):     at android.os.Looper.loop(Looper.java:136)
09-23 08:52:20.964: E/AndroidRuntime(2633):     at android.app.ActivityThread.main(ActivityThread.java:5017)
09-23 08:52:20.964: E/AndroidRuntime(2633):     at java.lang.reflect.Method.invokeNative(Native Method)
09-23 08:52:20.964: E/AndroidRuntime(2633):     at java.lang.reflect.Method.invoke(Method.java:515)
09-23 08:52:20.964: E/AndroidRuntime(2633):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-23 08:52:20.964: E/AndroidRuntime(2633):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-23 08:52:20.964: E/AndroidRuntime(2633):     at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案