问题
I am trying to create a ListView
in a Fragment
. The listView appear correctly but when I complied the code shows error.
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
What's wrong with my code? Why doesn't Toast
show up?
is it possible on click item start a detail activity where I can describe the meaning of item
Suppose List Item = Apple ,Mango, Banana onClick Item Details view = Apple is a brand or fruit, Banana is a fruit,Mango is a good fruit,
回答1:
What Wrong with my code ? Why the Toast does not show
Since you are on a Fragment
you must use getActivity()
instead of yourClass.this
.
Change your Toast
to this :
Toast.makeText(getActivity(),"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG).show();
EDIT
For a detail Activity you must create an Activity
first let's call it DetailActivity
then you have to change your onItemClickListener()
to this :
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);
}
});
So in your DetailActivity.class
you have to put this on your onCreate()
String nameFruit = intent.getStringExtra("DetailProductClicked");
Then depends what you have on this Activity
you can do something like :
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");
}
回答2:
To follow up @Skizo 's answer change your onItemClick to
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);
}
});
realistically though i would use an interface in the Fragment like so and tell my Activity to start the new Activity rather than the fragment:
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
and then add implements ServiceFragment.ItemListener
in your Activity:
@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);
}
来源:https://stackoverflow.com/questions/32596957/can-not-show-toast-in-onitemclicklistener-inside-fragment