ListView

时间:2015-09-09 11:20:31

标签: android listview adapter

我正在制作一个屏幕,我想在其中显示SQlite表中的数据。 调试我能看到的是,当涉及到listView.setAdapter(适配器)时,数据会进入适配器但不会填充在listview中。 (我正在使用自定义适配器,自定义列表项)

也没有出现任何错误。

请帮忙。我在下面发布我的代码。

place_list_item:

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


    <TextView
        android:id="@+id/serial_number_textview_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:padding="5dp"
        android:text="1"
        android:textColor="@color/common_signin_btn_default_background"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/duration_textview_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.4"
        android:padding="5dp"
        android:text="hello"
        android:textColor="@color/common_signin_btn_default_background"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/distance_textview_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.4"
        android:padding="5dp"
        android:text="hello"
        android:textColor="@color/common_signin_btn_default_background"
        android:textSize="16sp" />


</LinearLayout>

trip_history_screen:

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

    <LinearLayout
        android:id="@+id/heading_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="#000000"
        android:gravity="center_horizontal">

        <TextView
            android:id="@+id/header_label_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="Trip Histroy"
            android:textColor="@android:color/white"
            android:textSize="18dp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/titles_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/heading_layout"
        android:orientation="horizontal"
        android:weightSum="1.0">


        <TextView
            android:id="@+id/serial_number_title_textview_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:padding="5dp"
            android:text="No."
            android:textSize="16sp" />

        <TextView
            android:id="@+id/duration_title_textview_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.4"
            android:padding="5dp"
            android:text="Duration"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/distance_title_textview_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.4"
            android:padding="5dp"
            android:text="Distance"
            android:textSize="16sp" />


    </LinearLayout>

    <ListView
        android:id="@+id/trip_history_listview_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/titles_layout"
        android:layout_marginBottom="50dp"
        android:cacheColorHint="#00000000"
        android:divider="@drawable/listview_devider_dockets"
        android:dividerHeight="2dp"
        android:paddingTop="10dp" />
</LinearLayout>

适配器:

import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.RareMediaCompany.MuditaMileage.DataClasses.ListData;
import com.RareMediaCompany.MuditaMileage.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by SANGEETA on 17-08-2015.
 */
public class TripHistoryDataAdapter extends ArrayAdapter {
    List list = new ArrayList();
    private Typeface typeface;
    Context mContext;
    int resource;
//public TripHistoryDataAdapter(Context context, int resource, Typeface typeface) {
//    this.mContext = context;
//    this.resource = resource;
//    this.typeface = typeface;
//
//    // super(context, resource);
//}

    public TripHistoryDataAdapter(Context context, int resource) {
        super(context, resource);
    }

    static class DataHandler {
        TextView number;
        TextView duration;
        TextView distanceCovered;
    }

    @Override
    public void add(Object object) {
        super.add(object);
        list.add(object);
    }

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

    @Override
    public Object getItem(int position) {
        return this.list.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row;
        row = convertView;
        DataHandler handler;
        Log.i("LstAdapter", "Inside getView");
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.place_list_item, parent, false);
            handler = new DataHandler();
            handler.number = (TextView) row.findViewById(R.id.serial_number_textview_id);
            handler.duration = (TextView) row.findViewById(R.id.duration_textview_id);
            handler.distanceCovered = (TextView) row.findViewById(R.id.distance_textview_id);
            row.setTag(handler);
        } else {
            handler = (DataHandler) row.getTag();
        }

        ListData historyDataProvider;
        historyDataProvider = (ListData) this.getItem(position);
        handler.number.setText(historyDataProvider.getNumber());
        handler.duration.setText(historyDataProvider.getTime());
        handler.distanceCovered.setText(historyDataProvider.getDistance());
        return row;
    }
}

片段:

public class TripHistoryFragment extends Fragment {
    // 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 MyDbHelper helper;
    private ListView listView;
    private TextView serialNumberTitle, timeTitle, distanceCoveredTitle;
    private TextView headerTextView;
    private List<ListData> dataList;
    private TripHistoryDataAdapter adapter;
    private Typeface typeface;
    private GetDataTask getDataTaskObj;
    // private OnFragmentInteractionListener mListener;


    public TripHistoryFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.trip_history_screen, container, false);
        typeface = Typeface.createFromAsset(getActivity().getAssets(),
                "fonts/AvantGarde.ttf");
        listView = (ListView) rootView.findViewById(R.id.trip_history_listview_id);
        serialNumberTitle = (TextView) rootView.findViewById(R.id.serial_number_title_textview_id);
        timeTitle = (TextView) rootView.findViewById(R.id.duration_title_textview_id);
        distanceCoveredTitle = (TextView) rootView.findViewById(R.id.distance_title_textview_id);
        headerTextView = (TextView) rootView.findViewById(R.id.header_label_id);
        adapter = new TripHistoryDataAdapter(getActivity(), R.layout.place_list_item);
        serialNumberTitle.setTypeface(typeface);
        timeTitle.setTypeface(typeface);
        distanceCoveredTitle.setTypeface(typeface);
        headerTextView.setTypeface(typeface);
        getDataTaskObj = new GetDataTask(getActivity());
     //   listView.setAdapter(adapter);
        if (android.os.Build.VERSION.SDK_INT >= 11) {
            // allows task to run parallely
            getDataTaskObj.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
                    (Void[]) null);
        } else {
            getDataTaskObj.execute();
        }
        return rootView;
    }


    public class GetDataTask extends AsyncTask<Void, Void, Void> {
        Context context;
        Dialog dialog;

        // ProgressDialog progressDialog;

        public GetDataTask(Context context) {
            this.context = context;

            dialog = Utils.showValidationDialog(context, "Loading ...",
                    "Please wait ", false);

           // listDataArr = new ArrayList<ListData>();
        }


        @Override
        protected Void doInBackground(Void... params) {
            helper = new MyDbHelper(getActivity());

            Cursor cursor = helper.getAllLocationSavedCursor();
            //String myHexColor = "#CC2233";
            if (cursor.moveToFirst()) {
                do {

                    String number = cursor.getString(cursor.getColumnIndex("id"));
                    String duration = cursor.getString(cursor.getColumnIndex("UnixTimeStamp"));
                    String distance = cursor.getString(cursor.getColumnIndex("distance"));

                    adapter.add(new ListData(number, duration, distance));


                    //list.add(map);


                } while (cursor.moveToNext());
                cursor.close();
            }


            return null;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            if (adapter != null && !adapter.isEmpty()) {
                listView.setAdapter(adapter);
                Toast.makeText(getActivity(), "Running trip data is here", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getActivity(), "No History Found!!", Toast.LENGTH_SHORT).show();
            }
            super.onPostExecute(aVoid);
        }
    }
}

如果有人发现某事或需要更多信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

更改trip_history_Screen.xml的线性布局。使用此

 <LinearLayout
>         android:id="@+id/titles_layout"
>         android:layout_width="match_parent"
>         android:layout_height="wrap_content"
>         android:layout_below="@+id/heading_layout"
>         android:orientation="horizontal"
>         android:weightSum="1.0">

而不是:

 <LinearLayout
        android:id="@+id/titles_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/heading_layout"
        android:orientation="horizontal"
        android:weightSum="1.0">