(初学者)重置适配器? (数据集不会改变)

时间:2018-01-27 14:47:39

标签: android listview adapter android-adapter baseadapter

我尝试在点击选项后重新创建相同的listview活动,只是它不起作用。我尝试了SubjectSelectorActivity.this.recreate();startActivity(new Intent(SubjectSelectorActivity.this, SubjectSelectorActivity.class));,但这并没有重置onclick结果。我可以使用哪一行代码重新创建和重置我的Activity / Adapter中的onclick结果?我还想重新创建它,以便我可以将屏幕动画向左滑动​​并显示新的重新创建的活动,因此如果可以将其用于代码,那将是非常棒的。此外,如果您发现我的代码有任何问题或者有更简单的方法(可能是因为我是初学者),请告诉我:)

代码

package com.example.adend.timetable.activities;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.os.Bundle;
import android.view.LayoutInflater;
import com.example.adend.timetable.R;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.example.adend.timetable.appwidgets.AbsThemeActivity;
import com.kabouzeid.appthemehelper.ThemeStore;


public class SubjectSelectorActivity extends AbsThemeActivity implements View.OnClickListener {

    ListView lv;
    Context context;
    SubjectAdapter adapter;

    public static int[] subjectIcons ={
            R.drawable.cross,
            R.drawable.tags,
            R.drawable.film,
            R.drawable.book,
            R.drawable.food,
            R.drawable.earth,
            R.drawable.heart_pulse,
            R.drawable.run,
            R.drawable.history,
            R.drawable.indo,
            R.drawable.jap,
            R.drawable.law,
            R.drawable.calculator,
            R.drawable.film,
            R.drawable.library_music,
            R.drawable.earth,
            R.drawable.bike,
            R.drawable.hammer,
            R.drawable.run,
            R.drawable.phsy,
            R.drawable.currency_usd,
            R.drawable.microscope,
            R.drawable.drawing_box,
            R.drawable.settings,
            R.drawable.tshirt_crew,
            R.drawable.drawing_box,
            R.drawable.settings,
    };

