带有自定义适配器的 android listview 卡住 Activity

标签 android android-listview android-edittext custom-adapter

我想做一些类似于 EditContact Activity 的事情(使用简单的添加按钮添加各种号码以联系)。

我的第一个方法是创建自定义布局,我将其动态添加到 Activity 布局 (Activity with dynamicly added components freezes) 用户 Budius 提到使用带有自定义适配器的 ListView 来实现解决方案。所以我试过了,但行为就像我第一次尝试一样结束了。

添加 3-4 个条目后, Activity 卡住。 (在 galaxy s3 和模拟器上)

也许你有想法?

日志:

11-01 15:42:33.214: D/dalvikvm(712): GC_CONCURRENT freed 148K, 3% free 8283K/8519K, paused 74ms+102ms, total 242ms
11-01 15:42:33.733: I/Choreographer(712): Skipped 93 frames!  The application may be doing too much work on its main thread.
11-01 15:42:42.293: I/Choreographer(712): Skipped 49 frames!  The application may be doing too much work on its main thread.
11-01 15:42:44.946: D/dalvikvm(712): GC_CONCURRENT freed 72K, 2% free 8672K/8839K, paused 16ms+36ms, total 224ms
11-01 15:42:45.593: I/Choreographer(712): Skipped 66 frames!  The application may be doing too much work on its main thread.
11-01 15:42:48.913: I/Choreographer(712): Skipped 35 frames!  The application may be doing too much work on its main thread.
11-01 15:42:49.093: I/Choreographer(712): Skipped 42 frames!  The application may be doing too much work on its main thread.
11-01 15:42:55.083: I/Choreographer(712): Skipped 125 frames!  The application may be doing too much work on its main thread.
11-01 15:42:55.343: I/Choreographer(712): Skipped 68 frames!  The application may be doing too much work on its main thread.
11-01 15:42:55.653: I/Choreographer(712): Skipped 40 frames!  The application may be doing too much work on its main thread.
11-01 15:43:02.553: D/dalvikvm(712): GC_CONCURRENT freed 318K, 5% free 8821K/9223K, paused 16ms+68ms, total 231ms
11-01 15:43:02.863: D/dalvikvm(712): GC_FOR_ALLOC freed 418K, 7% free 8800K/9415K, paused 73ms, total 76ms
11-01 15:43:02.883: I/Choreographer(712): Skipped 224 frames!  The application may be doing too much work on its main thread.
11-01 15:43:03.013: D/dalvikvm(712): GC_CONCURRENT freed 397K, 7% free 8804K/9415K, paused 74ms+6ms, total 113ms
11-01 15:43:03.013: D/dalvikvm(712): WAIT_FOR_CONCURRENT_GC blocked 33ms
11-01 15:43:03.063: D/dalvikvm(712): GC_FOR_ALLOC freed 386K, 7% free 8802K/9415K, paused 41ms, total 41ms
11-01 15:43:03.183: D/dalvikvm(712): GC_CONCURRENT freed 388K, 7% free 8806K/9415K, paused 75ms+7ms, total 119ms
11-01 15:43:03.183: D/dalvikvm(712): WAIT_FOR_CONCURRENT_GC blocked 35ms
11-01 15:43:03.243: D/dalvikvm(712): GC_FOR_ALLOC freed 378K, 7% free 8803K/9415K, paused 51ms, total 51ms
11-01 15:43:03.433: I/Choreographer(712): Skipped 142 frames!  The application may be doing too much work on its main thread.
comment:..... adding the 4. row to custom adapter and listview ..... 
11-01 15:36:31.662: I/Choreographer(712): Skipped 36 frames!  The application may be doing too much work on its main thread.
11-01 15:36:32.542: D/dalvikvm(712): GC_CONCURRENT freed 415K, 23% free 9057K/11719K, paused 18ms+15ms, total 139ms
11-01 15:36:32.554: D/dalvikvm(712): WAIT_FOR_CONCURRENT_GC blocked 123ms
11-01 15:36:32.613: D/dalvikvm(712): GC_FOR_ALLOC freed 492K, 23% free 9046K/11719K, paused 56ms, total 56ms
11-01 15:36:32.753: D/dalvikvm(712): GC_CONCURRENT freed 449K, 23% free 9078K/11719K, paused 76ms+7ms, total 134ms
11-01 15:36:32.753: D/dalvikvm(712): WAIT_FOR_CONCURRENT_GC blocked 48ms
11-01 15:36:32.813: D/dalvikvm(712): GC_FOR_ALLOC freed 514K, 23% free 9046K/11719K, paused 54ms, total 54ms
11-01 15:36:32.943: D/dalvikvm(712): GC_CONCURRENT freed 451K, 23% free 9078K/11719K, paused 76ms+8ms, total 128ms
11-01 15:36:32.943: D/dalvikvm(712): WAIT_FOR_CONCURRENT_GC blocked 44ms
11-01 15:36:33.013: D/dalvikvm(712): GC_FOR_ALLOC freed 516K, 23% free 9046K/11719K, paused 53ms, total 53ms
11-01 15:36:33.143: D/dalvikvm(712): GC_CONCURRENT freed 453K, 23% free 9078K/11719K, paused 75ms+9ms, total 127ms
11-01 15:36:33.143: D/dalvikvm(712): WAIT_FOR_CONCURRENT_GC blocked 41ms
11-01 15:36:33.193: D/dalvikvm(712): GC_FOR_ALLOC freed 518K, 23% free 9046K/11719K, paused 50ms, total 50ms
11-01 15:36:33.333: D/dalvikvm(712): GC_CONCURRENT freed 454K, 23% free 9078K/11719K, paused 75ms+8ms, total 137ms
11-01 15:36:33.333: D/dalvikvm(712): WAIT_FOR_CONCURRENT_GC blocked 59ms
11-01 15:36:33.393: D/dalvikvm(712): GC_FOR_ALLOC freed 520K, 23% free 9046K/11719K, paused 56ms, total 56ms
11-01 15:36:33.533: D/dalvikvm(712): GC_CONCURRENT freed 456K, 23% free 9079K/11719K, paused 75ms+8ms, total 129ms
11-01 15:36:33.533: D/dalvikvm(712): WAIT_FOR_CONCURRENT_GC blocked 52ms
comment:..... many of this .....
11-01 15:40:05.064: D/dalvikvm(712): GC_FOR_ALLOC freed 451K, 23% free 9135K/11719K, paused 97ms, total 97ms
11-01 15:40:05.204: D/dalvikvm(712): GC_CONCURRENT freed 451K, 23% free 9135K/11719K, paused 76ms+6ms, total 136ms
11-01 15:40:05.204: D/dalvikvm(712): WAIT_FOR_CONCURRENT_GC blocked 47ms
11-01 15:40:05.293: D/dalvikvm(712): GC_FOR_ALLOC freed 451K, 23% free 9135K/11719K, paused 66ms, total 66ms
11-01 15:40:05.443: D/dalvikvm(712): GC_CONCURRENT freed 451K, 23% free 9135K/11719K, paused 76ms+6ms, total 148ms
11-01 15:40:05.443: D/dalvikvm(712): WAIT_FOR_CONCURRENT_GC blocked 67ms
11-01 15:40:05.513: D/dalvikvm(712): GC_FOR_ALLOC freed 451K, 23% free 9135K/11719K, paused 59ms, total 59ms
11-01 15:40:05.643: D/dalvikvm(712): GC_CONCURRENT freed 451K, 23% free 9135K/11719K, paused 76ms+7ms, total 127ms
11-01 15:40:05.643: D/dalvikvm(712): WAIT_FOR_CONCURRENT_GC blocked 41ms
11-01 15:40:05.703: D/dalvikvm(712): GC_FOR_ALLOC freed 451K, 23% free 9135K/11719K, paused 56ms, total 56ms
11-01 15:40:05.833: D/dalvikvm(712): GC_CONCURRENT freed 451K, 23% free 9135K/11719K, paused 75ms+7ms, total 127ms
11-01 15:40:05.833: D/dalvikvm(712): WAIT_FOR_CONCURRENT_GC blocked 42ms
11-01 15:40:05.913: D/dalvikvm(712): GC_FOR_ALLOC freed 451K, 23% free 9135K/11719K, paused 63ms, total 63ms
11-01 15:40:06.114: D/dalvikvm(712): GC_CONCURRENT freed 451K, 23% free 9135K/11719K, paused 76ms+39ms, total 196ms
11-01 15:40:06.114: D/dalvikvm(712): WAIT_FOR_CONCURRENT_GC blocked 118ms
11-01 15:40:06.963: E/Trace(734): error opening trace file: No such file or directory (2)
11-01 15:40:07.953: D/gralloc_goldfish(734): Emulator without GPU emulation detected.

