如何修复此片段类中的setcontentview错误和内部类错误?

时间:2016-06-14 07:48:16

标签: java android android-fragments

所以这是一个片段类。我正在尝试使用baseadaptor向listview添加一些项目。我是初学者所以我跟着这个问题。 How to customize listview using baseadapter 但现在我在第一行得到setcontentview错误(无法解决错误)。 72和内部类错误在147.所以请帮我修复它。   包布局;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.buckydroid.app.droidcpu.R;


public class ussd extends Fragment {
    ListView l1;
    String[] t1={"ussd1","ussd2"};
    String[] d1={"le1","ln2"};
    private View myfragment;
    int[] i1 ={R.drawable.ic_launcher,R.drawable.ic_launcher};
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private dataListAdapter.OnFragmentInteractionListener mListener;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment ussd.
     */
    // TODO: Rename and change types and number of parameters
    public static ussd newInstance(String param1, String param2) {
        ussd fragment = new ussd();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }
    public ussd() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_ussd, container, false);
        myfragment =  inflater.inflate(R.layout.fragment_ussd, container, false);
        //getting error at thus part "cannot resolve setcontentview"
        setContentView(R.layout.fragment_ussd);
        l1=(ListView)myfragment.findViewById(R.id.listView);
        l1.setAdapter(new dataListAdapter(t1,d1,i1));
    }

    class dataListAdapter extends BaseAdapter {
        String[] Title, Detail;
        int[] imge;

        dataListAdapter() {
            Title = null;
            Detail = null;
            imge=null;
        }

        public dataListAdapter(String[] text, String[] text1,int[] text3) {
            Title = text;
            Detail = text1;
            imge = text3;

        }

        public int getCount() {
            // TODO Auto-generated method stub
            return Title.length;
        }

        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = getLayoutInflater(Bundle.EMPTY);
            View row;
            //getting error at this part too "unconditional layout inflation from view adaptor
            row = inflater.inflate(R.layout.listing, parent, false);
            TextView title, detail;
            ImageView i1;
            title = (TextView) row.findViewById(R.id.title);
            detail = (TextView) row.findViewById(R.id.detail);
            i1=(ImageView)row.findViewById(R.id.img);
            title.setText(Title[position]);
            detail.setText(Detail[position]);
            i1.setImageResource(imge[position]);

            return (row);
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }




    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }






        }
    }

1 个答案:

答案 0 :(得分:0)

onCreateView中,您将返回第一行的膨胀布局。将其更改为

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        myfragment =  inflater.inflate(R.layout.fragment_ussd, container, false);
        //getting error at thus part "cannot resolve setcontentview"
        l1=(ListView)myfragment.findViewById(R.id.listView);
        l1.setAdapter(new dataListAdapter(t1,d1,i1));
        return myfragment;
        }