在片段中填充ListView

时间:2018-08-14 07:22:32

标签: listview android-fragments android-viewpager android-tablayout

我一直在使用OnCreate()Adapter类从Model方法创建并填充Listviews。 但是,我需要创建一个Tablayout并在每个Tab中显示一个ListView,并且由于每个Tab都扩展了Fragment,所以它不能以相同的方式工作。

这是我的 Tab1 课:

public class Tab1 extends Fragment {

    ArrayList<Model> model;
    private static Adapter adapter;
    ListView listView;

    //Overriden method onCreateView
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

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

        listView=(ListView)view.findViewById(R.id.list);


        model = new ArrayList<>();

        model.add(new Model("AAA","aaa",23));
        model.add(new Model("BBB","bbb",30));
        model.add(new Model("CCC","ccc",23));

                adapter = new Adapter(model, getActivity());
                listView.setAdapter(adapter);


        return view;
    }
}

我的适配器类:

public class Adapter extends ArrayAdapter<Model> implements View.OnClickListener{

    private ArrayList<Model> dataSet;
    Context mContext;


    // View lookup cache
    private class ViewHolder {
        TextView txtNom;
        TextView txtPrenom;
        TextView txtAge;
    }


    public Adapter(ArrayList<Model> data, Context context) {
        super(context, R.layout.row_item, data);
        this.dataSet = data;
        this.mContext=context;
        //startUpdateTimer();
    }


    @Override
    public void onClick(View v) {
        int position=(Integer) v.getTag();
        Object object= getItem(position);
        Model Model=(Model)object;

        switch (v.getId())
        {
            case R.id.AgeTV:

                Snackbar.make(v, "Age : " +Model.getAge(), Snackbar.LENGTH_LONG)
                        .setAction("No action", null).show();
                break;
        }
    }

    private int lastPosition = -1;


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        Model model = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        final ViewHolder viewHolder; // view lookup cache stored in tag

        final View result;

        if (convertView == null) {

            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.row_item, parent, false);
            viewHolder.txtNom = (TextView) convertView.findViewById(R.id.NomTV);
            viewHolder.txtPrenom = (TextView) convertView.findViewById(R.id.PrenomTV);
            viewHolder.txtAge = (TextView) convertView.findViewById(R.id.AgeTV);


            result=convertView;
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
            result=convertView;
        }

        lastPosition = position;

        viewHolder.txtNom.setText(model.getNom());
        viewHolder.txtPrenom.setText(model.getPrenom());
        viewHolder.txtAge.setText(model.getAge());

        viewHolder.txtAge.setOnClickListener(this);
        viewHolder.txtAge.setTag(position);

        // Return the completed view to render on screen
        return convertView;
    }

}

我的 Model 类:

public class Model {

    String Nom, Prenom;
    int Age;

    public Model(String nom, String prenom, int age) {
        Nom = nom;
        Prenom = prenom;
        Age = age;
    }

    public String getNom() {
        return Nom;
    }

    public void setNom(String nom) {
        Nom = nom;
    }

    public String getPrenom() {
        return Prenom;
    }

    public void setPrenom(String prenom) {
        Prenom = prenom;
    }

    public int getAge() {
        return Age;
    }

    public void setAge(int age) {
        Age = age;
    }
}

它不起作用,我也没弄错我得到的错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.production.achour_ar.tablayouttest, PID: 5759
                  android.content.res.Resources$NotFoundException: String resource ID #0x17
                      at android.content.res.Resources.getText(Resources.java:342)
                      at android.widget.TextView.setText(TextView.java:4805)
                      at com.production.achour_ar.tablayouttest.Adapter.getView(Adapter.java:84)
                      at android.widget.AbsListView.obtainView(AbsListView.java:2413)
                      at android.widget.ListView.makeAndAddView(ListView.java:1975)
                      at android.widget.ListView.fillDown(ListView.java:709)
                      at android.widget.ListView.fillFromTop(ListView.java:770)
                      at android.widget.ListView.layoutChildren(ListView.java:1749)
                      at android.widget.AbsListView.onLayout(AbsListView.java:2205)
                      at android.view.View.layout(View.java:17564)
                      at android.view.ViewGroup.layout(ViewGroup.java:5656)
                      at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
                      at android.view.View.layout(View.java:17564)
                      at android.view.ViewGroup.layout(ViewGroup.java:5656)
                      at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1775)
                      at android.view.View.layout(View.java:17564)
                      at android.view.ViewGroup.layout(ViewGroup.java:5656)
                      at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:142)
                      at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:41)
                      at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1556)
                      at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:888)
                      at android.view.View.layout(View.java:17564)
                      at android.view.ViewGroup.layout(ViewGroup.java:5656)
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                      at android.view.View.layout(View.java:17564)
                      at android.view.ViewGroup.layout(ViewGroup.java:5656)
                      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                      at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                      at android.view.View.layout(View.java:17564)
                      at android.view.ViewGroup.layout(ViewGroup.java:5656)
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                      at android.view.View.layout(View.java:17564)
                      at android.view.ViewGroup.layout(ViewGroup.java:5656)
                      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                      at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                      at android.view.View.layout(View.java:17564)
                      at android.view.ViewGroup.layout(ViewGroup.java:5656)
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                      at com.android.internal.policy.DecorView.onLayout(DecorView.java:755)
                      at android.view.View.layout(View.java:17564)
                      at android.view.ViewGroup.layout(ViewGroup.java:5656)
                      at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2415)
                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2130)
                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1286)
                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6551)
                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
                      at android.view.Choreographer.doCallbacks(Choreographer.java:683)
                      at android.view.Choreographer.doFrame(Choreographer.java:619)
                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
                      at android.os.Handler.handleCallback(Handler.java:751)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6312)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)

似乎片段处理方式不同。所以我想知道我在想什么。 如果根本不是从Tablayout填充每个Tab中的ListView的正确方法,我想知道我该怎么做。

0 个答案:

没有答案