所以我正计划创建一个简单的“无数据库”待办事项应用程序
我已经能够显示项目并将其添加到列表中。 现在,我试图通过传递被单击项的索引来删除。问题是我不知道如何传递参数。任何帮助将不胜感激!
这是我的代码
public class MainActivity extends AppCompatActivity {
List<String> notesList;
Toolbar toolbar;
ListView listNotes;
ArrayAdapter<String> mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitleTextColor(Color.WHITE);
notesList = new ArrayList<String>();
// notesList.add("1");
listNotes = findViewById(R.id.list_notes);
delete = findViewById(R.id.btn_delete);
loadNotesList();
}
private void loadNotesList() {
if(mAdapter == null){
mAdapter = new ArrayAdapter<String>(this,R.layout.note_item,R.id.note_title, notesList);
listNotes.setAdapter(mAdapter);
}
else {
mAdapter.clear();
mAdapter.addAll(notesList);
mAdapter.notifyDataSetChanged();
}
}
private void displayToast() {
// notesList.add("test");
Log.d("list", notesList.toString());
Toast.makeText(getApplicationContext(), notesList.toString(),
Toast.LENGTH_LONG).show();
}
private void deleteNote(View view){
View parent = (View)view.getParent();
TextView noteTitleTextView = (TextView)findViewById(R.id.note_title);
String noteTitle = String.valueOf(noteTitleTextView.getText());
Toast.makeText(getApplicationContext(), noteTitleTextView.toString(),
Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
notesList = new ArrayList<String>();
//notesList.add("tset vissew");
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_actions, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_add) {
displayToast();
final EditText noteEditText = new EditText(this);
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Add a new note")
.setMessage("What's on your mind?")
.setView(noteEditText)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String note = String.valueOf(noteEditText.getText());
notesList.add(note);
loadNotesList();
displayToast();
}
})
.setNegativeButton("Cancel", null)
.create();
dialog.show();
//loadNotesList();
//displayToast();
}
return super.onOptionsItemSelected(item);
}
}
我的列表项布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<TextView
android:id="@+id/note_title"
android:text="Example"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:gravity="center"/>
<Button
android:id="@+id/btn_delete"
android:text="Delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
代码看起来非常丑陋,对此感到抱歉。
答案 0 :(得分:2)
为您的ListView添加OnItemClickListener
listNotes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//here position is index of your list's item
}
});