Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 更新listView中的一行,每四行更新一次_Java_Android_Listview - Fatal编程技术网

Java 更新listView中的一行,每四行更新一次

Java 更新listView中的一行,每四行更新一次,java,android,listview,Java,Android,Listview,我想更新一个listview项目,它可以正常工作,直到我向下滚动到不可见的项目,并且似乎有一个项目(不可见)也得到更新(我向下滚动时看到) 上图描述了这个问题,当我更新位置0的listview项目时,位置5的项目(在我滚动之前不可见)也会更新(应该不会),如果我对pos 1执行相同操作,那么pos 6也会更新,依此类推,反之亦然,例如:当我更新pos 7并向上滚动时,pos 2也会更新,如何防止这种情况发生,并且只让listview更新我单击的项目(而不更新在滚动之前不可见的listview项

我想更新一个listview项目,它可以正常工作,直到我向下滚动到不可见的项目,并且似乎有一个项目(不可见)也得到更新(我向下滚动时看到)

上图描述了这个问题,当我更新位置0的listview项目时,位置5的项目(在我滚动之前不可见)也会更新(应该不会),如果我对pos 1执行相同操作,那么pos 6也会更新,依此类推,反之亦然,例如:当我更新pos 7并向上滚动时,pos 2也会更新,如何防止这种情况发生,并且只让listview更新我单击的项目(而不更新在滚动之前不可见的listview项目)

试试这个:

AnsweredQuestion.java:

public class AnsweredQuestion {
int selectedAnswerViewId;
int correctAnswerViewId;

public AnsweredQuestion(int selectedAnswerViewId, int correctAnswerViewId) {
    this.selectedAnswerViewId = selectedAnswerViewId;
    this.correctAnswerViewId = correctAnswerViewId;
}
}
TestActivity.java:

public class TestActivity extends ListActivity {

private SQLiteDatabase db;
private Cursor cursor;
private int correctAnswers;
private int incorrectAnswers;

private ArrayList<Integer> answeredQuestions = new ArrayList<>();
private HashMap<Integer, AnsweredQuestion> answeredQuestionHashMap = new HashMap<>();

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    correctAnswers = 0;
    incorrectAnswers = 0;

    try{
        DingDongDatabaseHelper coffeinaDatabaseHelper = new DingDongDatabaseHelper(this);
        db = coffeinaDatabaseHelper.getReadableDatabase();
        cursor = db.query("BPIE", new String[]{"_id", "QUESTION", "ANSWER1", "ANSWER2", "ANSWER3", "ANSWER4", "CORRECT"},null, null, null, null,null);
        Random rand = new Random();
        CursorAdapter listAdapter = new SimpleCursorAdapter(this, R.layout.test_adapter, cursor, new String[]{ "QUESTION", "ANSWER1", "ANSWER2", "ANSWER3", "ANSWER4"}, new int[]{R.id.text_test_question, R.id.radio_answer1, R.id.radio_answer2, R.id.radio_answer3, R.id.radio_answer4}, 0){
            @Override
            public void bindView(View view, Context context, Cursor cursor) {
                super.bindView(view, context, cursor);
                int position = cursor.getPosition();
                RadioButton radio1 = (RadioButton)view.findViewById(R.id.radio_answer1);
                RadioButton radio2 = (RadioButton)view.findViewById(R.id.radio_answer2);
                RadioButton radio3 = (RadioButton)view.findViewById(R.id.radio_answer3);
                RadioButton radio4 = (RadioButton)view.findViewById(R.id.radio_answer4);
                radio1.setBackgroundColor(Color.WHITE);
                radio2.setBackgroundColor(Color.WHITE);
                radio3.setBackgroundColor(Color.WHITE);
                radio4.setBackgroundColor(Color.WHITE);
                if(answeredQuestions.contains(position)){
                    AnsweredQuestion answeredQuestion = answeredQuestionHashMap.get(position);
                    RadioButton selectedAnswer = view.findViewById(answeredQuestion.selectedAnswerViewId);
                    RadioButton correctAnswer = view.findViewById(answeredQuestion.correctAnswerViewId);
                    selectedAnswer.setBackgroundColor(Color.RED);
                    correctAnswer.setBackgroundColor(Color.GREEN);
                }
            }
        };

        setListAdapter(listAdapter);

    }catch(SQLException e){
        Toast toast = Toast.makeText(this, "Błąd",Toast.LENGTH_SHORT);
        toast.show();
    }

}

protected void onListItemClick(ListView l, View item, int position, long id) {

    RadioButton radio1 = (RadioButton)item.findViewById(R.id.radio_answer1);
    RadioButton radio2 = (RadioButton)item.findViewById(R.id.radio_answer2);
    RadioButton radio3 = (RadioButton)item.findViewById(R.id.radio_answer3);
    RadioButton radio4 = (RadioButton)item.findViewById(R.id.radio_answer4);
    radio1.setBackgroundColor(Color.WHITE);
    radio2.setBackgroundColor(Color.WHITE);
    radio3.setBackgroundColor(Color.WHITE);
    radio4.setBackgroundColor(Color.WHITE);

    String correctAnswer="";
    String selectedAnswer="";
    if(item != null){
        RadioGroup radioGroup = (RadioGroup)item.findViewById(R.id.radio_group);
        RadioButton selectedButton = (RadioButton)item.findViewById(radioGroup.getCheckedRadioButtonId());
        RadioButton correctButton = null;

        if(selectedButton!=null){
            if(cursor.moveToPosition(position)) {
                correctAnswer = cursor.getString(6);
            }
            selectedAnswer = String.valueOf(selectedButton.getText());

            if(correctAnswer.equals(selectedAnswer)){
                selectedButton.setBackgroundColor(Color.GREEN);
                correctAnswers++;
                correctButton = selectedButton;
            }
            else
            {
                incorrectAnswers++;
                selectedButton.setBackgroundColor(Color.RED);

                if(correctAnswer.equals(radio1.getText())) correctButton = radio1;
                else if(correctAnswer.equals(radio2.getText())) correctButton = radio2;
                else if(correctAnswer.equals(radio3.getText())) correctButton = radio3;
                else if(correctAnswer.equals(radio4.getText())) correctButton = radio4;
                correctButton.setBackgroundColor(Color.GREEN);
            }
            Toast toast = Toast.makeText(TestActivity.this, "CORRECT: " + correctAnswer + "\nSELECTED: " + selectedAnswer, Toast.LENGTH_LONG);
            toast.show();

            if(!answeredQuestions.contains(position)) answeredQuestions.add(position);
            AnsweredQuestion answeredQuestion = new AnsweredQuestion(selectedButton.getId(), correctButton.getId());
            answeredQuestionHashMap.put(position, answeredQuestion);
        }
    }
}
}
公共类测试活动扩展了ListActivity{
专用数据库数据库;
私有游标;
私人回答;
私人int不正确答案;
private ArrayList answeredQuestions=new ArrayList();
private HashMap answeredQuestionHashMap=新HashMap();
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
正确答案=0;
不正确答案=0;
试一试{
DingDongDatabaseHelper CoffeinadaDatabaseHelper=新的DingDongDatabaseHelper(此);
db=coffeinadadatabasehelper.getReadableDatabase();
cursor=db.query(“BPIE”,新字符串[]{“\u id”,“QUESTION”,“ANSWER1”,“ANSWER2”,“ANSWER3”,“ANSWER4”,“CORRECT”},null,null,null,null);
Random rand=新的Random();
CursorAdapter listAdapter=新的SimpleCorsorAdapter(这个,R.layout.test_适配器,光标,新字符串[]{“问题”,“回答1”,“回答2”,“回答3”,“回答4”},新int[]{R.id.text_测试_问题,R.id.radio_回答1,R.id.radio_回答2,R.id.radio_回答3,R.id.radio_回答4},0){
@凌驾
公共void bindView(视图、上下文上下文、光标){
super.bindView(视图、上下文、光标);
int position=cursor.getPosition();
RadioButton radio1=(RadioButton)视图.findViewById(R.id.radio\u answer1);
RadioButton radio2=(RadioButton)视图.findViewById(R.id.radio\u answer2);
RadioButton radio3=(RadioButton)视图.findViewById(R.id.radio\u answer3);
RadioButton radio4=(RadioButton)视图.findViewById(R.id.radio\u answer4);
radio1.setBackgroundColor(颜色:白色);
radio2.setBackgroundColor(颜色:白色);
radio3.setBackgroundColor(颜色:白色);
收音机4.背景色(颜色:白色);
如果(回答问题。包含(位置)){
AnsweredQuestion AnsweredQuestion=answeredQuestionHashMap.get(位置);
单选按钮selectedAnswer=view.findViewById(answeredQuestion.selectedAnswerViewId);
RadioButton correctAnswer=view.findViewById(answeredQuestion.correctAnswerViewId);
选择回答。挫折背景颜色(颜色。红色);
回答正确。挫折背景颜色(颜色。绿色);
}
}
};
setListAdapter(listAdapter);
}捕获(SQLE异常){
Toast Toast=Toast.makeText(这是“Błd”,Toast.LENGTHąu SHORT);
toast.show();
}
}
受保护的void onListItemClick(列表视图l、视图项、int位置、长id){
RadioButton radio1=(RadioButton)项。findViewById(R.id.radio\u answer1);
RadioButton radio2=(RadioButton)项。findViewById(R.id.radio\u answer2);
RadioButton radio3=(RadioButton)项。findViewById(R.id.radio\u answer3);
RadioButton radio4=(RadioButton)项。findViewById(R.id.radio\u answer4);
radio1.setBackgroundColor(颜色:白色);
radio2.setBackgroundColor(颜色:白色);
radio3.setBackgroundColor(颜色:白色);
收音机4.背景色(颜色:白色);
字符串correctAnswer=“”;
字符串selectedAnswer=“”;
如果(项!=null){
放射组放射组=(放射组)项。findViewById(R.id.radio\U组);
RadioButton selectedButton=(RadioButton)项。findViewById(radioGroup.getCheckedRadioButtonId());
RadioButton correctButton=null;
如果(selectedButton!=null){
if(光标移动位置(位置)){
correctAnswer=cursor.getString(6);
}
selectedAnswer=String.valueOf(selectedButton.getText());
如果(正确答案等于(选择回答)){
selectedButton.setBackgroundColor(颜色.绿色);
正确答案++;
correctButton=selectedButton;
}
其他的
{
不正确的回答++;
selectedButton.setBackgroundColor(颜色:红色);
如果(correctAnswer.equals(radio1.getText()))correctButton=radio1;
如果(correctAnswer.equals(radio2.getText()))correctButton=radio2;
如果(correctAnswer.equals(radio3.getText()))correctButton=radio3;
如果(correctAnswer.equals(radio4.getText()))correctButton=radio4;
correctButton.setBackgroundColor(颜色.绿色);
}
Toast Toast=Toast.makeText(TestActivity.this,“CORRECT:+correctAnswer+”\n选择:+selectedAnswer,Toast.LENGTH\u LONG);
toast.show();
如果(!answeredQuestions.contains(position))answeredQuestions.add(position);
AnsweredQuestion AnsweredQuestion=新的回答问题(selectedButton.getId(),correctButton.getId());
回答问题hashmap.put(位置,回答问题);
}
}
}
}

