[英]How do I pass the index of an item when a button is clicked from a listview?
So I'm planning on creating a simple 'no-db' todo app 所以我打算创建一个简单的“无数据库”待办应用
I've been able to display and add items to my list. 我已经能够显示项目并将其添加到列表中。 Now I'm trying to delete by passing the index of the clicked item.
现在,我试图通过传递被单击项的索引来删除。 The problem is that I don't know how to pass parameters.
问题是我不知道如何传递参数。 Any help would be greatly appreciated!
任何帮助将不胜感激!
Here's my code 这是我的代码
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);
}
}
My layout for the list 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>
The code loooks really ugly, I'm sorry for that. 代码看起来很丑陋,对此我感到抱歉。
Add OnItemClickListener for your ListView 为您的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
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.