android 微调器不能 select 项目
android spinner can't select item
我有我的自定义基础适配器,我尝试在我的微调器中进行适配。
相对布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="47dp"
android:background="#ffffff" >
<TextView
android:id="@+id/spinnertxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="24dp"
android:text="TextView"
android:textColor="#777777"
android:textSize="17dp" />
<Button
android:id="@+id/button1"
android:layout_width="5dp"
android:layout_height="47dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#00b827" />
</RelativeLayout>
微调器:
<Spinner
android:id="@+id/pirsaxeobaspinner"
android:layout_width="match_parent"
android:layout_height="47dp"
android:background="@android:color/transparent"
android:drawSelectorOnTop="true"
android:popupBackground="#ffffff" />
基础适配器:
public class CustomSpinnerAdapter extends BaseAdapter {
private Context mContext;
private final ArrayList<String> menu_items_id;
UserHolder holder = null;
// private final int[] Imageid;
private static LayoutInflater inflater = null;
public CustomSpinnerAdapter(Context context, ArrayList<String> names) {
mContext = context;
// this.Imageid = Imageid;
this.menu_items_id = names;
inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return menu_items_id.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@SuppressLint("ViewHolder")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
holder = new UserHolder();
if (row == null) {
holder = new UserHolder();
row = inflater.inflate(R.layout.custom_spinner_adapter, null);
holder.textView = (TextView) row.findViewById(R.id.spinnertxt);
} else {
holder = (UserHolder) row.getTag();
}
holder.textView.setText(menu_items_id.get(position));
row.setTag(holder);
return row;
}
public class UserHolder {
public TextView textView;
}
}
主要java代码:
public class MyFragment extends Fragment {
public final static String TAG = MyFragment.class.getSimpleName();
private Spinner pirsaxeobaspinner;
private ArrayList<String> custom_adapter;
private CustomSpinnerAdapter my_adapter;
public static MyFragment newInstance() {
return new MyFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.ganayopiereba_fragment, container,
false);
pirsaxeobaspinner=(Spinner)rootView.findViewById(R.id.pirsaxeobaspinner);
custom_adapter=new ArrayList<String>();
custom_adapter.add("Door");
custom_adapter.add("Home");
my_adapter=new CustomSpinnerAdapter(getActivity(), custom_adapter);
pirsaxeobaspinner.setAdapter(my_adapter);
pirsaxeobaspinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
return rootView;
}
}
我可以在 Spinner but
中调整我的 ArrayList
我不能 ArrayList
中的 select 项目。
你需要改变这个:
@Override
public Object getItem(int position) {
return menu_items_id.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
我有我的自定义基础适配器,我尝试在我的微调器中进行适配。
相对布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="47dp"
android:background="#ffffff" >
<TextView
android:id="@+id/spinnertxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="24dp"
android:text="TextView"
android:textColor="#777777"
android:textSize="17dp" />
<Button
android:id="@+id/button1"
android:layout_width="5dp"
android:layout_height="47dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#00b827" />
</RelativeLayout>
微调器:
<Spinner
android:id="@+id/pirsaxeobaspinner"
android:layout_width="match_parent"
android:layout_height="47dp"
android:background="@android:color/transparent"
android:drawSelectorOnTop="true"
android:popupBackground="#ffffff" />
基础适配器:
public class CustomSpinnerAdapter extends BaseAdapter {
private Context mContext;
private final ArrayList<String> menu_items_id;
UserHolder holder = null;
// private final int[] Imageid;
private static LayoutInflater inflater = null;
public CustomSpinnerAdapter(Context context, ArrayList<String> names) {
mContext = context;
// this.Imageid = Imageid;
this.menu_items_id = names;
inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return menu_items_id.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@SuppressLint("ViewHolder")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
holder = new UserHolder();
if (row == null) {
holder = new UserHolder();
row = inflater.inflate(R.layout.custom_spinner_adapter, null);
holder.textView = (TextView) row.findViewById(R.id.spinnertxt);
} else {
holder = (UserHolder) row.getTag();
}
holder.textView.setText(menu_items_id.get(position));
row.setTag(holder);
return row;
}
public class UserHolder {
public TextView textView;
}
}
主要java代码:
public class MyFragment extends Fragment {
public final static String TAG = MyFragment.class.getSimpleName();
private Spinner pirsaxeobaspinner;
private ArrayList<String> custom_adapter;
private CustomSpinnerAdapter my_adapter;
public static MyFragment newInstance() {
return new MyFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.ganayopiereba_fragment, container,
false);
pirsaxeobaspinner=(Spinner)rootView.findViewById(R.id.pirsaxeobaspinner);
custom_adapter=new ArrayList<String>();
custom_adapter.add("Door");
custom_adapter.add("Home");
my_adapter=new CustomSpinnerAdapter(getActivity(), custom_adapter);
pirsaxeobaspinner.setAdapter(my_adapter);
pirsaxeobaspinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
return rootView;
}
}
我可以在 Spinner but
中调整我的 ArrayList
我不能 ArrayList
中的 select 项目。
你需要改变这个:
@Override
public Object getItem(int position) {
return menu_items_id.get(position);
}
@Override
public long getItemId(int position) {
return position;
}