无法在 Fragment 内的 OnItemClickListener 中显示 Toast
Can not show Toast in OnItemClickListener inside Fragment
我正在尝试在 Fragment
中创建 ListView
。 listView 显示正确,但当我编译代码时显示错误。
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class ServiceFragment extends Fragment {
private ListView serviceList;
private String[] serviceString;
public ServiceFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_service, container, false);
//build the list service
serviceList = (ListView) rootView.findViewById(R.id.ServiceList);
serviceString = getResources().getStringArray(R.array.service_list);
ArrayAdapter<String> objAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1,serviceString );
serviceList.setAdapter(objAdapter);
//on click item Listener
serviceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String itemValue = (String) serviceList.getItemAtPosition(position);
int itemPosition = position;
Toast.makeText(ServiceFragment.this,"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG).show();
}
});
return rootView;
}//end of onCreateView
}//end of class ServiceFragment
我的代码有什么问题?为什么 Toast
没有出现?
是否可以在单击项目时启动一个详细信息activity,我可以在其中描述项目的含义
假设
列表项 = 苹果、芒果、香蕉
onClick Item Details view = Apple 是品牌或水果,Banana 是水果,Mango 是好水果,
What Wrong with my code ? Why the Toast does not show
因为你在Fragment
,你必须使用getActivity()
而不是yourClass.this
。
将您的 Toast
更改为:
Toast.makeText(getActivity(),"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG).show();
编辑
有关详细信息 Activity 您必须先创建一个 Activity
让我们称它为 DetailActivity
然后您必须将 onItemClickListener()
更改为:
serviceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String itemValue = (String) serviceList.getItemAtPosition(position);
int itemPosition = position;
Toast.makeText(getActivity(),"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG).show();
Intent intent = new Intent(getActivity(), DetailActivity.class);
intent.putExtra("DetailProductClicked", itemValue);
startActivity(intent);
}
});
所以在你的 DetailActivity.class
你必须把这个放在你的 onCreate()
String nameFruit = intent.getStringExtra("DetailProductClicked");
然后取决于你有什么Activity
你可以做这样的事情:
if (nameFruit.equals("Apple"){
tv.setText(nameFruit +" is a brand or fruit");
}
if (nameFruit.equals("Mango"){
tv.setText(nameFruit +" is a good fruit");
}
if (nameFruit.equals("Banana"){
tv.setText(nameFruit +" is a fruit");
}
要跟进@Skizo 的回答,请将您的 onItemClick 更改为
serviceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String itemValue = (String) serviceList.getItemAtPosition(position);
int itemPosition = position;
Intent nextIntent = new Intent(getActivity(), MyNextActivity.class);
// ... create a bundle to pass of the Object in your data at itemPosition
startActivity(nextIntent);
}
});
实际上,虽然我会像这样在片段中使用一个接口并告诉我的 Activity 开始新的 Activity 而不是片段:
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class ServiceFragment extends Fragment {
private ListView serviceList;
private String[] serviceString;
private ItemListener mCallback;
public ServiceFragment(){}
public interface ItemListener{
// use position or Object data here
void onItemClicked(int position);
}
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
mCallback = (ItemListener) activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_service, container, false);
//build the list service
serviceList = (ListView) rootView.findViewById(R.id.ServiceList);
serviceString = getResources().getStringArray(R.array.service_list);
ArrayAdapter<String> objAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1,serviceString );
serviceList.setAdapter(objAdapter);
//on click item Listener
serviceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String itemValue = (String) serviceList.getItemAtPosition(position);
int itemPosition = position;
mCallback.onItemClicked(itemPosition);
}
});
return rootView;
}//end of onCreateView
}//end of class ServiceFragment
然后在 Activity 中添加 implements ServiceFragment.ItemListener
:
@Override
public void onItemClicked(int position){
// Now handling this element via the Activity, much cleaner
Intent nextIntent = new Intent(this, MyNextActivity.class);
// ... create a bundle to pass of the Object in your data at itemPosition
startActivity(nextIntent);
}
我正在尝试在 Fragment
中创建 ListView
。 listView 显示正确,但当我编译代码时显示错误。
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class ServiceFragment extends Fragment {
private ListView serviceList;
private String[] serviceString;
public ServiceFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_service, container, false);
//build the list service
serviceList = (ListView) rootView.findViewById(R.id.ServiceList);
serviceString = getResources().getStringArray(R.array.service_list);
ArrayAdapter<String> objAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1,serviceString );
serviceList.setAdapter(objAdapter);
//on click item Listener
serviceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String itemValue = (String) serviceList.getItemAtPosition(position);
int itemPosition = position;
Toast.makeText(ServiceFragment.this,"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG).show();
}
});
return rootView;
}//end of onCreateView
}//end of class ServiceFragment
我的代码有什么问题?为什么 Toast
没有出现?
是否可以在单击项目时启动一个详细信息activity,我可以在其中描述项目的含义
假设 列表项 = 苹果、芒果、香蕉 onClick Item Details view = Apple 是品牌或水果,Banana 是水果,Mango 是好水果,
What Wrong with my code ? Why the Toast does not show
因为你在Fragment
,你必须使用getActivity()
而不是yourClass.this
。
将您的 Toast
更改为:
Toast.makeText(getActivity(),"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG).show();
编辑
有关详细信息 Activity 您必须先创建一个 Activity
让我们称它为 DetailActivity
然后您必须将 onItemClickListener()
更改为:
serviceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String itemValue = (String) serviceList.getItemAtPosition(position);
int itemPosition = position;
Toast.makeText(getActivity(),"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG).show();
Intent intent = new Intent(getActivity(), DetailActivity.class);
intent.putExtra("DetailProductClicked", itemValue);
startActivity(intent);
}
});
所以在你的 DetailActivity.class
你必须把这个放在你的 onCreate()
String nameFruit = intent.getStringExtra("DetailProductClicked");
然后取决于你有什么Activity
你可以做这样的事情:
if (nameFruit.equals("Apple"){
tv.setText(nameFruit +" is a brand or fruit");
}
if (nameFruit.equals("Mango"){
tv.setText(nameFruit +" is a good fruit");
}
if (nameFruit.equals("Banana"){
tv.setText(nameFruit +" is a fruit");
}
要跟进@Skizo 的回答,请将您的 onItemClick 更改为
serviceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String itemValue = (String) serviceList.getItemAtPosition(position);
int itemPosition = position;
Intent nextIntent = new Intent(getActivity(), MyNextActivity.class);
// ... create a bundle to pass of the Object in your data at itemPosition
startActivity(nextIntent);
}
});
实际上,虽然我会像这样在片段中使用一个接口并告诉我的 Activity 开始新的 Activity 而不是片段:
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class ServiceFragment extends Fragment {
private ListView serviceList;
private String[] serviceString;
private ItemListener mCallback;
public ServiceFragment(){}
public interface ItemListener{
// use position or Object data here
void onItemClicked(int position);
}
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
mCallback = (ItemListener) activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_service, container, false);
//build the list service
serviceList = (ListView) rootView.findViewById(R.id.ServiceList);
serviceString = getResources().getStringArray(R.array.service_list);
ArrayAdapter<String> objAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1,serviceString );
serviceList.setAdapter(objAdapter);
//on click item Listener
serviceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String itemValue = (String) serviceList.getItemAtPosition(position);
int itemPosition = position;
mCallback.onItemClicked(itemPosition);
}
});
return rootView;
}//end of onCreateView
}//end of class ServiceFragment
然后在 Activity 中添加 implements ServiceFragment.ItemListener
:
@Override
public void onItemClicked(int position){
// Now handling this element via the Activity, much cleaner
Intent nextIntent = new Intent(this, MyNextActivity.class);
// ... create a bundle to pass of the Object in your data at itemPosition
startActivity(nextIntent);
}