由asyncktask和BaseAdapter实现的ListView不会出现在片段中

问题描述 投票:1回答:2

我正在尝试使用BaseAdapter和Asyncktask(从DB实现它)创建一个列表视图,但它没有出现错误记录或压缩

这是我的代码:

listclasses.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">

  <ListView
      android:id="@+id/listetud"
      android:layout_width="match_parent"
      android:layout_height="match_parent" >
  </ListView>
</LinearLayout>

classes.xml

单排

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dp">

        <TextView
            android:id="@+id/matclass"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:drawableLeft="@drawable/schoolicn"
            android:text=" :"
            android:typeface="monospace" />

        <TextView
            android:id="@+id/profclass"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:drawableLeft="@drawable/teachericn"
            android:text="  :"
            android:typeface="monospace" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp" >

            <TextView
                android:id="@+id/dateclass"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/planicn"
                android:typeface="serif" />

            <TextView
                android:id="@+id/heurclass"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:drawableLeft="@drawable/alarmicn"
                android:typeface="serif" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp" >

            <TextView
                android:id="@+id/locclass"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/location"
                android:text=" :"
                android:typeface="serif" />

            <TextView
                android:id="@+id/priceclass"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:drawableLeft="@drawable/priceicn"
                android:text=" :"
                android:typeface="serif" />

        </LinearLayout>

    </LinearLayout>

Java代码:

my adapter.Java

public final class MyAdapter extends BaseAdapter {
    private  List<Item> mItems;
    private final LayoutInflater mInflater;
    ListView ListItem;
    private Context context;


    public MyAdapter(Context context, List<Item> mItems) {
        this.context= context;
        this.mItems=mItems;

        mInflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);



    }



    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Item getItem(int i) {
        return mItems.get(i);
    }

    @Override
    public long getItemId(int i) {
       return i;

    }


    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View v = view;

        TextView matiere;
        TextView prof;
        TextView date;
        TextView heure;
        TextView location;
        TextView price;


        if (v == null) {
            v = mInflater.inflate(R.layout.classes, viewGroup, false);
            v.setTag(R.id.matclass, v.findViewById(R.id.matclass));
            v.setTag(R.id.profclass, v.findViewById(R.id.profclass));
            v.setTag(R.id.dateclass, v.findViewById(R.id.dateclass));
            v.setTag(R.id.heurclass, v.findViewById(R.id.heurclass));
            v.setTag(R.id.locclass, v.findViewById(R.id.locclass));
            v.setTag(R.id.priceclass, v.findViewById(R.id.priceclass));
        }


        matiere = (TextView) v.getTag(R.id.matclass);
        prof = (TextView) v.getTag(R.id.profclass);
        date = (TextView) v.getTag(R.id.dateclass);
        heure = (TextView) v.getTag(R.id.heurclass);
        location = (TextView) v.getTag(R.id.locclass);
        price = (TextView) v.getTag(R.id.priceclass);

         Item item = getItem(i);


        matiere.setText(item.matiere);
        prof.setText(item.prof);
        date.setText(item.date);
        heure.setText(item.heure);
        location.setText(item.location);
        price.setText(item.price);

         ListItem = (ListView) v.findViewById(R.id.listetud);


        return v;
    }

    public static class Item {
        public final String matiere;
        public final String prof;
        public final String date;
        public final String heure;
        public final String location;
        public final String price;



        Item(String matiere, String prof,String date,String heure,String location,String price) {
            this.matiere = matiere;
            this.prof = prof;
            this.date = date;
            this.heure = heure;
            this.location = location;
            this.price = price;



        }
    }

ClassesLayout

public class ClassesLayout extends Fragment {

    View view;
    ListView list;
    private  List<MyAdapter.Item> mItems  = new ArrayList<MyAdapter.Item>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate( R.layout.listclasses, null);


        new GetClasses().execute();

        return view;


    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


    }

    class GetClasses extends AsyncTask<Void, Void, Void> {
        JSONParser jsonParser = new JSONParser();
        private static final String TAG_SUCCESS = "success";
        private String URL_CLASSES = "http://prof-finder.aba.ae/classes.php";





        @Override
        protected Void doInBackground(Void... arg0) {
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            JSONObject jsonObj = jsonParser.makeHttpRequest(URL_CLASSES,
                    "GET", params);
            // String json = jsonParser.makeServiceCall(URL_CATEGORIES, ServiceHandler.GET);

            Log.e("Create log", jsonObj.toString());

            try {
                int success = jsonObj.getInt(TAG_SUCCESS);
                Log.e("Response: ", "> " + success);
                // JSONObject jsonObj = new JSONObject(json);
                if (jsonObj != null) {
                    JSONArray classes = jsonObj
                            .getJSONArray("classe");
                    Log.e("Create log", classes.toString());

                    for (int i = 0; i < classes.length(); i++) {
                        JSONObject clasObj = (JSONObject) classes.get(i);
                        mItems.add(new MyAdapter.Item(clasObj.getString("matiere"),clasObj.getString("prof"),clasObj.getString("date"),clasObj.getString("heure"),clasObj.getString("adresse"),clasObj.getString("price")));
                    }
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }


            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

            MyAdapter adapter = new MyAdapter(view.getContext(), mItems);
            list = (ListView) view.findViewById(R.id.listetud);
            list.setAdapter(adapter);



// get data from the table by the ListAdapter


        }

    }


}

我用它来调用我的活动中的片段,它可以用于其他片段

 fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_content,new ClassesLayout() , "Accueil")
                .addToBackStack(null)
                .commit();

我真的通过了一个星期来搜索问题,我尝试了很多方法来做到这一点,但没有任何结果

android android-layout listview android-fragments baseadapter
2个回答
0
投票

尝试

@Override
public int getCount() {
    return mItems.size();
}

你需要告诉adapter你在List有多少物品。


0
投票

试试这个

 @Override
 public int getCount() {
    return mItems.size();
 }
© www.soinside.com 2019 - 2024. All rights reserved.