0

CustomAdapterが同じことをループし続けるのは正常なのか気になりますか?たとえば、データベースから3行のレコードがあり、それらをリストに表示したいとします。customAdapterを使用して表示すると、同じものが3回ループします。つまり、レコードが10行ある場合、10回*10レコード=100回のループになります。このような場合、私のプログラムは確かにメモリを使い果たします。これに対する解決策はありますか?以下のようにログを添付しました。「~~行ID~~0」に気付いた場合は、同じように3回ループし続けます。

 public class ViewMsgMain extends ListActivity{

// Retrieve member id from local database
String memberid = FypGeneral.LOGINMEMBERID;
SharedPreferences sp_memberid;
int memid;

String sender_id="", content_type = "", content_path = "", content_id = "", imageaudio_id = "", content_date="";
String over_contentid = "", uploaded_content_id = "", receiver_id = "", read_status = "", 
        sender_member_image="", imageAudio_image="";
InputStream inputstream = null;
StringBuilder stringbuilder = null;
String result = null;

//  retrieved message
String retrieveMsgId[] , uploaded_content_type[];
private LayoutInflater mInflater;
private Vector<RowData> data;
RowData rd;
CustomAdapter adapter;
int pos=1;
String content_type_split[];
String content_date_split[];
String sender_member_image_split[], uploaded_content_id_split[];
String content_path_split[], content_id_split[];
String imageAudio_image_split[];

Bitmap bitmap;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewmsglist);

    //===================================================
    // Get member id from local database
    sp_memberid = getSharedPreferences(memberid, MODE_PRIVATE);
    if(sp_memberid.contains("memberid")==true)
    {memid = sp_memberid.getInt("memberid", 0);}
    Log.e("homepage member id == ", "~ "+memid);
    //===================================================

    try{
        //http post
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://localhost/project/viewmessage.php?memberid="+memid);
        //httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); //encode a list of NameValuePair objects suitable for HTTP calls
        HttpResponse response = httpclient.execute(httppost); // to make an HTTPPOST call with the HttpClient
        HttpEntity entity = response.getEntity();
        inputstream = entity.getContent();
    }
    catch(Exception e){
        Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show();
    }

    //Convert response to string  
    try
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputstream,"UTF-8"));

        stringbuilder = new StringBuilder();

        String line = null;

        while ((line = reader.readLine()) != null) 
        {
            stringbuilder.append(line + "\n");
        }

        inputstream.close();

        result = stringbuilder.toString();
    }
    catch(Exception e)
    {
        Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show();
    }
    //END Convert response to string   
    try{
        JSONArray jArray = new JSONArray(result);
        JSONObject json_data=null;
        if(jArray.length() == 0)
        {
            Toast.makeText(getApplicationContext(), "Tiada mesej", Toast.LENGTH_SHORT).show();
        }
        else
        {
            for(int i=0;i<jArray.length();i++)
            {
                json_data = jArray.getJSONObject(i);
                content_type = content_type + json_data.getString("uploadedcontenttype")+",";
                content_path = content_path+"http://localhost/project/"+(String) json_data.getString("contentpath")+",";
                imageAudio_image = imageAudio_image + json_data.getString("imageaudiopath")+",";
                //r.add(json_data.getString("member_id") + json_data.getString("member_name") + json_data.getString("member_usernamepath"));
                content_id = content_id + json_data.getString("contentid")+",";
                imageaudio_id = imageaudio_id + json_data.getString("imageaudioid")+",";
                content_date = content_date + json_data.getString("contentdate")+",";
                over_contentid = over_contentid + json_data.getString("overallid")+",";
                uploaded_content_id = uploaded_content_id +  json_data.getString("uploadedcontentid")+",";
                sender_id = sender_id +  json_data.getString("senderid")+",";
                receiver_id = receiver_id + json_data.getString("receiverid")+",";
                read_status =read_status + json_data.getString("readstatus")+",";
                sender_member_image = sender_member_image + "http://localhost/project/www/"+json_data.getString("memberimage")+",";
            }
        }
        Log.e("retrieved ", "~ "+content_type + "@ " +  " # "+ content_path + " $ "+ sender_id);
    }
    catch(JSONException e1){
        Toast.makeText(getApplicationContext(), "Tiada mesej", Toast.LENGTH_SHORT).show();
        Log.e("erroe ", e1.toString() );
        //Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show();
    } catch (ParseException e1) {
        Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show();
    }

    // Split the data retrieved from database
    content_type_split = content_type.split(",");
    content_path_split = content_path.split(",");
    content_id_split = content_id.split(",");
    content_date_split = content_date.split(",");
    sender_member_image_split = sender_member_image.split(",");
    uploaded_content_id_split = uploaded_content_id.split(",");
    imageAudio_image_split = imageAudio_image.split(",");

    mInflater = (LayoutInflater) getSystemService(
            Activity.LAYOUT_INFLATER_SERVICE);
    data = new Vector<RowData>();
    for(int i=0;i<content_type_split.length;i++){

        try {
            rd = new RowData(i,content_type_split[i],content_date_split[i]);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        data.add(rd);
    }
    CustomAdapter adapter = new CustomAdapter(this, R.layout.list,
            R.id.title, data);
    setListAdapter(adapter);
    //getListView().setTextFilterEnabled(true);
}

