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

时间:2015-04-24 09:45:43

标签: java android sqlite arraylist android-cursoradapter

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

假设我在数据库中有数据,如: 一个 乙 C d Ë F G H 一世 Ĵ ķ 大号 中号 ñ ........ 我的ListView以这种方式显示数据。但ArrayList<String>以这种方式添加数据: 一个 乙 C d Ë F 一个 乙 C d Ë F G H ......

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

我的代码,CustomList,扩展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);
        }
    }
}

扩展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);
    }


}

enter image description here

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

1 个答案:

答案 0 :(得分:0)

在bindView方法中填充该数组不是一个好主意。 Android不会为列表中的每一行创建单独的视图,它会利用已经创建的视图,这就是为什么你总会在你的arraylist中收到意想不到的结果的原因。最好是在活动中填充数组,而不是适配器。只需在数据库中查询相同的结果,如下所示:

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

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