gpt4 book ai didi

android - 如何调用 setOnItemClickListener()

转载 作者:行者123 更新时间:2023-11-29 17:22:44 24 4
gpt4 key购买 nike

我有一个 ListView ,我想在单击它时删除 ListView 中的一个项目。
我使用了 list.setOnItemClickListener(this); 但“this”用红色下划线表示,

setOnItemClickListener (Android.Widget.AdapterView.OnItemClickListener) in AdapterView cannot be applied to (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

或者只是在 ListView 中设置一个 new AdapterView.OnItemClickListener()

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com