ListActivity中的项重复自己

时间:2013-04-29 17:04:29

标签: android listactivity

在图1中,在列表中选择一个项目后,说出历史记录,新活动开始。请看图二。在图2中,点击返回列表后,列表中的项目重复出现,如图3所示。

为什么会这样?

以下是我的代码的一部分:

public class CategoryListActivity extends ListActivity implements LoaderCallbacks<List<Category>> {

String url = "http://vlm1.uta.edu/~zhangzhong/questions.json";
private ArrayAdapter<Category> mListAdapter;
//private ListView listview;
//private ArrayList<Category> categories = new ArrayList<Category>();
private static final int LOADER_ID_CHECKS = 1;
private static final String TAG = "CategoryListActivity";
final Context context = this;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //listview = (ListView) findViewById(R.id.listview);
    mListAdapter = new ArrayAdapter<Category>(this, 
            android.R.layout.simple_list_item_1,
            android.R.id.text1,
            DataStore.categories);
    //listview.setOnItemClickListener(mOnItemClickListener);
    //listview.setAdapter(mListAdapter);
    setListAdapter(mListAdapter);       
    getLoaderManager().initLoader(LOADER_ID_CHECKS, null, this);   
}

@Override
protected void onListItemClick(ListView listView, View view, int position, long id) {
    Intent detailIntent = new Intent(this, ScreenSlideActivity.class);
    detailIntent.putExtra("category_id", id);
    startActivity(detailIntent);
}

@Override
public Loader<List<Category>> onCreateLoader(int id, Bundle args) {
    switch (id) {
        case LOADER_ID_CHECKS:              
            Loader<List<Category>> loader = new CategoryListLoader(this, url);
            loader.forceLoad();
            return loader;
    }
    return null;
}

@Override
public void onLoadFinished(Loader<List<Category>> loader, List<Category> result) {
    Log.d(TAG, "onLoadFinished");

    if (result != null) {
        Log.d(TAG, "items: " + result.size());
    }

    switch (loader.getId()) {
        case LOADER_ID_CHECKS:
            if (result != null) {
                for (Category category : result) {
                    DataStore.categories.add(category);
                }
            }   
            mListAdapter.notifyDataSetChanged();
            break;
    }
}

@Override
public void onLoaderReset(Loader<List<Category>> loader) {
    Log.d(TAG, "onLoaderReset");
    switch (loader.getId()) {
        case LOADER_ID_CHECKS:
            break;
    }
}        
}

enter image description here enter image description here enter image description here

2 个答案:

答案 0 :(得分:1)

case LOADER_ID_CHECKS:
    if (result != null) {
        for (Category category : result) {
            DataStore.categories.add(category);
        }
    }   
    mListAdapter.notifyDataSetChanged();
    break;

应该是

case LOADER_ID_CHECKS:
     DataStore.categories.clear();
     if (result != null) {
         DataStore.categories.addAll(result);
     }
     mListAdapter.notifyDataSetChanged();
     break;

答案 1 :(得分:0)

检查onLoadFinished()是否再次呼叫。如果再次调用onLoadFinishe(),您的数据将再次添加到数组列表中。