2017-01-05 13 views
1

私はスピナー用のカスタムアダプターを作成しました。スピナーはリストから名前を表示する必要があります。以下は、私のコードです:ここではスピナーが正しいデータを表示していません

empNameList = new ArrayList<HashMap<String, String>>(); 
       JSONArray jsonArray = response.getJSONArray("Salon"); 
       for (int i = 0; i < jsonArray.length(); i++) { 
        JSONObject jsonObject = jsonArray.getJSONObject(i); 
        String empName = jsonObject.getString("employeename"); 
        String empId = jsonObject.getString("employee_id"); 
        HashMap<String, String> hashMap = new HashMap<String, String>(); 
        hashMap.put("empId", empId); 
        hashMap.put("empName", empName); 
        empNameList.add(hashMap); 
       }  
employeeAdapter = new EmployeeAdapter(getApplicationContext(), R.layout.service_list, empNameList); 
        employeeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
        spinner.setAdapter(employeeAdapter); 

は私のアダプタクラスです:

public class EmployeeAdapter extends ArrayAdapter<HashMap<String, String>> { 
ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>(); 
Context context; 
int layoutResourceId; 
HashMap<String, String> hashmap; 

public EmployeeAdapter(Context context, int layoutResourceId, ArrayList<HashMap<String, String>> arrayList) { 
    super(context, layoutResourceId, arrayList); 
    this.arrayList = arrayList; 
    this.context = context; 
    this.layoutResourceId = layoutResourceId; 

} 

@Override 
public View getView(int position, View convertView, ViewGroup parrent) { 
    View view = convertView; 
    final ViewHolder holder; 

    if (view == null) { 
     LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     view = layoutInflater.inflate(layoutResourceId, parrent, false); 
     holder = new ViewHolder(); 
     holder.textView42 = (TextView) view.findViewById(R.id.textView42); 
     view.setTag(holder); 
    } else { 

     holder = (ViewHolder) view.getTag(); 
    } 
    if (position % 2 == 0) { 
     view.setBackgroundColor(context.getResources().getColor(R.color.pink)); 
    } else { 
     view.setBackgroundColor(Color.WHITE); 
    } 
    hashmap = new HashMap<String, String>(); 
    hashmap = arrayList.get(position); 
    System.out.println("empName = " + hashmap.get("empName")); 
    holder.textView42.setText(hashmap.get("empName")); 
    return view; 
} 

static class ViewHolder { 
    TextView textView42; 
} 

スピナーはempNameListの名前、ハッシュマップを表示していません。どんな助けでも感謝します。

+0

SpinnerがempNameListの名前ではなくハッシュマップを表示しているとはどういう意味ですか? –

+0

スピナーは スニルSangeeta ...... ようなデータを表示しなければならないしかし、それは {のempName =スニル、EMPID = 2} { のempName = sangeeta、EMPID = 3}のような –

+0

データ表示されている ":" - コロン? – Prabs

答えて

1

アダプタクラスでは、以下のメソッドをオーバーライドするだけです。

public View getDropDownView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View view = layoutInflater.inflate(layoutResourceId, parent, false); 
    // View row = inflater.inflate(yourRowlayout, parent,false); 
    TextView make = (TextView) view.findViewById(R.id.textviewname); 
    view.setBackgroundColor(Color.GRAY); 
    make.setText(arrayList.get(position).get("empName")); 
    return view; 
} 
+0

はい!それは動作します。 :) – Raghavendra

+0

それは働いた。 :)ありがとうございます:) –

+0

今、リストから項目を選択すると、スピナーにhashmapが表示されています。 –

関連する問題