سؤال

Im writing a country/capital flashcard app for history class. If I click the button, and it changes to the capital, when I scroll down, then scroll back up, it reverts to the state. How can I stop this from happening? here is my code:

package com.example.hostoryflashcards;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class EastEurope extends ListActivity {
    String capital[] = {
            "Albainia", "Belarus", "Bosina and Herzegouina", "Bulgaria",
            "Croatia", "Czech Republic", "Estonia", "Finland", "Greece",
            "Hungary", "Kosovo", "Latvia", "Lithuania", "Macedonia", "Molova",
            "Montenegro", "Poland", "Romainia", "Russia", "Serbia", "Slovakia",
            "Ukraine", "RELOAD"
    };
    String answer[] = {
            "TIRANA", "MINSKA", "SARAJERO", "SOFIA", "ZAGREB", "PRAUGE",
            "TALLINN", "HELSINKI", "ATHENS", "BUDAPEST", "PRISTINA", "RIGA",
            "VILNIUS", "SKOPJE", "CHISINAU", "PODGORICA", "WARSAW", "BUCHAREST",
            "MOSCOW", "BELGRADE", "BRATISLAVA", "LJUBLJANA", "KIEV"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loadActivity();
    }

    private void loadActivity() {
        setListAdapter(new ArrayAdapter<String>(EastEurope.this, android.R.layout.simple_list_item_1, capital));
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        TextView ov;
        ov = (TextView) v;

        if (ov.getText().toString() == answer[position]) {
            ov.setText(capital[position]);
        } else {
            ov.setText(answer[position]);
        }
    }
}
هل كانت مفيدة؟

المحلول

The issue that you're changing the displayed view, but not the underlying data. When the adapter needs to recreate the view, it uses the data which hasn't changed.

Given that, the best approach is to change the data and tell the adapter to update, rather than changing the views themselves.

Add a changeable list and use it in the adapter:

ArrayList<String> displayed;

// in loadActivity
displayed = new ArrayList<String>(Arrays.asList(capital));
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, displayed));

Then change the data when a list item's clicked and notify the adapter:

// In onListItemClick
if (displayed.get(position).equals(answer[position])) {
    displayed.set(position, capital[position]);
} else {
    displayed.set(position, answer[position]);
}
((ArrayAdapter) getListAdapter()).notifyDataSetChanged();

نصائح أخرى

I'm fairly new to android and not sure if this is the standard way of solving this, but I have solved this problem by storing the selected state of your element(s) in a field. When you scroll, check the state of your button and if it should be selected, perform whatever actions you wish on it (Highlighting it, changing its text, clicking it, etc.)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top