0

私は以下のコードのgetView()メソッドで作業しています。オブジェクトのリストを繰り返し処理して、それらのインデックスを取得しようとしています。以下のコードでは、これを行うためにwhileループを使用しています。しかし、期待した結果が得られていません。30個のオブジェクトを保持し、アイテムと呼ばれるマイリスト。私はwhileループを使用しており、次のメソッドでインデックスを呼び出しています:items.get(j)およびitems.indexOf(j)。リスト内のインデックスに対応する整数を取得する代わりに、インデックスまたはメモリアドレスに対して-1を取得します。サイズ30のリストが-1のインデックスを持つことはどのように可能ですか?以下の私のコードをご覧ください。

質問1:リスト内の各オブジェクトのインデックスを印刷するにはどうすればよいですか。

質問2:この奇妙な動作は、getviewがリストビューをリサイクルすることに関係するメモリリークですか?

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出力は、1回の起動で約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
4

2 に答える 2

0

関数はオブジェクトを受け取り、indexOfリストと同じタイプのオブジェクトである必要があります。items.indexOf(items.get(index))正しい構文もそうです。詳細については、 http://docs.oracle.com/javase/7/docs/api/java/util/List.html#indexOf%28java.lang.Object%29を参照してください。

于 2012-06-11T20:33:34.820 に答える
0

関数items.get()は、要求した文字列値が見つからない場合、-1を返します。したがって、-1は無効なインデックスであるため、関数indexOfと直接組み合わせると、これは危険な場合があります。別のコーディング手法であるListIteratorを使用することをお勧めします。シンプルで安全なので一般的です。オブジェクトアイテムを使用した簡単な例を次に示します。

Iterator it = items.iterator();
while (it.hasNext()) {
   System.out.println(it.next());
}

見た目がいかにシンプルで安全かをご覧ください。イテレータは、更新の途中ではなく、データの入力/更新の後でいつでも呼び出されます。あなたはインターネットから多くのサンプルを見つけることができます。

于 2012-06-11T22:17:14.843 に答える