这是我的 Activity :

package com.float.rezepte;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.float.utilities.ImageProcessing;
import com.float.utilities.Utilities;

public class AddRecipeActivity extends SherlockActivity {

private EditText editTextName;
private EditText editTextDescription;
private EditText editTextDuration;
private EditText editTextPortions;
private EditText editTextPortionsUnit;
private RatingBar ratingBarDifficutly;
private ImageView imageViewPicture;
private static final int SELECT_PICTURE = 1;
private static final int TAKE_PICTURE = 2;
public boolean deletePicture = false;
private String selectedImagePath;
private byte[] recipePicture = null;
private Recipe recipe;
private ProgressDialog dialog;
private IngredientAdapter ingredientAdapter;
private ImageButton imageButtonAddIngredient;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_recipe);
    ConnectView();
    ArrayList<Ingredient> values;

    if (savedInstanceState != null)
    {
        recipe = savedInstanceState.getParcelable("recipe");
        editTextName.setText(recipe.getName());
        editTextDescription.setText(recipe.getDescription());
        editTextDuration.setText(String.valueOf(recipe.getDuration()));
        editTextPortions.setText(String.valueOf(recipe.getPortions()));
        editTextPortionsUnit.setText(recipe.getPortionsUnit());
        ratingBarDifficutly.setRating(recipe.getDifficulty());
        values = (ArrayList<Ingredient>) recipe.getIngredientList();
    }else{
        recipe = new Recipe();
        values = new ArrayList<Ingredient>();
    }

    ingredientAdapter = new IngredientAdapter(this, values); 
    final ListView listViewIngredients = (ListView) findViewById(R.id.listViewIngredients);
    listViewIngredients.setAdapter(ingredientAdapter);

    imageButtonAddIngredient.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            ingredientAdapter.addItem(new Ingredient());
            ingredientAdapter.notifyDataSetChanged();
            Utilities.setListViewHeightBasedOnChildren(listViewIngredients);
        }
    });

    final ViewTreeObserver observer = imageViewPicture.getViewTreeObserver();
       observer.addOnGlobalLayoutListener (new OnGlobalLayoutListener () {
            @Override
            public void onGlobalLayout() {          
                imageViewPicture.getViewTreeObserver().addOnGlobalLayoutListener(this);
                if(recipe.getPicture() != null){
                    imageViewPicture.setImageBitmap(ImageProcessing.scaleImage(recipe.getPicture(), imageViewPicture.getWidth(), imageViewPicture.getHeight()));
                }
            }
        });
}

