У меня есть динамическое ListView
который использует ArrayAdapter
. Когда имя выбрано из счетчика, имени вместе со значком, показывающим, являются ли они штекером, или розетка добавляется к ListView
.
Главным образом все хорошо (имя добавляется к списку правильно, вместе со значком). Но значок, показывающий пол, добавляется к неправильному объекту в ListView
. Имя добавляется к нижней части списка, но значок помещается в имя наверху списка. Я не знаю, является ли это способ, которым я использую ViewHolder
но существует нулевая документация относительно него в веб-сайте Android.
// Listview inflater
inflater = (LayoutInflater) (this).getSystemService(LAYOUT_INFLATER_SERVICE);
// List Array.
mAdapter = new ArrayAdapter(this, R.layout.player_simple_list,
R.id.label, mStrings) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.i("ANDY","View getView Called");
// A ViewHolder keeps references to children views to
// avoid unneccessary calls to findViewById() on each row.
ViewHolder holder;
if (null == convertView) {
Log.i("ANDY","Position not previously used, so inflating");
convertView = inflater.inflate(R.layout.player_simple_list, null);
// Creates a ViewHolder and store references to the
// two children views we want to bind data to.
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.label);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
if (sexmale == true) {
holder.icon.setImageBitmap(maleicon);
}
else {
holder.icon.setImageBitmap(femaleicon);
}
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
holder.text.setText(getItem(position));
// Change icon depending is the sexmale variable is true or false.
Log.i("ANDY","getCount = "+mAdapter.getCount());
return convertView;
}
};
setListAdapter(mAdapter);
Обновление: ViewHolder
предназначен только для хранения ссылок на представления компонентов внутри макета элемента. Это помогает избежать накладных расходов на вызов findViewById
для визуализации каждого компонента внутри сложных макетов элементов с несколькими компонентами (например, TextView
и ImageView
в этом случае).
Я исправил это с помощью подпрограммы (называемой getSex
) для получения данных о поле и установки всех данных представления, включая значки, за пределами блоков if-else
.
Рабочий код теперь выглядит следующим образом:
if (null == convertView) {
Log.i("ANDY","Position not previously used, so inflating");
convertView = inflater.inflate(R.layout.player_simple_list, null);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.label);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
holder.text.setText(getItem(position));
// Change icon depending is the sexmale variable is true or false.
if (getSex (getItem(position)) == true) {
holder.icon.setImageBitmap(maleicon);
}
else {
holder.icon.setImageBitmap(femaleicon);
}
return convertView;
Вы должны перейти от if через несколько строк данных после комментария, как в этом вопросе
// Bind the data efficiently with the holder.
, поэтому он будет выглядеть так
if (null == convertView) {
Log.i("ANDY","Position not previously used, so inflating");
convertView = inflater.inflate(R.layout.player_simple_list, null);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
holder.text = (TextView) convertView.findViewById(R.id.label);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
if (sexmale == true) {
holder.icon.setImageBitmap(maleicon);
}
else {
holder.icon.setImageBitmap(femaleicon);
}
holder.text.setText(getItem(position));
Вы должны установить значки после if-else-if
для создания или связывания держателя
. В противном случае значки будут правильно отображаться только в первых нескольких элементах списка, т.е. до тех пор, пока ListView
не будет заполнен.
public View getView(int position, View convertView, ViewGroup parent) {
Log.i("ANDY","View getView Called");
// A ViewHolder keeps references to children views
// to avoid unneccessary calls to findViewById() on each row.
ViewHolder holder;
if (null == convertView) {
Log.i("ANDY","Position not previously used, so inflating");
convertView = inflater.inflate(R.layout.player_simple_list, null);
// Creates a ViewHolder and store references to
// the two children views we want to bind data to.
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.label);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
holder.text.setText(getItem(position));
// Change icon depending is the sexmale variable is true or false.
if (sexmale == true) {
holder.icon.setImageBitmap(maleicon);
}
else {
holder.icon.setImageBitmap(femaleicon);
}
Log.i("ANDY","getCount = "+mAdapter.getCount());
return convertView;
}