我需要帮助才能将ViewHolder实现到我的ListView。
...
ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, array_spinner);//Inserisco le possibilita
mylist = (ListView) findViewById(R.id.listView1);
mylist.setAdapter(adapter);
mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
...
有人可以帮帮我吗?
答案 0 :(得分:1)
public class YourAdapter extends ArrayAdapter<String> {
Context context;
int layoutResourceId;
String data[] = null;
public YourAdapter(Context context, int layoutResourceId, String[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.txtTitle = (TextView)row.findViewById(R.id.row_textview);
row.setTag(holder);
}
else
{
holder = (ViewHolder)row.getTag();
}
String title = data[position];
holder.txtTitle.setText(title);
return row;
}
static class ViewHolder
{
TextView txtTitle;
}
}
并在您的活动中执行此操作:
YourAdapter adapter = new YourAdapter<>(this,R.layout.your_custom_layout, your_string_array);
mylist = (ListView) findViewById(R.id.listView1);
mylist.setAdapter(adapter);