Android-如何删除列表项?
我有一个主扩展适配器,当用户单击按钮时,它会添加行。在适配器中,我得到了一个删除项按钮和一个复选框。如何从列表中删除该项目Android-如何删除列表项?,android,listview,adapter,Android,Listview,Adapter,我有一个主扩展适配器,当用户单击按钮时,它会添加行。在适配器中,我得到了一个删除项按钮和一个复选框。如何从列表中删除该项目 public class main extends Activity{ ArrayList<String> noteList = new ArrayList<String>(); FancyAdapter aa = null; Button calculate; EditText result; String total; String name
public class main extends Activity{
ArrayList<String> noteList = new ArrayList<String>();
FancyAdapter aa = null;
Button calculate;
EditText result;
String total;
String name;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Spinner spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.spinner_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
ListView myListView = (ListView)findViewById(R.id.noteList);
aa = new FancyAdapter();
final EditText price = (EditText)findViewById(R.id.price);
final EditText name1 = (EditText)findViewById(R.id.name);
myListView.setAdapter(aa);
myListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
}
});
Button btnSimple = (Button)findViewById(R.id.btnSimple);
btnSimple.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
try
{
double totalPrice = Double.parseDouble(price.getText().toString());
int position = spinner.getSelectedItemPosition();
name = name1.getText().toString();
if(position == 0)
{
totalPrice = totalPrice * 1.07;
total = String.valueOf(totalPrice);
System.out.println(total);
}
else
{
totalPrice = (totalPrice * 1.1)*1.07;
total = String.valueOf(totalPrice);
System.out.println(total);
}
String wholeString = name + ":$" +total;
noteList.add(0, wholeString);
System.out.println(total);
name1.setText("");
price.setText("");
aa.notifyDataSetChanged();
}
catch (Exception e)
{
}
}
});
}
class FancyAdapter extends ArrayAdapter<String>
{
Button calculate;
EditText price;
EditText result;
FancyAdapter()
{
super(main.this, android.R.layout.simple_list_item_1, noteList);
}
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if(row == null)
{
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.custom_list_item, null);
}
StringTokenizer tokens = new StringTokenizer(noteList.get(position), ":");
String first = tokens.nextToken();
String second = tokens.nextToken();
row.getTag();
((TextView)row.findViewById(R.id.nametv)).setText(first);
((EditText)row.findViewById(R.id.result)).setText(second);
Button deleteButton = (Button) row.findViewById(R.id.button);
deleteButton.setTag(position);
deleteButton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
}
}
);
return (row);
}
}
}
public类主扩展活动{
ArrayList noteList=新建ArrayList();
FancyAdapter aa=null;
按钮计算;
编辑文本结果;
字符串总数;
字符串名;
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
最终微调器微调器=(微调器)findViewById(R.id.Spinner);
ArrayAdapter=ArrayAdapter.createFromResource(
这个,R.array.spinner\u数组,android.R.layout.simple\u spinner\u项);
setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
旋转器。设置适配器(适配器);
ListView myListView=(ListView)findViewById(R.id.noteList);
aa=新的FancyAdapter();
最终EditText价格=(EditText)findViewById(R.id.price);
最终EditText名称1=(EditText)findViewById(R.id.name);
myListView.setAdapter(aa);
myListView.setOnItemClickListener(新的OnItemClickListener(){
公共链接(AdapterView arg0、视图arg1、,
整数位置,长arg3){
}
});
按钮btnSimple=(按钮)findViewById(R.id.btnSimple);
setOnClickListener(新的OnClickListener(){
公共void onClick(视图v)
{
尝试
{
double totalPrice=double.parseDouble(price.getText().toString());
int position=spinner.getSelectedItemPosition();
name=name1.getText().toString();
如果(位置==0)
{
总价=总价*1.07;
总计=字符串.valueOf(总价);
系统输出打印项次(总计);
}
其他的
{
总价=(总价*1.1)*1.07;
总计=字符串.valueOf(总价);
系统输出打印项次(总计);
}
字符串批发字符串=名称+“:$”+总计;
添加(0,整体字符串);
系统输出打印项次(总计);
名称1.setText(“”);
price.setText(“”);
aa.notifyDataSetChanged();
}
捕获(例外e)
{
}
}
});
}
类FancyAdapter扩展了ArrayAdapter
{
按钮计算;
编辑文本价格;
编辑文本结果;
FancyAdapter()
{
super(main.this、android.R.layout.simple\u list\u item\u 1、noteList);
}
公共视图getView(int位置、视图转换视图、视图组父视图)
{
视图行=转换视图;
if(行==null)
{
LayoutInflater充气机=getLayoutInflater();
行=充气机。充气(R.layout.custom\u list\u项,空);
}
StringTokenizer令牌=新的StringTokenizer(noteList.get(position),“:”;
String first=tokens.nextToken();
String second=tokens.nextToken();
row.getTag();
((TextView)row.findviewbyd(R.id.nametv)).setText(第一);
((EditText)row.findviewbyd(R.id.result)).setText(第二);
Button deleteButton=(Button)row.findViewById(R.id.Button);
deleteButton.setTag(位置);
deleteButton.setOnClickListener(
新建按钮。OnClickListener(){
公共void onClick(视图v){
}
}
);
返回(行);
}
}
}
使用从arraylist中删除项目
noteList.remove(itemIndex);
然后打电话
aa.notifyDataSetChanged();
删除列表项,并调用notifyDataSetChanged()
啊。。。我懂了。。它是noteList.remove。我尝试了aa.remove和myListView.remove,但无法获得它。谢谢。是否可以添加动画作为结果?可能的副本
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if(row == null)
{
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.custom_list_item, null);
}
StringTokenizer tokens = new StringTokenizer(noteList.get(position), ":");
String first = tokens.nextToken();
String second = tokens.nextToken();
row.getTag();
((TextView)row.findViewById(R.id.nametv)).setText(first);
((EditText)row.findViewById(R.id.result)).setText(second);
Button deleteButton = (Button) row.findViewById(R.id.button);
deleteButton.setTag(position);
deleteButton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
noteList.remove(position);
notifyDataSetChanged();
}
}
);
return (row);
}