希望有帮助

post
SimpleCursorAdapter
code这是在类中生成的这可能是因为某些视图被自动重用,并且它们的状态被保留。要控制重用视图的行为,我想您需要设置一个Si
public class TestActivity extends ListActivity {

private SQLiteDatabase db;
private Cursor cursor;
private int correctAnswers;
private int incorrectAnswers;

private ArrayList<Integer> answeredQuestions = new ArrayList<>();
private HashMap<Integer, AnsweredQuestion> answeredQuestionHashMap = new HashMap<>();

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    correctAnswers = 0;
    incorrectAnswers = 0;

    try{
        DingDongDatabaseHelper coffeinaDatabaseHelper = new DingDongDatabaseHelper(this);
        db = coffeinaDatabaseHelper.getReadableDatabase();
        cursor = db.query("BPIE", new String[]{"_id", "QUESTION", "ANSWER1", "ANSWER2", "ANSWER3", "ANSWER4", "CORRECT"},null, null, null, null,null);
        Random rand = new Random();
        CursorAdapter listAdapter = new SimpleCursorAdapter(this, R.layout.test_adapter, cursor, new String[]{ "QUESTION", "ANSWER1", "ANSWER2", "ANSWER3", "ANSWER4"}, new int[]{R.id.text_test_question, R.id.radio_answer1, R.id.radio_answer2, R.id.radio_answer3, R.id.radio_answer4}, 0){
            @Override
            public void bindView(View view, Context context, Cursor cursor) {
                super.bindView(view, context, cursor);
                int position = cursor.getPosition();
                RadioButton radio1 = (RadioButton)view.findViewById(R.id.radio_answer1);
                RadioButton radio2 = (RadioButton)view.findViewById(R.id.radio_answer2);
                RadioButton radio3 = (RadioButton)view.findViewById(R.id.radio_answer3);
                RadioButton radio4 = (RadioButton)view.findViewById(R.id.radio_answer4);
                radio1.setBackgroundColor(Color.WHITE);
                radio2.setBackgroundColor(Color.WHITE);
                radio3.setBackgroundColor(Color.WHITE);
                radio4.setBackgroundColor(Color.WHITE);
                if(answeredQuestions.contains(position)){
                    AnsweredQuestion answeredQuestion = answeredQuestionHashMap.get(position);
                    RadioButton selectedAnswer = view.findViewById(answeredQuestion.selectedAnswerViewId);
                    RadioButton correctAnswer = view.findViewById(answeredQuestion.correctAnswerViewId);
                    selectedAnswer.setBackgroundColor(Color.RED);
                    correctAnswer.setBackgroundColor(Color.GREEN);
                }
            }
        };

        setListAdapter(listAdapter);

    }catch(SQLException e){
        Toast toast = Toast.makeText(this, "Błąd",Toast.LENGTH_SHORT);
        toast.show();
    }

}