    public static String[] subjects = {
            "Christian ED",
            "Coding and Informatics",
            "Drama",
            "English",
            "Food Studies",
            "Geography",
            "Health",
            "Health/PE",
            "History",
            "Indonesian",
            "Japanese",
            "Law and Economics",
            "Mathematics",
            "Media Arts",
            "Music",
            "Our World",
            "Outdoor Education",
            "PDT – Materials",
            "Phys ED",
            "Psychology",
            "Running a Small Business",
            "Science",
            "Studio Arts",
            "Systems Engineering",
            "Textiles",
            "Visual Communication Design",
            "Custom",
    };

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

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);
        Button next = (findViewById(R.id.next));
        Button cancel = (findViewById(R.id.cancel));
        toolbar.setBackgroundColor(ThemeStore.primaryColor(this));
        setStatusbarColor(ThemeStore.primaryColor(this));
        next.setTextColor(ThemeStore.primaryColor(this));

        next.setOnClickListener(this);

        context = this;

        lv = findViewById(R.id.list);
        adapter = new SubjectAdapter(this, subjects, subjectIcons);
        lv.setAdapter(adapter);

    }

    public int getCounter() {
        SharedPreferences getnotch = context.getSharedPreferences("counter", MODE_PRIVATE);
        int defaultValue = 0;
        int notch = getnotch.getInt("counter", defaultValue);
        return notch;
    }

    private String getPass() {
        SharedPreferences pass = context.getSharedPreferences("pass", MODE_PRIVATE);
        String defaultValue = null;
        String passi = pass.getString("pass", defaultValue);
        return passi;
    }

    @Override
    public void onClick(View v) {

        String subposition = getPass();

        if (getPass() != null) {

            SharedPreferences counter = getSharedPreferences("counter", MODE_PRIVATE);
            SharedPreferences.Editor countereditor = counter.edit();

            if (getCounter() == 0) {

                SharedPreferences sharedMonP1 = getSharedPreferences("MonP1", MODE_PRIVATE);
                SharedPreferences.Editor monp1editor = sharedMonP1.edit();
                monp1editor.putString("MonP1", subposition);
                monp1editor.commit();

                countereditor.putInt("counter", getCounter() + 1);
                countereditor.commit();

                startActivity(new Intent(SubjectSelectorActivity.this, SubjectSelectorActivity.class));

                // restart activity for a new onclick result - tried "startActivity(new Intent(SubjectSelectorActivity.this, SubjectSelectorActivity.class));"
                // but it keeps the same onclick result and sets all of the SharedPreferences as the same string.
                // As it restarts, animation slide to left

            }

            if (getCounter() == 1) {
                SharedPreferences sharedMonP2 = getSharedPreferences("MonP2", MODE_PRIVATE);
                SharedPreferences.Editor monp2editor = sharedMonP2.edit();
                monp2editor.putString("MonP2", subposition);
                monp2editor.commit();

                countereditor.putInt("counter", getCounter() + 1);
                countereditor.commit();

                startActivity(new Intent(SubjectSelectorActivity.this, SubjectSelectorActivity.class));

                // restart activity for a new onclick result - tried "startActivity(new Intent(SubjectSelectorActivity.this, SubjectSelectorActivity.class));"
                // but it keeps the same onclick result and sets all of the SharedPreferences as the same string.
                // As it restarts, animation slide to left
            }

            if (getCounter() == 2) {
                SharedPreferences sharedMonP3 = getSharedPreferences("MonP3", MODE_PRIVATE);
                SharedPreferences.Editor monp3editor = sharedMonP3.edit();
                monp3editor.putString("MonP3", subposition);
                monp3editor.commit();

                countereditor.putInt("counter", getCounter() + 1);
                countereditor.commit();

                startActivity(new Intent(SubjectSelectorActivity.this, SubjectSelectorActivity.class));

                // restart activity for a new onclick result - tried "startActivity(new Intent(SubjectSelectorActivity.this, SubjectSelectorActivity.class));"
                // but it keeps the same onclick result and sets all of the SharedPreferences as the same string.
                // As it restarts, animation slide to left
            }

            if (getCounter() == 3) {
                startActivity(new Intent(SubjectSelectorActivity.this, MainActivity.class));
            }
        }
    }

    public class SubjectAdapter extends BaseAdapter {

        String[] result;
        int[] imageId;
        Context context;

        private LayoutInflater inflater = null;


        public SubjectAdapter(SubjectSelectorActivity subjectSelector, String[] subjects, int[] subjectIcons) {
            result = subjects;
            context = subjectSelector;
            imageId = subjectIcons;
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        }

        @Override
        public int getCount() {
            return result.length;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        public class Holder {
            TextView tv1;
            ImageView img;
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (getCounter() == 3){
                SharedPreferences counter = getSharedPreferences("counter", MODE_PRIVATE);
                SharedPreferences.Editor countereditor = counter.edit();
                countereditor.putInt("counter", 0);
                countereditor.commit();
            }

            Holder holder = new Holder();
            View rowView;
            rowView = inflater.inflate(R.layout.subject, null);
            holder.tv1 = rowView.findViewById(R.id.subtext);
            holder.img = rowView.findViewById(R.id.subimg);
            holder.tv1.setText(result[position]);
            holder.img.setImageResource(imageId[position]);
            rowView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    String sub = result[position];

                    SharedPreferences passs = getSharedPreferences("pass", MODE_PRIVATE);
                    SharedPreferences.Editor passe = passs.edit();
                    passe.putString("pass", sub);
                    passe.commit();

                }

            });

            return rowView;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用

groovy.transform.stc

每当更改提供给适配器的数据并且您想要重置listView以再次创建视图时。

如果要重新绘制listView中的所有项目,请使用

 adapter.notifyDataSetChanged();