更改由 CustomAdapter(扩展 BaseAdapter)创建的微调器上的文本
Change the text on spinners created by a CustomAdapter (extends BaseAdapter)
我的应用程序创建了一个 ListView,它根据数组中的字符串数创建列表项。但是,我希望每个 Spinner 项目都选择了适当的项目。
示例:
String[] items = {"item1", "item2", "item3"}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] items = {"item1", "item2", "item3"}
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ListView listView = (ListView) findViewById(R.id.requestItems_listView);
listView.setAdapter(new CustomAdapter(this));
}
private class CustomAdapter extends BaseAdapter {
LayoutInflater inflater;
public CustomAdapter(Context context) {
inflater = LayoutInflater.from(context);
}
public int getCount() {
// TODO Auto-generated method stub
return items.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int arg0, View convertview, ViewGroup arg2) {
ViewHolder viewHolder;
if (convertview == null) {
convertview = inflater.inflate(R.layout.request_item_list_item, null);
viewHolder = new ViewHolder();
viewHolder.spinner = (Spinner) convertview.findViewById(R.id.itemSpinner);
viewHolder.spinner.setAdapter(adapter);
convertview.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertview.getTag();
}
return convertview;
}
public class ViewHolder {
Spinner spinner;
}
}
}
所以这应该在 ListView 中创建 3 个(因为 String[] "items" 中有 3 个项目)"request_item_list_item" 个项目。
每个微调器(request_item_list_item 的 1 个部分)都在位置 0,所以:
"item1"
"item1"
"item1"
不过我希望它执行以下操作:
"item1"
"item2"
"item3"
这会在 CustomAdapter 中完成吗?
奖金问题:
如果我希望用户能够按 "Add Button" 以将列表项的数量增加 1(这样就会有 4 request_item_list_item、5、6 等)是否有 .添加方法我应该使用?或者我只是更改 getCount 并刷新列表(这不是理想的......如果用户已经在 request_item_list_items 中填写了信息,那么他们将在刷新时丢失所有输入) ?
对于第一个问题,您只需要像这样更改 getView
:(如 I_A_Mok
评论的那样)
public View getView(int arg0, View convertview, ViewGroup arg2) {
ViewHolder viewHolder;
if (convertview == null) {
convertview = inflater.inflate(R.layout.request_item_list_item, null);
viewHolder = new ViewHolder();
viewHolder.spinner = (Spinner) convertview.findViewById(R.id.itemSpinner);
viewHolder.spinner.setAdapter(adapter);
convertview.setTag(viewHolder);
viewHolder.spinner.setSelection(arg0);
} else {
viewHolder = (ViewHolder) convertview.getTag();
}
return convertview;
}
第二个问题:
CustomAdapter myAdapter;
ArrayList<String> items;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
items = new ArrayList<>();
items.add("item1");
items.add("item2");
items.add("item3");
ListView listView = (ListView) findViewById(R.id.requestItems_listView);
myAdapter = new CustomAdapter(this, items);
listView.setAdapter(myAdapter);
Button bt_add_new = (Button)findViewById(R.id.bt_add_new);
bt_add_new.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
items.add("item" + items.size() + 1);
myAdapter.setItems(items);
myAdapter.notifyDataSetChanged();
}
});
}
private class CustomAdapter extends BaseAdapter {
LayoutInflater inflater;
private ArrayList<String> items;
public CustomAdapter(Context context, ArrayList<String> items) {
inflater = LayoutInflater.from(context);
this.items = items;
}
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int arg0, View convertview, ViewGroup arg2) {
ViewHolder viewHolder;
convertview = inflater.inflate(R.layout.request_item_list_item, null);
viewHolder = new ViewHolder();
viewHolder.spinner = (Spinner) convertview.findViewById(R.id.itemSpinner);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
viewHolder.spinner.setAdapter(adapter);
convertview.setTag(viewHolder);
return convertview;
}
public ArrayList<String> getItems() {
return items;
}
public void setItems(ArrayList<String> items) {
this.items = items;
}
public class ViewHolder {
Spinner spinner;
}
}
我的应用程序创建了一个 ListView,它根据数组中的字符串数创建列表项。但是,我希望每个 Spinner 项目都选择了适当的项目。
示例:
String[] items = {"item1", "item2", "item3"}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] items = {"item1", "item2", "item3"}
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ListView listView = (ListView) findViewById(R.id.requestItems_listView);
listView.setAdapter(new CustomAdapter(this));
}
private class CustomAdapter extends BaseAdapter {
LayoutInflater inflater;
public CustomAdapter(Context context) {
inflater = LayoutInflater.from(context);
}
public int getCount() {
// TODO Auto-generated method stub
return items.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int arg0, View convertview, ViewGroup arg2) {
ViewHolder viewHolder;
if (convertview == null) {
convertview = inflater.inflate(R.layout.request_item_list_item, null);
viewHolder = new ViewHolder();
viewHolder.spinner = (Spinner) convertview.findViewById(R.id.itemSpinner);
viewHolder.spinner.setAdapter(adapter);
convertview.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertview.getTag();
}
return convertview;
}
public class ViewHolder {
Spinner spinner;
}
}
}
所以这应该在 ListView 中创建 3 个(因为 String[] "items" 中有 3 个项目)"request_item_list_item" 个项目。
每个微调器(request_item_list_item 的 1 个部分)都在位置 0,所以:
"item1"
"item1"
"item1"
不过我希望它执行以下操作:
"item1"
"item2"
"item3"
这会在 CustomAdapter 中完成吗?
奖金问题: 如果我希望用户能够按 "Add Button" 以将列表项的数量增加 1(这样就会有 4 request_item_list_item、5、6 等)是否有 .添加方法我应该使用?或者我只是更改 getCount 并刷新列表(这不是理想的......如果用户已经在 request_item_list_items 中填写了信息,那么他们将在刷新时丢失所有输入) ?
对于第一个问题,您只需要像这样更改 getView
:(如 I_A_Mok
评论的那样)
public View getView(int arg0, View convertview, ViewGroup arg2) {
ViewHolder viewHolder;
if (convertview == null) {
convertview = inflater.inflate(R.layout.request_item_list_item, null);
viewHolder = new ViewHolder();
viewHolder.spinner = (Spinner) convertview.findViewById(R.id.itemSpinner);
viewHolder.spinner.setAdapter(adapter);
convertview.setTag(viewHolder);
viewHolder.spinner.setSelection(arg0);
} else {
viewHolder = (ViewHolder) convertview.getTag();
}
return convertview;
}
第二个问题:
CustomAdapter myAdapter;
ArrayList<String> items;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
items = new ArrayList<>();
items.add("item1");
items.add("item2");
items.add("item3");
ListView listView = (ListView) findViewById(R.id.requestItems_listView);
myAdapter = new CustomAdapter(this, items);
listView.setAdapter(myAdapter);
Button bt_add_new = (Button)findViewById(R.id.bt_add_new);
bt_add_new.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
items.add("item" + items.size() + 1);
myAdapter.setItems(items);
myAdapter.notifyDataSetChanged();
}
});
}
private class CustomAdapter extends BaseAdapter {
LayoutInflater inflater;
private ArrayList<String> items;
public CustomAdapter(Context context, ArrayList<String> items) {
inflater = LayoutInflater.from(context);
this.items = items;
}
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int arg0, View convertview, ViewGroup arg2) {
ViewHolder viewHolder;
convertview = inflater.inflate(R.layout.request_item_list_item, null);
viewHolder = new ViewHolder();
viewHolder.spinner = (Spinner) convertview.findViewById(R.id.itemSpinner);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
viewHolder.spinner.setAdapter(adapter);
convertview.setTag(viewHolder);
return convertview;
}
public ArrayList<String> getItems() {
return items;
}
public void setItems(ArrayList<String> items) {
this.items = items;
}
public class ViewHolder {
Spinner spinner;
}
}