protected void onListItemClick(ListView l, View item, int position, long id) {

    RadioButton radio1 = (RadioButton)item.findViewById(R.id.radio_answer1);
    RadioButton radio2 = (RadioButton)item.findViewById(R.id.radio_answer2);
    RadioButton radio3 = (RadioButton)item.findViewById(R.id.radio_answer3);
    RadioButton radio4 = (RadioButton)item.findViewById(R.id.radio_answer4);
    radio1.setBackgroundColor(Color.WHITE);
    radio2.setBackgroundColor(Color.WHITE);
    radio3.setBackgroundColor(Color.WHITE);
    radio4.setBackgroundColor(Color.WHITE);

    String correctAnswer="";
    String selectedAnswer="";
    if(item != null){
        RadioGroup radioGroup = (RadioGroup)item.findViewById(R.id.radio_group);
        RadioButton selectedButton = (RadioButton)item.findViewById(radioGroup.getCheckedRadioButtonId());
        RadioButton correctButton = null;

        if(selectedButton!=null){
            if(cursor.moveToPosition(position)) {
                correctAnswer = cursor.getString(6);
            }
            selectedAnswer = String.valueOf(selectedButton.getText());

            if(correctAnswer.equals(selectedAnswer)){
                selectedButton.setBackgroundColor(Color.GREEN);
                correctAnswers++;
                correctButton = selectedButton;
            }
            else
            {
                incorrectAnswers++;
                selectedButton.setBackgroundColor(Color.RED);

                if(correctAnswer.equals(radio1.getText())) correctButton = radio1;
                else if(correctAnswer.equals(radio2.getText())) correctButton = radio2;
                else if(correctAnswer.equals(radio3.getText())) correctButton = radio3;
                else if(correctAnswer.equals(radio4.getText())) correctButton = radio4;
                correctButton.setBackgroundColor(Color.GREEN);
            }
            Toast toast = Toast.makeText(TestActivity.this, "CORRECT: " + correctAnswer + "\nSELECTED: " + selectedAnswer, Toast.LENGTH_LONG);
            toast.show();

            if(!answeredQuestions.contains(position)) answeredQuestions.add(position);
            AnsweredQuestion answeredQuestion = new AnsweredQuestion(selectedButton.getId(), correctButton.getId());
            answeredQuestionHashMap.put(position, answeredQuestion);
        }
    }
}
}