試圖創建一個簡單的應用程序,允許用戶輸入註釋並將其保存到列表視圖中。我需要實現哪些代碼才能允許用戶長時間點擊列表中的某些內容來刪除它。如何在ListView中選擇多項來刪除它們?
public class Notes extends AppCompatActivity {
Button save;
ArrayList<String> addArray = new ArrayList<String>();
EditText txt;
ListView show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notes);
txt = (EditText) findViewById(R.id.textInput);
show = (ListView) findViewById(R.id.listView);
save = (Button) findViewById(R.id.saveButton);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(Notes.this, android.R.layout.simple_list_item_1, addArray);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String getInput = txt.getText().toString();
if (addArray.contains(getInput)) {
Toast.makeText(getBaseContext(), "Note already added!", Toast.LENGTH_LONG).show();
} else if (getInput == null || getInput.trim().equals("")) {
Toast.makeText(getBaseContext(), "Input required!", Toast.LENGTH_LONG).show();
} else {
addArray.add(getInput);
show.setAdapter(adapter);
((EditText) findViewById(R.id.textInput)).setText(" ");
}
}
});
show.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
return true;
}
});
}
}
[自定義ListView和上下文菜單的可能重複。如何得到它?](http://stackoverflow.com/questions/3972945/custom-listview-and-context-menu-how-to-get-it) –
你檢查了我的答案? –