如果我有课程列表,如何将适配器设置为微调器ArrayList<HashMap<String, String>>
,我将显示它的继续信息
我应该使用什么类型的正确适配器
ArrayAdapter 应该可以工作。尝试使用字符串 []。
Spinner s = null;
final String[] choices = {"1","2"};
ArrayAdapter<String> a =new ArrayAdapter<String>(ObservationSubmit.this,android.R.layout.simple_spinner_item, choices);
a.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(a);
String[] arraySpinner = new String[] {
"1", "2", "3", "4", "5", "6", "7"
};
Spinner s = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arraySpinner);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.agreeta.firebasedatabasedemo.R;
import java.util.ArrayList;
import java.util.HashMap;
public class TestAdapter extends ArrayAdapter {
private Context mContext;
private ArrayList<HashMap<String, String>> hashMapArrayList = new ArrayList<>();
public TestAdapter(@NonNull Context context, ArrayList<HashMap<String, String>> list) {
super(context, 0 , list);
mContext = context;
hashMapArrayList = list;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItem = convertView;
if(listItem == null)
listItem = LayoutInflater.from(mContext).inflate(R.layout.like_row,parent,false);
TextView release = (TextView) listItem.findViewById(R.id.tv_title);
release.setText(hashMapArrayList.get(position).get("key"));
return listItem;
}
}
Try with this example. Here "key" is your HashMap key by which you can set value to row of spinner.