【发布时间】:2014-06-14 06:37:37
【问题描述】:
由于某种原因,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 个占上风...
有人有什么建议吗? 日志猫:
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...
【问题讨论】:
-
顺便说一句,在任何人面前。注释掉的“imageBlock[i]”...“imageBlock = res...”还有一个数组,提供对每 10 个图像资源的引用...
-
调试你的代码并在
getCount方法中检查friendList的大小 -
正确检查
getItem返回的 10 或 5。 -
@shayanpourvatan 我刚刚添加了大约 100 个元素,但它仍然只部署了 5 个。我现在将使用 getCount 对其进行调试,并尽快回复您。
-
适配器只创建与当前屏幕上一样多的视图,然后在您向下滚动时创建更多视图。滚动时是否正在创建其他视图?
标签: android