我在下面的代码中使用 getView() 方法。我正在尝试遍历对象列表并获取它们的索引。在下面的代码中,我使用了一个 while 循环来执行此操作。但是,我没有得到预期的结果。我的列表包含 30 个对象,称为项目。我正在使用 while 循环并通过以下方法调用索引:items.get(j)
和items.indexOf(j)
. 我没有得到与列表中的索引相对应的整数,而是得到 -1 的索引或内存地址。大小为 30 的列表如何获得 -1 索引?请在下面查看我的代码。
问题1:如何打印出列表中每个对象的索引。
问题2:这种奇怪的行为是否与getview回收listview有关的内存泄漏?
package com.convention.notification.app;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class NewsRowAdapter extends ArrayAdapter<Item> {
private Activity activity;
private List<Item> items;
private Item objBean;
private int row;
private List<Integer> disable;
View view ;
public NewsRowAdapter(Activity act, int resource, List<Item> arrayList, List<Integer> disableList) {
super(act, resource, arrayList);
this.activity = act;
this.row = resource;
this.items = arrayList;
this.disable=disableList;
System.out.println("results of delete list a:"+disable.toString());
}
public long getItemId(int position){
return position;
}
public Item getItem(int position){
return null;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(row, null);
long id=getItemId(position);
//System.out.println(" at position "+position);
int j=0;
System.out.println("item size at "+items.size());
while (j< items.size())
{
System.out.println("while loop items.get(index): " +items.get(j) );
System.out.println( "while loop item.indexOf(index): "+items.indexOf(j) );
j++;
}
try{
// System.out.println("item size at "+items.size());
for(int k =0;k< items.size();k++){
// System.out.println("item index at "+items.get(k));
// System.out.println("value of index "+k);
// if(id == disable.get(k)){
// view.setBackgroundColor(Color.YELLOW);
// //System.out.println("background set to yellow at disable list "+disable.get(j));
// // System.out.println("background set to yellow at id "+id);
// break;
// } else {
// view.setBackgroundColor(Color.WHITE);
// // System.out.println("background set to white at position "+position);
// }
//
}
//}
}catch(IndexOutOfBoundsException e){
//System.out.println(" crash");
}
//ViewHolder is a custom class that gets TextViews by name: tvName, tvCity, tvBDate, tvGender, tvAge;
holder = new ViewHolder();
/* setTag Sets the tag associated with this view. A tag can be used to
* mark a view in its hierarchy and does not have to be unique
* within the hierarchy. Tags can also be used to store data within
* a view without resorting to another data structure.
*/
view.setTag(holder);
} else {
//the Object stored in this view as a tag
holder = (ViewHolder) view.getTag();
}
if ((items == null) || ((position + 1) > items.size()))
return view;
objBean = items.get(position);
holder.tv_event_name = (TextView) view.findViewById(R.id.tv_event_name);
holder.tv_event_date = (TextView) view.findViewById(R.id.tv_event_date);
holder.tv_event_start = (TextView) view.findViewById(R.id.tv_event_start);
holder.tv_event_end = (TextView) view.findViewById(R.id.tv_event_end);
holder.tv_event_location = (TextView) view.findViewById(R.id.tv_event_location);
if (holder.tv_event_name != null && null != objBean.getName()
&& objBean.getName().trim().length() > 0) {
holder.tv_event_name.setText(Html.fromHtml(objBean.getName()));
}
if (holder.tv_event_date != null && null != objBean.getDate()
&& objBean.getDate().trim().length() > 0) {
holder.tv_event_date.setText(Html.fromHtml(objBean.getDate()));
}
if (holder.tv_event_start != null && null != objBean.getStartTime()
&& objBean.getStartTime().trim().length() > 0) {
holder.tv_event_start.setText(Html.fromHtml(objBean.getStartTime()));
}
if (holder.tv_event_end != null && null != objBean.getEndTime()
&& objBean.getEndTime().trim().length() > 0) {
holder.tv_event_end.setText(Html.fromHtml(objBean.getEndTime()));
}
if (holder.tv_event_location != null && null != objBean.getLocation ()
&& objBean.getLocation ().trim().length() > 0) {
holder.tv_event_location.setText(Html.fromHtml(objBean.getLocation ()));
}
return view;
}
public class ViewHolder {
public TextView
tv_event_name,
tv_event_date,
tv_event_start,
tv_event_end,
tv_event_location
/*tv_event_delete_flag*/;
}
}
Logcat:请注意,此 logcat 输出在一次启动中重复大约 5 到 7 次。我在这里缩短了它。
06-11 19:58:17.471: I/AndroidRuntime(1347): NOTE: attach of thread 'Binder Thread #3' failed
06-11 19:58:18.541: I/ActivityManager(60): Displayed com.convention.notification.app/.DataView: +1s161ms
06-11 19:58:20.471: I/System.out(1355): item disalbed is at postion :1
06-11 19:58:20.471: I/System.out(1355): item disalbed is at postion :6
06-11 19:58:20.471: I/System.out(1355): item disalbed is at postion :14
06-11 19:58:20.471: I/System.out(1355): item disalbed is at postion :15
06-11 19:58:20.471: I/System.out(1355): item disalbed is at postion :18
06-11 19:58:20.471: I/System.out(1355): results of delete list :[1, 6, 14, 15, 18]
06-11 19:58:20.481: I/System.out(1355): results of delete list a:[1, 6, 14, 15, 18]
06-11 19:58:20.481: I/System.out(1355): set adapaer to list view called;
06-11 19:58:20.581: I/System.out(1355): item size at 30
06-11 19:58:20.581: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574210
06-11 19:58:20.581: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.581: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574488
06-11 19:58:20.581: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.581: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@405744e0
06-11 19:58:20.631: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.631: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574538
06-11 19:58:20.631: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.631: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574590
06-11 19:58:20.631: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.631: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@405745e8
06-11 19:58:20.642: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.642: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574640
06-11 19:58:20.642: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.642: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574698
06-11 19:58:20.642: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.642: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@405746f0
06-11 19:58:20.642: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.642: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574748
06-11 19:58:20.671: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.671: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@405747c0
06-11 19:58:20.671: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.681: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574818
06-11 19:58:20.681: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.681: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574870
06-11 19:58:20.681: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.681: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574928
06-11 19:58:20.681: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.721: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574980
06-11 19:58:20.721: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.721: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@405749d8
06-11 19:58:20.721: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.731: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574a30
06-11 19:58:20.731: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.731: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574a88
06-11 19:58:20.731: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.731: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574ae0
06-11 19:58:20.761: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.761: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574bb8
06-11 19:58:20.761: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.761: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574c30
06-11 19:58:20.761: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.761: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574c88
06-11 19:58:20.761: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.771: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574ce0
06-11 19:58:20.781: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.781: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574d38
06-11 19:58:20.811: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.811: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574d90
06-11 19:58:20.811: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.811: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574de8
06-11 19:58:20.811: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.811: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574e40
06-11 19:58:20.811: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.811: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574e98
06-11 19:58:20.811: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.811: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574fa8
06-11 19:58:20.811: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.811: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40575000
06-11 19:58:20.811: I/System.out(1355): while loop item.indexOf(index): -1