private void ConnectView(){
    editTextName = (EditText) findViewById(R.id.editTextName);
    editTextDescription = (EditText) findViewById(R.id.editTextDescription);
    editTextDuration = (EditText) findViewById(R.id.editTextDuration);
    editTextPortions = (EditText) findViewById(R.id.editTextPortions);
    editTextPortionsUnit = (EditText) findViewById(R.id.editTextPortionsUnit);
    ratingBarDifficutly = (RatingBar) findViewById(R.id.ratingBarDifficulty);
    imageViewPicture = (ImageView) findViewById(R.id.imageViewPicture);
    imageButtonAddIngredient = (ImageButton) findViewById(R.id.imageButtonAddIngredient);
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  savedInstanceState.putParcelable("recipe", copyUserInputToRecipe());
}

private Recipe copyUserInputToRecipe(){
    recipe.setName(editTextName.getText().toString());
    recipe.setDescription(editTextDescription.getText().toString());
    recipe.setDifficulty(ratingBarDifficutly.getRating());
    if(!editTextDuration.getText().toString().equals("")){
        recipe.setDuration(Integer.parseInt(editTextDuration.getText().toString()));
    }
    if(!editTextPortions.getText().toString().equals("")){
        recipe.setPortions(Integer.parseInt(editTextPortions.getText().toString()));
    }
    recipe.setPortionsUnit(editTextPortionsUnit.getText().toString());
    if(recipePicture != null){
        recipe.setPicture(recipePicture);
    }
    return recipe;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.activity_add_recipe, menu);
    return true;
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(android.R.anim.fade_in,
            android.R.anim.fade_out);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_cancel_recipe:
        finish();
        break;
    case R.id.menu_save_recipe:
            Intent returnIntent = new Intent();
            returnIntent.putExtra("recipe", recipe);
            setResult(RESULT_OK, returnIntent);
            finish();

        break;
    }
    return super.onOptionsItemSelected(item);
}
}

编辑: ViewTreeObserver 是问题所在。删除它后,没有出现卡住。

最佳答案

两件事:

  1. 您在适配器中实现的 addItem 方法不是线程保存。使其同步。您在 getView 方法中也有一个 data.remove(...) 调用 - 再次以线程不安全的方式完成。

  2. 当相应的文本更改时,您修改 Ingredient 对象。在您的 Ingredient 类的实现中,您是否还更改了关联的 EditText 组件的内容?如果是这样,您将进入一个无限循环,每次调用 editText.setText(...) 时都会调用您的 TextWatcher。 SGS 3 是一个四核 CPU 设备,因此它可以实时处理多达 4 个线程(一个永久用于 UI,因此保留其他 3 个)。

解决这些问题,看看您是否能捕获情况的尾部。操作繁重对于 SGS 3 来说应该不是什么大问题——它有足够的能力来应对。

希望这对您有所帮助。

关于带有自定义适配器的 android listview 卡住 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13180111/

相关文章:

安卓设备配置问题

android - 如何在 fragment 中使用 onNewIntent(Intent intent) 方法?

java - 单击行更改行布局

android - 快速滚动时 CustomListView 崩溃

android - inputType 不隐藏密码

android - 如何检测编辑文本中的特殊字符并显示 Toast 作为响应(Android)?

android - 从联系人中选择一个号码但删除国家代码

Android 相机 android.hardware.Camera 已弃用

java - SQLite数据存储在本地并希望在云端存储和同步

java - 当我添加另一个元素时,如何修复我的 listView 复制相同的元素?