gpt4 book ai didi

java - 为什么在此 fragment 中未到达我的 onClick 方法

转载 作者:行者123 更新时间:2023-12-01 17:59:07 24 4
gpt4 key购买 nike

我正在尝试在 Android 的 fragment 中创建 ListView 。我已成功使用俱乐部数组填充 ListView ,但当我单击列表时,我的方法 OnClick 永远不会到达。我希望看到弹出一个带有行号的 toast 。

请参阅下面的代码:

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

/**
* club selection java Class
*/



public class ClubsFragment extends Fragment implements View.OnClickListener {

String[] mClubArray;
ListView MyList;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


//returning our layout file
//change R.layout.yourlayoutfilename for each of your fragments

View view = inflater.inflate(R.layout.fragment_clubs, container, false);

mClubArray = getResources().getStringArray(R.array.club_array);

ArrayAdapter<String> itemsAdapter =
new ArrayAdapter<String>(getContext(), R.layout.list_item_style, mClubArray);

MyList = (ListView) view.findViewById(R.id.ClubList);
MyList.setAdapter(itemsAdapter);

return view;

}


@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//you can set the title for your toolbar here for different fragments different titles
getActivity().setTitle("Clubs");

}



@Override
public void onClick(View view) {

MyList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{

Toast.makeText(getContext(),
"Click ListItem Number " + position, Toast.LENGTH_LONG)
.show();


}
});


}





}

最佳答案

在你的onCreateView()中:

使用OnItemClickListener

设置适配器后

  MyList.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
String value = (String)adapter.getItemAtPosition(position);
Toast.makeText(this,value,Toast.LENGTH_SHORT).show();
list.remove(position);
adapter.notifyDataSetChanged();
// do what you intend to do on click of listview row
}
});

删除项目:

ArrayList<String> list =new ArrayList<String>(Arrays.asList(mClubArray)); //declare globally so that u can access it 
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, list);

MyList.setAdapter(adapter);
MyList.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), "You selected : " + item, Toast.LENGTH_SHORT).show();
String item = list.get(position);
list.remove(position);
adapter.notifyDataSetChanged();

}
});

关于java - 为什么在此 fragment 中未到达我的 onClick 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42325624/

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