Java 在我放入cutom arrayadapter后listview崩溃

Java 在我放入cutom arrayadapter后listview崩溃,java,android,listview,android-listview,Java,Android,Listview,Android Listview,我最近开始为android编程,我想制作一个稍微复杂一点的程序,以供练习 我遇到了一个与java超级类如何工作有关的问题,我目前无法用我所知道的进行调试 当我将listView放在默认的arrayadapter中时,我编写了一个工作得非常好的代码,但当我尝试使我的自定义arrayadapter成为默认的arrayadapter时,它会在他尝试刷新listView时崩溃。 以下是与自定义适配器相关的部分代码: private Button buttonNovoIme; private Li

我最近开始为android编程,我想制作一个稍微复杂一点的程序,以供练习

我遇到了一个与java超级类如何工作有关的问题,我目前无法用我所知道的进行调试

当我将listView放在默认的arrayadapter中时,我编写了一个工作得非常好的代码,但当我尝试使我的自定义arrayadapter成为默认的arrayadapter时,它会在他尝试刷新listView时崩溃。 以下是与自定义适配器相关的部分代码:

    private Button buttonNovoIme;
private ListView lvRadnici;
private ArrayList <String> listaRadnikaArray;
private StableArrayAdapter adapter;
private EditText novoIme;
private TextView buferZaIme;
private TextView errorText;



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


    buttonNovoIme= (Button) findViewById(R.id.buttonNovoIme);
    lvRadnici = (ListView) findViewById(R.id.listViewRadnici);
    listaRadnikaArray = new ArrayList<String>();
    novoIme = (EditText) findViewById(R.id.editTextNovoIme);
    errorText= (TextView) findViewById(R.id.errorText);

 final StableArrayAdapter adapter =  new StableArrayAdapter (this, R.layout.listview_podesavanje ,  listaRadnikaArray);
    lvRadnici.setAdapter(adapter);

 buttonNovoIme.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


            if (listaRadnikaArray.contains(novoIme.getText().toString()) || novoIme.getText().toString().matches("")) {


                    if (novoIme.getText().toString().matches("")) {
                            errorText.setText("prazno");    
                    }
                    else {
                        errorText.setText("duplikat");
                    }


            }

            else {
            listaRadnikaArray.add(novoIme.getText().toString());
            adapter.notifyDataSetChanged();
            errorText.setText("");
            }

            novoIme.setText("");
        }
    });

谢谢。

您正在创建一个空的arrayList,然后将其提供给适配器:

listaRadnikaArray = new ArrayList<String>(); // this is empty
novoIme = (EditText) findViewById(R.id.editTextNovoIme);
errorText= (TextView) findViewById(R.id.errorText);
final StableArrayAdapter adapter =  new StableArrayAdapter (this, R.layout.listview_podesavanje ,  listaRadnikaArray);
listaradnikarray=newarraylist();//这是空的
novoIme=(EditText)findViewById(R.id.editTextNovoIme);
errorText=(TextView)findViewById(R.id.errorText);
最终StableArrayAdapter=新的StableArrayAdapter(此,R.layout.listview_podesavanje,listaRadnikaArray);

在适配器中使用ArrayList之前,需要在ArrayList中放入一些项。

好的,我解决了这个问题。这是hashMap的错误。我不知道,当您创建自定义ArrayAdapter行时,构造函数后面的行不会定期刷新

  public StableArrayAdapter(Context context, int textViewResourceId,
            List<String> objects) {
          super(context, textViewResourceId, objects);
          for (int i = 0; i < objects.size(); ++i) {  // this will not auto-refresh after 
            mIdMap.put(objects.get(i), i);            // you change items in ArrayList
          }
public stablerayadapter(上下文上下文,int textViewResourceId,
列出对象){
超级(上下文、textViewResourceId、对象);
对于(int i=0;i
所以我需要做的是重写notifyDataSetChanged()

public void notifyDataSetChanged(列出新对象){
super.notifyDataSetChanged();
对于(int i=0;i

现在,每次我用adapter.notifyDataSetChanged(ArrayList)更新ArrayList时,它也会更新hashMap。

谢谢,但事实并非如此。我试图将一些项放入数组列表中,但我遇到了相同的崩溃。
listaRadnikaArray = new ArrayList<String>(); // this is empty
novoIme = (EditText) findViewById(R.id.editTextNovoIme);
errorText= (TextView) findViewById(R.id.errorText);
final StableArrayAdapter adapter =  new StableArrayAdapter (this, R.layout.listview_podesavanje ,  listaRadnikaArray);
  public StableArrayAdapter(Context context, int textViewResourceId,
            List<String> objects) {
          super(context, textViewResourceId, objects);
          for (int i = 0; i < objects.size(); ++i) {  // this will not auto-refresh after 
            mIdMap.put(objects.get(i), i);            // you change items in ArrayList
          }
 public void  notifyDataSetChanged (List<String> newObject){
                super.notifyDataSetChanged();
                for (int i = 0; i < newObject.size(); ++i) {
                    mIdMap.put(newObject.get(i), i);