无法在Fragment中的OnItemClickListener中显示Toast

时间:2015-09-15 22:51:44

标签: android android-fragments android-toast

我正在尝试在ListView中创建Fragment。 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没有出现?

是否可以在点击项目上启动详细活动,我可以在其中描述项目

的含义

假设 列表项目= Apple,Mango,Banana onClick项目详细信息视图= Apple是一个品牌或水果,香蕉是一种水果,芒果是一种很好的水果,

2 个答案:

答案 0 :(得分:1)

要跟进@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

然后在您的活动中添加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);

 }

答案 1 :(得分:0)

  

我的代码有什么问题?为什么Toast没有显示

由于您使用Fragment必须使用getActivity()而不是yourClass.this

将您的Toast更改为:

Toast.makeText(getActivity(),"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG).show();

修改

对于详细活动,您必须先创建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");
}