[英]Deleting item from listview by long click in Android error
I want to add items to appear as ListView , and I also want when I long click on a specific item, it should get deleted. 我想添加要显示为ListView的项目,并且我还希望在长按特定项目时将其删除。
For the adding part, it is working well, but for the deleting part, it doesn't work out. 对于添加的部分,它运行良好,但是对于删除的部分,则无法正常工作。
I think I should add an ArrayList, but I don't know how I can link it with the adapter, so the items that I enter should be put in ArrayList.. 我想我应该添加一个ArrayList,但是我不知道如何将其与适配器链接,因此我输入的项目应该放在ArrayList中。
package com.example.asus.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<String> arrayList = new ArrayList<String>();
final ListView myList = (ListView)findViewById(R.id.listToDo);
final ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
myList.setAdapter(listAdapter);
Button addButton = (Button) findViewById(R.id.button);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText myItem = (EditText)findViewById(R.id.editText);
String value = myItem.getText().toString();
listAdapter.add(value);
myItem.getText().clear();
}
});
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),((TextView)view).getText().toString(),Toast.LENGTH_LONG).show();
}
});
myList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//Adapter adapter = new Adapter(this, listAdapter);
listAdapter.remove(String.valueOf(position));
listAdapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "Item deleted", Toast.LENGTH_LONG).show();
return false;
}
});
}
}
It was working well in the adding part without any error. 在添加部分中运行良好,没有任何错误。 Update:
更新:
I added 我加了
listAdapter.remove(String.valueOf(position));
instead of 代替
listAdapter.remove(position);
There is not any error, but when I long click the item, a message appears "Item deleted" as in the toast, but the item still exists on the listview, how can I fix that? 没有任何错误,但是当我长按该项目时,会在烤面包中出现一条“已删除项目”的消息,但是该项目仍然存在于列表视图中,我该如何解决?
Because listAdapter
is an instance of ArrayAdapter . 因为
listAdapter
是ArrayAdapter的实例。 It do not have any APIs which allow you delete an item based on given position. 它没有任何API允许您根据给定位置删除项目。 To delete an item you should you remove(T) API.
要删除项目,您应该删除 API。
By the way when init the listAdapter
you should pass arrayList
into constructor. 顺便说一下,当初始化
listAdapter
,应该将arrayList
传递给构造函数。
final ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
To delete a selected item 删除所选项目
arrayList.remove(position);
listAdapter.notifyDataSetChanged();
or 要么
listAdapter.remove(arrayList.get(position));
listAdapter.notifyDataSetChanged();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.