我正在尝试添加和删除我的待办事项列表中的项目。它添加了很好的项目,但我已经设置了一个onLongClickListener来删除项目,它什么也没做。我是初学者,所以我可能错过了一些东西。请帮帮我。任何帮助表示赞赏。
package com.example.todolist;
import java.util.ArrayList;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.os.Build;
import android.app.ListActivity;
public class MainActivity extends Activity implements OnClickListener{
private ArrayList<String> items;
private ArrayAdapter <String> adapter;
private EditText ptadd;
private Button badd;
private ListView lvTodo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
items = new ArrayList <String>();
adapter = new ArrayAdapter<String>(this,adroid.R.layout.simple_list_item_1, items);
lvTodo.setAdapter(adapter);
setListViewListener();
}
private void setListViewListener(){
lvTodo.setOnItemLongClickListener(new OnItemLongClickListener (){
@Override
public boolean onItemLongClick(AdapterView<?> vadapter, View item,
int position, long id) {
// TODO Auto-generated method stub
items.remove(position);
adapter.notifyDataSetChanged();
return true;
}
});;
}
private void initialize() {
// TODO Auto-generated method stub
ptadd = (EditText) findViewById(R.id.etadd);
lvTodo = (ListView) findViewById(R.id.lvTodo);
badd = (Button) findViewById(R.id.badd);
badd.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String lv = ptadd.getText().toString();
adapter.add(lv);
ptadd.setText("");
}
}
我的XML文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100"
>
<ListView
android:id="@+id/lvTodo"
android:layout_width="293sp"
android:layout_height="250sp" >
</ListView>
<EditText
android:id="@+id/etadd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/button1"
android:layout_weight="70"
android:ems="10"
/>
<Button
android:id="@+id/badd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="30"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Add"
/>
</RelativeLayout>
答案 0 :(得分:0)
在您发布的代码中,您永远不会调用setListViewListener
。在initialize
private void initialize() {
// TODO Auto-generated method stub
ptadd = (EditText) findViewById(R.id.etadd);
lvTodo = (ListView) findViewById(R.id.lvTodo);
badd = (Button) findViewById(R.id.badd);
badd.setOnClickListener(this);
//Missing this
setListViewListener();
}