ListView崩溃,无法处理持有者类

时间:2015-11-21 20:57:02

标签: java android listview android-listview android-arrayadapter

Hello StackOverflowers,

经过数年,数月和数周的潜伏,我终于遇到了Android开发者网站和现有帖子都无法解决的问题,所以请尽可能帮助我解决这个小问题。

我想用我的代码实现什么? (更新!) →我想创建一个列表,使用CheckBoxes,在选择其中一些后,我想将已检查的列表转移到下一个Activity中。 以下代码不会解决有关传递的问题。我希望通过提供的2个课程来实现,我可以检查项目上下滚动,并记住它们是否被检查过。

我的问题(更新了!) →ListView中的CheckBoxes不记得在滚动回到它们的位置后是否检查了它们。

我已将有问题的代码封装到此单独的项目中

这是守则。 请注意,Planet-class和PlanetAdapter属于同一个JavaClass。

-----------> MainAcitivty< ----------------------

package com.example.christian.listviewwithcheckboxes;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.Toast;


import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ListView lv;
    ArrayList<Planet> planetList;
    PlanetAdapter plAdapter;

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

        lv = (ListView) findViewById(R.id.listview);
        displayPlanetList();

        lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CheckBox checkbox = (CheckBox) view.findViewById(R.id.chk_box);
                checkbox.setChecked(!checkbox.isChecked());

                Planet p = planetList.get(position);
                p.setSelected(checkbox.isChecked());
                plAdapter.notifyDataSetChanged();

                Toast.makeText(MainActivity.this, "Checkbox checked/unchecked", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void displayPlanetList() {
        planetList = new ArrayList<Planet>();

        planetList.add((new Planet("please ", 29)));
        planetList.add((new Planet("help ", 30)));
        planetList.add((new Planet("me", 31)));
        planetList.add((new Planet("earth", 1)));
        planetList.add((new Planet("moon", 2)));
        planetList.add((new Planet("mercury", 3)));
        planetList.add((new Planet("jupiter", 4)));
        planetList.add((new Planet("venus", 5)));
        planetList.add((new Planet("pluto", 6)));
        planetList.add((new Planet("mars", 7)));
        planetList.add((new Planet("KOI-1843.03", 8)));
        planetList.add((new Planet("KOI-1843.01", 9)));
        planetList.add((new Planet("KOI-1843.02", 10)));
        planetList.add((new Planet("Kepler-9b", 11)));
        planetList.add((new Planet("Kepler-9c", 12)));
        planetList.add((new Planet("Kepler-9d", 13)));
        planetList.add((new Planet("GJ 160.2b", 14)));
        planetList.add((new Planet("HD 240210b", 15)));
        planetList.add((new Planet("OGLE-05-309L b", 16)));
        planetList.add((new Planet("WASP-82 b", 17)));
        planetList.add((new Planet("Gliese 1214b", 18)));
        planetList.add((new Planet("Kepler-153b", 19)));
        planetList.add((new Planet("Kepler-153c", 20)));
        planetList.add((new Planet("HD 4203 b", 21)));
        planetList.add((new Planet("HD 4203 c", 22)));
        planetList.add((new Planet("HD 179079 b", 23)));
        planetList.add((new Planet("HD 187123 b", 24)));
        planetList.add((new Planet("HD 187123 c", 25)));
        planetList.add((new Planet("Kepler-431 b", 26)));
        planetList.add((new Planet("Kepler-431 b", 27)));
        planetList.add((new Planet("Kepler-431 d", 28)));

        plAdapter = new PlanetAdapter(planetList, this);
        lv.setAdapter(plAdapter);
    }
}

-----------&GT; MainAcitivty&LT; ----------------------

-----------&GT; PlanetAdapter&LT; ----------------------

package com.example.christian.listviewwithcheckboxes;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import java.util.List;

class Planet {
    String name;
    int distance;
    boolean selected = false;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getDistance() {
        return distance;
    }

    public void setDistance(int distance) {
        this.distance = distance;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public Planet(String name, int distance) {
        super();
        this.name = name;
        this.distance = distance;
    }

}

public class PlanetAdapter extends ArrayAdapter<Planet> {

    private List<Planet> planetList;
    private Context context;


    public PlanetAdapter(List<Planet> planetList, Context context) {
        super(context, R.layout.single_listview_item, planetList);
        this.planetList = planetList;
        this.context = context;
    }

