由于某种原因,getView()方法不被称为全额。它应该被称为10次。但它不是......
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
res = getResources();
searchField = (EditText) findViewById(R.id.EditText01);
list = (ListView) findViewById(android.R.id.list);
// button = (Button)findViewById(R.id.btnFriendList);
friendList = new ArrayList<Friend>();
nameBlock = res.getStringArray(R.array.names);
descBlock = res.getStringArray(R.array.descriptions);
//imageBlock = res.getIntArray(R.array.images);
int size = nameBlock.length;
for(int i = 0 ; i < size; i++) {
Log.d("FREINDADD", "Freind Added" + i);
friendList.add(new Friend(nameBlock[i], descBlock[i], imageBlock[i]));
}
Log.i("Application", "Application started succesfully...");
setListAdapter(new VirtuAdapter(this));
searchField.addTextChangedListener(new TextWatcher()
{
@Override public void afterTextChanged(Editable s) {}
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override public void onTextChanged(CharSequence s, int start, int before, int count)
{
friendList.clear();
textlength = searchField.getText().length();
for (int i = 0; i < nameBlock.length; i++)
{
if (textlength <= nameBlock[i].length())
{
if(nameBlock[i].toLowerCase().contains(searchField.getText().toString().toLowerCase().trim())) {
Log.i("VirtuFriendList", "List recyling in process... ");
friendList.add(new Friend(nameBlock[i], descBlock[i], imageBlock[i]));
}
}
}
AppendList(friendList);
}
});
}
public void AppendList(ArrayList<Friend> freind) {
setListAdapter(new VirtuAdapter(this));
}
public class VirtuAdapter extends BaseAdapter
{
Activity content;
public VirtuAdapter(Activity context)
{
this.content = context;
}
@Override
public int getCount()
{
return friendList.size();
}
@Override
public Object getItem(int position)
{
return friendList.get(position);
}
@Override
public long getItemId(int position)
{
return friendList.size();
}
class ViewHolder {
TextView myTitle;
TextView myDescription;
ImageView myImage;
ViewHolder(View view) {
myImage = (ImageView)view.findViewById(R.id.imageview);
myTitle = (TextView)view.findViewById(R.id.title);
myDescription = (TextView)view.findViewById(R.id.mutualTitle);
}
}
@Override
public View getView(int position, View view, ViewGroup parent)
{
View row = view;
ViewHolder holder = null;
if(row == null)
{
// If it is visible to the user, deploy the row(s) - allocated in local memory
LayoutInflater inflater = (LayoutInflater)content .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.search_list_item, parent, false);
holder = new ViewHolder(row);
row.setTag(holder);
Log.d("VIRTU", "Row deployed...");
}
else
{
// Recycle the row if it is not visible to to the user - store in local memory
holder = (ViewHolder)row.getTag();
Log.d("VIRTU", "Row recycled...");
}
Friend temp = friendList.get(position);
// Set the resources for each component in the list
holder.myImage.setImageResource(temp.getImage());
holder.myTitle.setText(temp.getName());
holder.myDescription.setText(temp.getDesc());
return row;
}
}
每个字符串数组中有10个元素。结果是,应该在ListView中创建10个元素......但是,只有5个元素占优势......
有人有什么建议吗? logcat的:
D/VIRTU(788): Row deployed...
I/System.out(788): SIZESIZE:10
D/VIRTU(788): SIZE:10
D/VIRTU(788): Row deployed...
I/System.out(788): SIZESIZE:10
D/VIRTU(788): SIZE:10
06-14 03:01:52.546: D/VIRTU(788): Row deployed...
06-14 03:01:52.546: I/System.out(788): SIZESIZE:10
06-14 03:01:52.546: D/VIRTU(788): SIZE:10
06-14 03:01:52.565: D/VIRTU(788): Row deployed...
06-14 03:01:52.565: I/System.out(788): SIZESIZE:10
06-14 03:01:52.565: D/VIRTU(788): SIZE:10
06-14 03:01:52.585: D/VIRTU(788): Row deployed...
答案 0 :(得分:0)
试试这种方式,希望这可以帮助您解决问题。
public class VirtuAdapter extends BaseAdapter
{
private Context content;
private ArrayList<Friend> friendList;
public VirtuAdapter(Context context,ArrayList<Friend> friendList)
{
this.content = context;
this.friendList = friendList;
}
@Override
public int getCount()
{
return friendList.size();
}
@Override
public Object getItem(int position)
{
return friendList.get(position);
}
@Override
public long getItemId(int position)
{
return friendList.size();
}
class ViewHolder {
TextView myTitle;
TextView myDescription;
ImageView myImage;
}
@Override
public View getView(int position, View view, ViewGroup parent)
{
ViewHolder holder;
if(view == null)
{
holder = new ViewHolder();
view = LayoutInflater.from(content).inflate(R.layout.search_list_item, null, false);
holder.myImage = (ImageView)view.findViewById(R.id.imageview);
holder.myTitle = (TextView)view.findViewById(R.id.title);
holder.myDescription = (TextView)view.findViewById(R.id.mutualTitle);
view.setTag(holder);
}
else
{
holder = (ViewHolder)view.getTag();
}
holder.myImage.setImageResource(friendList.get(position).getImage());
holder.myTitle.setText(friendList.get(position).getName());
holder.myDescription.setText(friendList.get(position).getDesc());
return view;
}
}