2016-04-13 8 views
0

私はsqliteデータベースから子供の名前を入力する必要があるスピナーを持っています。私は子供のリストを返す方法があります。リスト項目のテキストプロパティをスピナーにリストする方法

public List<ChildUser> getChildren() { 
     List<ChildUser> childList = new ArrayList<ChildUser>(); 

     //select columns 
     String[] tableColumns = new String[]{"id", "childname", "nickname"}; 

     Cursor cursor = db.query("child", tableColumns, "parent =? ", new String[]{_parentName}, null, null, null); 
     cursor.moveToFirst(); 

     while (!cursor.isAfterLast()) { 
      ChildUser child = new ChildUser(); 
      //take values from database 
      child.setId(cursor.getInt(0)); 
      child.setChildName(cursor.getString(1)); 
      child.setNickName(cursor.getString(2)); 


      //add to db 
      childList.add(child); 

      cursor.moveToNext(); 
     } 
     return childList; 
    } 

私はその後、スピナーを埋めるためにフラグメントからこのメソッドを呼び出して、しかし、私はどのように私は、スピナーで私の選択した項目のテキストプロパティを表示することができ、たchildList dispayedの別のプロパティを取得していますか?私も、私はそれを適用することができるかどうイムわからない、カスタムリストアダプタを持つスピナー

を見るにはここをクリックしてください---

final ChildDataBase cdb = new ChildDataBase(getActivity()); 

     final List<ChildUser> childList = cdb.getChildren(); 



     Spinner spinner = (Spinner) v.findViewById(R.id.del_child_spinner); 
     ArrayAdapter<ChildUser> spinnerAdapter = new ArrayAdapter<ChildUser>(getActivity(), android.R.layout.simple_spinner_item, childList); 
     spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spinner.setAdapter(spinnerAdapter); 
     spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 

       childId = childList.get(position).getId(); 

       Toast succ = Toast.makeText(Home.super.getContext(), childList.get(position).getChildName() + " Selected.", Toast.LENGTH_SHORT); 
       succ.show(); 


       return; 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> parent) { 
       Toast succ = Toast.makeText(Home.super.getContext(), "No child selected.", Toast.LENGTH_SHORT); 
       succ.show(); 
      } 
     }); 

Spinner <(まさに私のスピナーディスプレイを見るためにリンクをクリックしてください)何とかこの作業を完了させるために。

/** 
* Created by Victor on 4/3/2016. 
*/ 
public class ChildListAdapter extends ArrayAdapter<ChildUser> { 

    private FragmentTransaction fragmentTransaction; 
    private android.support.v4.app.FragmentManager fragmentManager; 
    private final Context context; 

    private final List<ChildUser> childList; 

    public ChildListAdapter(Context context, List<ChildUser> childList){ 
     super(context, R.layout.fragment_child_list_layout, childList);//fragment_child_list 
     this.context = context; 
     this.childList= childList; 
    } 
    @Override 
    public View getView(final int position, View convertView, ViewGroup parent){ 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View rowView = inflater.inflate(R.layout.fragment_child_list_layout,parent,false); 


     Button childName = (Button)rowView.findViewById(R.id.childButton); 
     childName.setText(childList.get(position).getNickName()); 

     childName.setOnClickListener(new View.OnClickListener() { 


      public void onClick(View v) { 
       String selectedChild = childList.get(position).getChildName(); 
       // do something with selected item 
       Toast fail = Toast.makeText(ChildListAdapter.super.getContext(), selectedChild + " selected", Toast.LENGTH_SHORT); 
       fail.show(); 
       Globals._selectChild = selectedChild; 
       TaskList taskList = new TaskList(); 
       fragmentTransaction = ((FragmentActivity)context).getSupportFragmentManager().beginTransaction(); 
       fragmentTransaction.replace(R.id.fragholder, taskList); 
       fragmentTransaction.commit(); 

      } 
     }); 


     return rowView; 


    } 




} 

これは、あなたのクラスの、toString()メソッドによって返された文字列を印刷している私のChildUserオブジェクト

public class ChildUser { 
    String childName; 
    String nickName, gender, parent; 
    int id, age; 

    public void setChildName(String childName) 
    { 
     this.childName = childName; 
    } 
    public String getChildName() 
    { 
     return this.childName; 
    } 
    public void setNickName(String nickName) 
    { 
     this.nickName = nickName; 
    } 
    public String getNickName() 
    { 
     return this.nickName; 
    } 
    public void setGender(String gender) 
    { 
     this.gender = gender; 
    } 
    public String getGender() 
    { 
     return this.gender; 
    } 
    public void setParent(String parent) 
    { 
     this.parent = parent; 
    } 
    public String getParent() 
    { 
     return this.parent; 
    } 
    public void setId(int id) 
    { 
     this.id = id; 
    } 
    public int getId() 
    { 
     return this.id; 
    } 
    public void setAge(int age) 
    { 
     this.age = age; 
    } 
    public int getAge() 
    { 
     return this.age; 
    } 
} 
+0

私はあなたのコンストラクタで 'super'を呼び出すので、これが起こると思います。コメントしてください&私のchildlistadapterの – yennsarah

+0

を参照してください?私がそれをコメントアウトするとエラーがたくさん出る – vhdz04

+0

sqliteに保存されているデータをスピナーに入れたいのですか –

答えて

1

です。

toString()をオーバーライドして正しい値を返すことができます。そうでない場合は、独自のカスタムアダプターを実装するか、モデルリストをarrayAdapterに渡す代わりに正しいプロパティーを使用して文字列配列を作成する必要があります。

最も簡単な()は、このことができます

public class ChildUser { 
    String childName; 
    String nickName, gender, parent; 
    int id, age; 

    @Override 
    public String toString() 
    { 
     return this.childName; 
    } 
/*.......*/ 
} 

希望をのtoStringをオーバーライドすることです。

+0

私はそれが簡単だと信じていない!どうもありがとうございます。私はあなたの答えをupvoteするが、まだそこにいない希望:/ – vhdz04

関連する問題