    private static class PlanetHolder {
        public TextView planetName;
        public TextView distView;
        public CheckBox chkBox;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        PlanetHolder holder = new PlanetHolder();

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService((Context.LAYOUT_INFLATER_SERVICE));
            v = inflater.inflate(R.layout.single_listview_item, null);

            holder.planetName = (TextView) v.findViewById(R.id.name);
            holder.distView = (TextView) v.findViewById(R.id.dist);
            holder.chkBox = (CheckBox) v.findViewById(R.id.chk_box);

            v.setTag(holder);

        } else {
            holder = (PlanetHolder) v.getTag();
        }

        Planet p = planetList.get(position);
        holder.planetName.setText((p.getName()));
        holder.distView.setText("" + p.getDistance());
        holder.chkBox.setChecked(p.isSelected());

        holder.chkBox.setTag(p);

        return v;
    }
}

-----------&GT; PlanetAdapter&LT; ----------------------

StackOverflow-团队救援请:)

此致 克里斯

编辑(更新):此时没有出现任何错误,但是在上下滚动时,复选框是否记不清。

PS:谢谢你的帮助,你非常感谢我,非常感谢你。

2 个答案:

答案 0 :(得分:1)

您应该将holder.chkBox.setTag(p);替换为v.setTag(p)。此时,else语句将始终将持有者设置为null

答案 1 :(得分:0)

由于我们在评论中的对话,我已经更新了我的答案。 我希望,它能够工作:-)我没有测试过它。

MainActivity

public class MainActivity extends AppCompatActivity {

    ListView lv;
    ArrayList<Planet> planetList;
    PlanetAdapter plAdapter;

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

        lv = (ListView) findViewById(R.id.listview);
        displayPlanetList();

        lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                CheckBox checkbox = (CheckBox) view.findViewById(R.id.chk_box);
                checkbox.setChecked(!checkbox.isChecked());

                Planet p = planetList.get(position);
                p.setSelected(checkbox.isChecked());
                plAdapter.notifyDataSetChanged();

                Toast.makeText(this, "Clicked, state is " + isChecked, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void displayPlanetList() {
        planetList = new ArrayList<Planet>();
        planetList.add((new Planet("please ", 29)));
        planetList.add((new Planet("help ", 30)));
        planetList.add((new Planet("me", 31)));
        planetList.add((new Planet("earth", 1)));
        planetList.add((new Planet("moon", 2)));
        planetList.add((new Planet("mercury", 3)));
        planetList.add((new Planet("jupiter", 4)));
        planetList.add((new Planet("venus", 5)));
        planetList.add((new Planet("pluto", 6)));
        planetList.add((new Planet("mars", 7)));
        planetList.add((new Planet("KOI-1843.03", 8)));
        planetList.add((new Planet("KOI-1843.01", 9)));
        planetList.add((new Planet("KOI-1843.02", 10)));
        planetList.add((new Planet("Kepler-9b", 11)));
        planetList.add((new Planet("Kepler-9c", 12)));
        planetList.add((new Planet("Kepler-9d", 13)));
        planetList.add((new Planet("GJ 160.2b", 14)));
        planetList.add((new Planet("HD 240210b", 15)));
        planetList.add((new Planet("OGLE-05-309L b", 16)));
        planetList.add((new Planet("WASP-82 b", 17)));
        planetList.add((new Planet("Gliese 1214b", 18)));
        planetList.add((new Planet("Kepler-153b", 19)));
        planetList.add((new Planet("Kepler-153c", 20)));
        planetList.add((new Planet("HD 4203 b", 21)));
        planetList.add((new Planet("HD 4203 c", 22)));
        planetList.add((new Planet("HD 179079 b", 23)));
        planetList.add((new Planet("HD 187123 b", 24)));
        planetList.add((new Planet("HD 187123 c", 25)));
        planetList.add((new Planet("Kepler-431 b", 26)));
        planetList.add((new Planet("Kepler-431 b", 27)));
        planetList.add((new Planet("Kepler-431 d", 28)));

        plAdapter = new PlanetAdapter(planetList, this);
        // ListView listView = (ListView) findViewById(R.id.listview);
        lv.setAdapter(plAdapter);


    }
}
在您的适配器中

,您可以删除此行:

PlanetAdapter

 holder.chkBox.setOnCheckedChangeListener((MainActivity) context);