ArrayList<String> 在 CursorAdapter 中无法正常工作

ArrayList<String> is not working properly in CursorAdapter

我正在尝试从 SQLite 数据库获取数据库并尝试填充 ListView 并在 ArrayList<String> 中添加数据。 ListView 填充得很好,但问题是 ArrayList<String> 没有很好地填充。

假设我在数据库中有如下数据: 一种 乙 C 丁 乙 F G H 我 杰 钾 大号 米 否 ………… 我的 ListView 以这种方式显示数据。但是 ArrayList<String> 以这种方式添加数据: 一种 乙 C 丁 乙 F 一种 乙 C 丁 乙 F G H ......

这意味着我的 ArrayList<String> 取前 6 个值然后重复它,然后它取下一个 6 值然后再次重复它.....。这意味着当我从列表视图中单击项目 "H" 时,ArrayList<String> 会显示 "B"。我不知道为什么会这样?

我的代码,扩展了 CursorAdapter

的自定义列表
import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CursorAdapter;
import android.widget.TextView;

public class CustomList extends CursorAdapter {

    CheckBox favoriteBox;
    TextView cityName, countryName;
    ArrayList<String> city_name;
    ArrayList<String> country_name;
    ArrayList<String> country_short;
    ArrayList<CheckBox> check_box;

    public CustomList(Context context, Cursor c, ArrayList<String> city_name,
            ArrayList<String> country_name, ArrayList<String> country_short,
            ArrayList<CheckBox> check_box) {
        super(context, c, 0);
        this.city_name = city_name;
        this.country_name = country_name;
        this.country_short = country_short;
        this.check_box = check_box;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View retView = inflater.inflate(R.layout.all_city_list, parent, false);

        return retView;
    }

    @Override
    public void bindView(final View view, Context context, Cursor cursor) {
        cityName = (TextView) view.findViewById(R.id.cityName);
        String cityS = cursor.getString(cursor.getColumnIndex("city_name"));
        cityName.setText(cityS);
        city_name.add(cityS);//adding value to ArrayList<String>

        countryName = (TextView) view.findViewById(R.id.countryName);
        String conS = cursor.getString(cursor.getColumnIndex("country_name"));
        countryName.setText(conS);
        country_name.add(conS);//adding value to ArrayList<String>
        country_short.add(""
                + cursor.getString(cursor.getColumnIndex("country_short")));//adding value to ArrayList<String>

        favoriteBox = (CheckBox) view.findViewById(R.id.favoriteCheckBox);
        check_box.add(favoriteBox);
        if (cursor.getInt(cursor.getColumnIndex("favorite")) == 0) {
            favoriteBox.setChecked(false);
        } else {
            favoriteBox.setChecked(true);
        }
    }
}

Class AllCityListFragment 扩展 Fragment

import java.io.IOException;
import java.util.ArrayList;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.Toast;

public class AllCityListFragment extends Fragment {
    private CustomList customAdapter;
    private DatabaseHelper databaseHelper;
    ArrayList<String> city_name = new ArrayList<String>();
    ArrayList<String> country_name = new ArrayList<String>();
    ArrayList<String> country_short = new ArrayList<String>();
    ArrayList<CheckBox> check_box = new ArrayList<CheckBox>();

    public static final String ARG_OS = "OS";
    int pos;
    String sql = "";

    ListView list;
    View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.all_city_layout, null);

        databaseHelper = new DatabaseHelper(view.getContext());

        try {
            databaseHelper.createDataBase();
            databaseHelper.openDataBase();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Toast.makeText(view.getContext(), "Error: " + e.getMessage(),
                    Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }

        list = (ListView) view.findViewById(R.id.citylist);
        list.setFocusable(false);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View myview,
                    int position, long id) {
                Toast.makeText(getActivity(),
                        position + " " + city_name.get(position),
                        Toast.LENGTH_LONG).show();
            }
        });

            sql = "SELECT _id, city_name, country_name, country_short, favorite FROM city order by country_name asc;";


        new Handler().post(new Runnable() {
            @Override
            public void run() {
                customAdapter = new CustomList(getActivity(), databaseHelper
                        .getResult(sql), city_name, country_name,
                        country_short, check_box);

                list.setAdapter(customAdapter);
            }
        });

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }


}

在这张图片中,我点击了 "Shirajganj",但 Toast 显示“230 Thakurgaon”位置 =230 是正确的,但 city_name.get(230) 应该是 "Shirajganj" 。当我试图在 CustomList 的 binView 中向 ArrayList<String> 添加值时,我认为它做错了。我怎么解决这个问题?

在 bindView 方法中填充该数组不是一个好主意。 Android 不会为列表中的每一行创建单独的视图,它利用已经创建的视图,这就是为什么您总是会在数组列表中收到意想不到的结果。更好的方法是在 activity 中填充数组而不是适配器。只需像这样查询您的数据库以获得相同的结果:

Cursor cursor = getContentResolver().query(
    uri,  
    projection,                       
    selectionClause                   
    selectionArgs,                    
    sortOrder);   

while (cursor.moveToNext ()){
    arrayList.add(cursor.getAsString(cursor.getColumnIndex('column_name')));
}
cursor.close()