带有自定义适配器的android listview冻结活动

时间:2012-11-01 15:16:36

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

我想做类似于EditContact活动的事情(添加各种数字以与简单的添加按钮联系)。

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

添加3-4个条目后,活动会冻结。 (在星系s3和模拟器上)

也许你有个主意?

logcat的:

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.

这是我的活动:

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是问题所在。移除后,没有冻结。

4 个答案:

答案 0 :(得分:1)

两件事:

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

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

  3. 解决这些问题,看看你是否能抓住情况的尾巴。操作沉重对于SGS 3来说应该不是什么大问题 - 它有足够的应对能力。

    希望这有帮助。

答案 1 :(得分:0)

如果我不得不猜测,似乎你的适配器方法中有文本观察者类,更重要的是getView()中的文本监听器。所以,我假设它正在为每一行创建类,并且一直在同时收听每个行。根据您拥有的行数,如果文本观察者固有的话,可能会占用大量资源。您应该找到一种以不同方式实现它的方法。也许在你的适配器外面做一个设置(为了安全),或者做一些构造函数,或者让适配器以某种方式实现它。或者我要做什么 - 只听取所选行中的视图。并在未选中时关闭。值得一试。

编辑:有很多方法。但我认为BaseAdapter最简单的方法是在你想听的每一件事上设置position的标签然后onFocusChanged我想(如果OnClick给出了问题) ,如果标签与位置匹配,请执行您的操作。但如果没有这一点,如果表现良好,那么这一切可能都是不必要就我们所知,侦听器方法已经很好地优化了。只是我当时想不出另一种解释。

答案 2 :(得分:0)

因此,您可以将图像字节存储在设备方向更改时重复使用...

您可以通过覆盖

来检测设备方向更改
    onConfigurationChanged(Configuration newConfig) 

活动中的方法。继续阅读 http://developer.android.com/reference/android/app/Activity.html#onConfigurationChanged(android.content.res.Configuration

http://developer.android.com/reference/android/content/res/Configuration.html

确保指定

    android:configChanges="orientation" 

在该活动的清单条目中。通过此功能,您可以检测设备是纵向还是横向。最重要的是,这只会在设备方向改变时触发(与全局布局监听器不同,后者会随着屏幕上的每一个微小变化而激活)。

希望这会有所帮助。

答案 3 :(得分:0)

如果您在主ui中进行更新或通过线程执行更新或在主ui线程上授予网络策略权限,则会发生这些事情。

执行此操作的唯一方法是使用AsyncTask。通过这个以及在asynctask set adapter view等结束时获取您的数据。