我有一个列表视图,当我单击它时,我想在列表视图中删除一个项目。
我使用了list.setOnItemClickListener(this);
,但“ this”用红色下划线表示,
AdapterView中的setOnItemClickListener(Android.Widget.AdapterView.OnItemClickListener)无法应用于(com.autohush.www.dah.MainActivityFragment1)
我也尝试用getActivity()
代替this
,但是没有用。我该怎么办?
public class MainActivityFragment1 extends Fragment {
public MainActivityFragment1() {
}
private List<ListItems> LocationList;
ArrayAdapter<ListItems> adapter;
ListView list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private class MyAdapter extends ArrayAdapter<ListItems> {
public MyAdapter(Context context, List<ListItems> values) {
super(context, R.layout.list_item,values);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
LayoutInflater inflater = LayoutInflater.from(getContext());
if( v == null){
v = inflater.inflate(R.layout.list_item, parent, false);
}
ListItems currLoc = LocationList.get(position);
TextView theTextView = (TextView) v.findViewById(R.id.list_item_textview);
theTextView.setText(currLoc.getLocation());
ImageView theImageView = (ImageView) v.findViewById(R.id.imageview1);
theImageView.setImageResource(R.drawable.bullet);
return v;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.list_view, container, false);
final EditText txtinput;
LocationList = new ArrayList<ListItems>();
list = (ListView) rootView.findViewById(R.id.list_view);
adapter = new MyAdapter(getActivity(),LocationList);
list.setAdapter(adapter);
txtinput = (EditText) rootView.findViewById(R.id.txtinput);
ImageButton btnadd = (ImageButton) rootView.findViewById(R.id.imageButton);
btnadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String newItem = txtinput.getText().toString();
if (newItem.equals(""))
Toast.makeText(getActivity(), "Enter Location Name", Toast.LENGTH_LONG).show();
else {
LocationList.add(new ListItems(newItem));
adapter.notifyDataSetChanged();
//Hide Keyboard
InputMethodManager inputManager = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
}
}
});
list.setOnItemClickListener(this);
setRetainInstance(true);
return rootView;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ListItems item = LocationList.get(position);
LocationList.remove(item);
adapter = new MyAdapter(getActivity(),LocationList);
list.setAdapter(adapter);
}
}
最佳答案
您的错误提示setOnItemClickListener
不能采用MainActivityFragment1
的参数,因为您需要实现AdapterView.OnItemClickListener
。
MainActivityFragment1 extends Fragment implements AdapterView.OnItemClickListener
或者只是将
new AdapterView.OnItemClickListener()
设置到ListView中。list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ListItems item = LocationList.get(position);
LocationList.remove(item);
adapter = new MyAdapter(getActivity(),LocationList);
list.setAdapter(adapter);
}
});
关于android - 如何调用setOnItemClickListener(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36039593/