mardi 24 novembre 2015

Make spinner display items even no editText value is inserted

In Fragment A, I have 2 editText (W1,W2) and 2 spinner(a1,a2) which has items {1,2,3}.

The editText value and spinner item will be inserted into SQLite. Before value get inserted into SQLite, I have make a condition to check whether W2 consits of a value. If not, it only insert W1 and a1.

Fragment A

 Button btn1 = (Button) findViewById(R.id.button2);
 btn1.setOnClickListener(new View.OnClickListener() {
     public void onClick(View arg0) {
     AlertDialog.Builder builder = new AlertDialog.Builder(WorkDetailsTable.this);
     builder.setTitle("Data Saved");
     builder.setMessage("Are you sure you want to save?");
     builder.setIcon(android.R.drawable.ic_dialog_alert);
     builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int ii) 
     {
          if ((W1 != null && W1.trim().length() > 0) && ((W1 != null && W1.trim().length() > 0))
         {
            a1 = spinnerTra.getSelectedItem().toString();
            a2= spinnerTra2.getSelectedItem().toString();
            W1=txtWork1.getText().toString();
            W2=txtWork2.getText().toString();
            WD.insertWorkDetails(a1, W1);
            WD.insertWorkDetails(a2, W2, );
           } 
           else if ((TextUtils.isEmpty(W2)) 
          {
              WD.insertWorkDetails(a1, W1);  
           }

And in Fragment B, I have 2 editText(wd1,wd2) and 2 spinner(project1,project2). Fragment B will retrieved all the values from SQLite.

 public void RetrievePage(String date, String id) {
 final String date2=date;
 final String id2 = id;
 database = dbHelper.getWritableDatabase();
 c = database.rawQuery("SELECT wd.Project, wd.WorkDescription FROM Information i LEFT JOIN WorkDetails wd ON wd.Twf_id=i._id WHERE i.Name = ? AND i._id= ? ",
                        new String[]{String.valueOf("Lim"),String.valueOf(id2)}, null);
  Details WD = new Details();
  int rowNum=1;
  if (c != null) {
      while (c.moveToNext()) {
       switch (rowNum) {
       case 1:
         Project11 = c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Project));
         addProject1(Project11);
         WD.setProject(Project11);
         Log.d("TAG", Project11);
                    WorkDescrption1=c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.WorkDescrition));
          WD.setWorkDescription(WorkDescrption1);
          wd1.setText(WorkDescrption1);

         case 2:
           WorkDescription2 = c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.WorkDescription));
            Log.e("TAG",WorkDescription2);
            Project22 = c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Project));
             addProject2(Project22);
             WD.setProject(Project22);
             WD.setWorkDescription(WorkDescription2);
             wd2.setText(WorkDescription2);
             break;
              }
              rowNum += 1;
               }
              c.close();
               }
                }

  public void addProject1(String l)
      {
          String[] arr = new String[]{"1","2","3"};
            List<String> list = new ArrayList<String>();
            String project11 = l;
            list.add(project11);
            for(String s:arr){
                if(!list.contains(s)){
                    list.add(s);
                }
            }
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, list);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            project1.setAdapter(adapter);
        }

        public void addProject2(String d)
        {
            String[] arr = new String[]{"1","2","3"};
            List<String> list = new ArrayList<String>();
            String project22 = d;
            list.add(project22);
            for(String s:arr){
                if(!list.contains(s)){
                    list.add(s);
                }
            }
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, list);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            project2.setAdapter(adapter);
        }

For Spinner project1 and project2, it will display the inserted value first. Eg, in fragment A, spinner a1 insert value 1 and spinner a2 insert 3. So in fragment B, project1 will display 1 first and project2 display 3 first.

How can I make the project2 display {1,2,3} in fragment B? Nothing is displayed on project2 if no value is inserted in fragment A . Where should I move the addProject2(Project22); ? thanks

Aucun commentaire:

Enregistrer un commentaire