public void onListItemClick(ListView parent, View v, int position,
        long id) {
    // Show details in another page
    Intent viewMsgIntent = new Intent(ViewMsgMain.this, ViewMsgDetails.class);
    viewMsgIntent.putExtra("content_type", content_type_split[position]);
    viewMsgIntent.putExtra("content_path", content_path_split[position]);
    viewMsgIntent.putExtra("sender_member_image", sender_member_image_split[position]);
    viewMsgIntent.putExtra("imageAudio_image", imageAudio_image_split[position]);
    startActivity(viewMsgIntent);
}
private class RowData {
    protected int mId;
    protected String mTitle;
    protected String mDetail;
    RowData(int id,String title,String detail){
        mId=id;
        mTitle = title;
        mDetail=detail;
    }
    @Override
    public String toString() {
        Log.e("rowdata",mId+" "+mTitle+" "+mDetail);
        return mId+" "+mTitle+" "+mDetail;
    }
}
private class CustomAdapter extends ArrayAdapter<RowData> {

    public CustomAdapter(Context context, int resource,
            int textViewResourceId, List<RowData> objects) {               

        super(context, resource, textViewResourceId, objects);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {   

        ViewHolder holder = null;
        TextView title = null;
        TextView detail = null;
        ImageView i11=null;
        ImageView i112=null;
        RowData rowData= getItem(position);
        Log.e("content_type_split.length ", "## "+content_type_split.length+"   ~~ row id~~   "+ rowData.mId);
        if(null == convertView){
            convertView = mInflater.inflate(R.layout.list, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder = (ViewHolder) convertView.getTag();
        title = holder.gettitle();
        title.setText(rowData.mTitle);
        detail = holder.getdetail();
        detail.setText(rowData.mDetail);                                                     

        i11=holder.getImage();
        if(content_type_split[rowData.mId].equals("1"))
        {
            i11.setImageResource(R.drawable.imageicon2);
        }
        else if(content_type_split[rowData.mId].equals("2"))
        {
            i11.setImageResource(R.drawable.audioicon2);
        }
        else if(content_type_split[rowData.mId].equals("3"))
        {
            i11.setImageResource(R.drawable.videoicon2);
        }

        Log.e("get view ," , " ~ "+sender_member_image_split[rowData.mId]);

        i112=holder.getImage2();
        try {
            Log.e("enter bitmap ", "yes");
            bitmap = BitmapFactory.decodeStream((InputStream)new URL(sender_member_image_split[rowData.mId]).getContent());
            i112.setImageBitmap(bitmap);

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return convertView;
    }
    private class ViewHolder {
        private View mRow;
        private TextView title = null;
        private TextView detail = null;
        private ImageView i11=null;
        private ImageView i112=null; 

        public ViewHolder(View row) {
            mRow = row;
        }
        public TextView gettitle() {
            if(null == title){
                title = (TextView) mRow.findViewById(R.id.title);
            }
            return title;
        }     

        public TextView getdetail() {
            if(null == detail){
                detail = (TextView) mRow.findViewById(R.id.detail);
            }
            return detail;
        }
        public ImageView getImage() {
            if(null == i11){
                i11 = (ImageView) mRow.findViewById(R.id.img);
            }
            return i11;
        }

        public ImageView getImage2() {
            if(null == i112){
                i112 = (ImageView) mRow.findViewById(R.id.img2);

            }
            return i112;
        }
    }
}

}

ログ

            05-15 21:59:52.660: E/content_type_split.length(7388): ## 3   ~~ row id~~   0
            05-15 21:59:52.670: D/dalvikvm(7388): GC_CONCURRENT freed 352K, 11% free 9318K/10439K, paused 2ms+3ms
            05-15 21:59:52.690: E/get view ,(7388):  ~ http://localhost/project/www/username/kids.jpg
            05-15 21:59:52.690: E/enter bitmap(7388): yes
            05-15 21:59:52.720: W/System.err(7388): Error reading from ./org/apache/harmony/awt/www/content/image/jpeg.class
            05-15 21:59:52.740: E/content_type_split.length(7388): ## 3   ~~ row id~~   1
            05-15 21:59:52.740: E/get view ,(7388):  ~ http://localhost/project/www/username/1_2012512080548_DSC00701.JPG
            05-15 21:59:52.740: E/enter bitmap(7388): yes
            05-15 21:59:52.790: W/System.err(7388): Error reading from ./org/apache/harmony/awt/www/content/image/jpeg.class
            05-15 21:59:52.810: D/dalvikvm(7388): GC_FOR_ALLOC freed 562K, 14% free 9034K/10439K, paused 23ms
            05-15 21:59:52.810: I/dalvikvm-heap(7388): Grow heap (frag case) to 9.477MB for 614416-byte allocation
            05-15 21:59:52.850: D/dalvikvm(7388): GC_FOR_ALLOC freed <1K, 14% free 9633K/11079K, paused 22ms
            05-15 21:59:52.880: E/content_type_split.length(7388): ## 3   ~~ row id~~   2
            05-15 21:59:52.890: E/get view ,(7388):  ~ http://localhost/project/www/username/kids.jpg
            05-15 21:59:52.890: E/enter bitmap(7388): yes
            05-15 21:59:52.930: W/System.err(7388): Error reading from ./org/apache/harmony/awt/www/content/image/jpeg.class
            05-15 21:59:52.950: E/content_type_split.length(7388): ## 3   ~~ row id~~   0
            05-15 21:59:52.950: E/get view ,(7388):  ~ http://localhost/project/www/username/kids.jpg
            05-15 21:59:52.950: E/enter bitmap(7388): yes
            05-15 21:59:52.980: W/System.err(7388): Error reading from ./org/apache/harmony/awt/www/content/image/jpeg.class
            05-15 21:59:52.990: E/content_type_split.length(7388): ## 3   ~~ row id~~   1
            05-15 21:59:52.990: E/get view ,(7388):  ~ http://localhost/project/www/username/1_2012512080548_DSC00701.JPG
            05-15 21:59:52.990: E/enter bitmap(7388): yes
            05-15 21:59:53.040: W/System.err(7388): Error reading from ./org/apache/harmony/awt/www/content/image/jpeg.class
            05-15 21:59:53.060: D/dalvikvm(7388): GC_FOR_ALLOC freed 1064K, 19% free 9034K/11079K, paused 23ms
            05-15 21:59:53.100: E/content_type_split.length(7388): ## 3   ~~ row id~~   2
            05-15 21:59:53.100: E/get view ,(7388):  ~ http://localhost/project/www/username/kids.jpg
            05-15 21:59:53.100: E/enter bitmap(7388): yes
            05-15 21:59:53.130: W/System.err(7388): Error reading from ./org/apache/harmony/awt/www/content/image/jpeg.class
            05-15 21:59:53.170: D/dalvikvm(7388): GC_CONCURRENT freed 820K, 19% free 9033K/11079K, paused 2ms+2ms
            05-15 21:59:53.170: E/content_type_split.length(7388): ## 3   ~~ row id~~   0
            05-15 21:59:53.180: E/get view ,(7388):  ~ http://localhost/project/www/username/kids.jpg
            05-15 21:59:53.180: E/enter bitmap(7388): yes
            05-15 21:59:53.210: W/System.err(7388): Error reading from ./org/apache/harmony/awt/www/content/image/jpeg.class
            05-15 21:59:53.230: E/content_type_split.length(7388): ## 3   ~~ row id~~   1
            05-15 21:59:53.240: E/get view ,(7388):  ~ http://localhost/project/www/username/1_2012512080548_DSC00701.JPG
            05-15 21:59:53.240: E/enter bitmap(7388): yes
            05-15 21:59:53.280: W/System.err(7388): Error reading from ./org/apache/harmony/awt/www/content/image/jpeg.class
            05-15 21:59:53.320: D/dalvikvm(7388): GC_CONCURRENT freed 268K, 13% free 9640K/11079K, paused 3ms+2ms
            05-15 21:59:53.330: E/content_type_split.length(7388): ## 3   ~~ row id~~   2
            05-15 21:59:53.350: E/get view ,(7388):  ~ http://localhost/project/www/username/kids.jpg
            05-15 21:59:53.350: E/enter bitmap(7388): yes
4

2 に答える 2

1

この回答をよく見てください

カスタムリストビューアダプターの getView メソッドが複数回呼び出され、一貫した順序で呼び出されない

これは問題ではありません。getView() が呼び出される順序や回数に関する保証はまったくありません。

于 2012-05-15T14:20:14.003 に答える