2016-12-31 83 views
0

我的适配器没什么问题。在向列表添加新内容并使用notifyDataSetChanged刷新后,onClickListener不适用于该新项目。当我点击返回到添加菜单后,该项目工作正常。Android适配器notifyDataSetChanged仅适用于加载(第二个适配器)

所以加载部分完美地工作。

带有列表的第一个适配器可以完美地工作。它几乎相同的代码。

onCreate功能...

Button addContent = (Button)findViewById(R.id.addContent_button); 
final ListView myList = (ListView)findViewById(R.id.mainMenuList); 
final boolean deleteMode = false; 
String[] liegenSchaften = new String[] {}; 
final List<String> content = new ArrayList<String>(Arrays.asList(liegenSchaften)); 
final ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, content); 

myList.setAdapter(adapter); 

//load the Save Data 
Map<String, ?> map = getSaveMap(); 

//add exists data to list 
for (Map.Entry<String, ?> entry : map.entrySet()) { 
    content.add(entry.getValue().toString()); 
} 

// Update adapter, this works fine! 
adapter.notifyDataSetChanged(); 

addContent.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     content.add(editedText.getText().toString()); 
     /* This adapter dont Update the new Content, the item display and is not clickeble */ 
     adapter.notifyDataSetChanged(); 

     editor.putString(editedText.getText().toString(), editedText.getText().toString()); 
     editor.commit(); 
    } 
}); 

myList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View view, 
          int position, long id) { 
     //load the Save Data 
     Map<String, ?> map = getSaveMap(); 

     Object obj = myList.getAdapter().getItem(position); 
     String value = obj.toString(); 

     //add exists data to list 
     for (Map.Entry<String, ?> entry : map.entrySet()) { 
      if(entry.getValue().toString() == value) { 
       if(deleteMode) { 
        editor.remove(value); 
        editor.commit(); 

        content.remove(position); 
        adapter.notifyDataSetChanged(); 
       } else { 
        selectedContent = entry.getValue().toString(); 
        addMessage.setText(entry.getValue().toString() + " Wurde gewählt."); 
        addMessage.show(); 
       } 
      } 
     } 
    } 
}); 
+0

我记得得到同样的问题。如果我记得正确的话,我通过在适配器类本身内部创建侦听器来避免这个问题。 –

+0

你的意思是?也许你有一个小例子? – ivorysmoker

+0

在这里你可以找到自定义适配器的示例 - http://stackoverflow.com/a/372​​35985/3145960 –

回答

0

我已经找到了问题:

查询是错误的。现在我用等于和ArrayAdapter精美的作品!

if(entry.getValue().toString().equals(value))