我可以在Listview项目中添加GridView吗?

时间:2011-10-17 12:04:40

标签: android listview gridview

我想在GirdView内添加ListView?可能吗 ?我为每个

设置了一个不同的适配器

编辑//在以下代码(gridView)上出现原因?

这是GridView的适配器

package com.android.utils;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.uis.R;


public class TimesAdapter extends BaseAdapter {

    private ArrayList<String> listTitle;
    private ArrayList<String> listDates;
    private Activity activity;

    public TimesAdapter(Activity activity, ArrayList<String> listDates,ArrayList<String> listTitle){
        this.listTitle=listTitle;
        this.listDates=listDates;
        this.activity=activity;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder view;
        LayoutInflater inflator = activity.getLayoutInflater();
        if(convertView==null){
            view = new ViewHolder();
            //inflate : inflate xml resources into object
            convertView = inflator.inflate(R.layout.gridview_times_row, null);
            view.tvTitle=(TextView) convertView.findViewById(R.id.tvTitle);
            view.tvDate=(TextView) convertView.findViewById(R.id.tvDate);

            convertView.setTag(view);

        }else{
            view = (ViewHolder) convertView.getTag();
        }
        view.tvTitle.setText(listTitle.get(position));      
        view.tvDate.setText(listDates.get(position));
        return convertView;
    }

    static class ViewHolder{
        public TextView tvTitle;
        public TextView tvDate;
    }

}

这是ListView(显示GridView)的类,带有它的适配器类

package com.android.uis;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.classes.Appointment;
import com.android.database.DatabaseHelper;
import com.android.networkservices.TimeTableWebService;
import com.android.utils.TimesAdapter;

public class DaysActivity extends Activity {
    private ListView lvDay;
    private Button bUpdateTimeTable,bChangeDate;
    private ArrayList<String> items;
    private ArrayList<Appointment> appointments;
    private ArrayList appointmentsArray[] = new ArrayList[7];
    private EfficientAdapter adap;
    private int mYear;
    private int mMonth;
    private int mDay;
    static final int DATE_DIALOG_ID = 0;
    private Activity activity;
    int dayIndex;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.day_layout);
        lvDay = (ListView)this.findViewById(R.id.lvDay);
        bUpdateTimeTable = (Button)this.findViewById(R.id.bUpdateTimeTable);
        bChangeDate = (Button)this.findViewById(R.id.bChangeDate);
        items = new ArrayList<String>();
        appointments = new ArrayList<Appointment>();
        lvDay.setClickable(true);
        activity = this;

        if(getIntent()!=null)
            if(getIntent().getExtras()!=null)
                if(getIntent().getExtras().getString("day")!="")
                    dayIndex = Integer.parseInt(getIntent().getExtras().getString("day"));
                else
                    dayIndex = -1;
            else
                dayIndex = -1;
        else
            dayIndex = -1;
        Log.d("dayIndex",dayIndex+"");
        bindList(dayIndex);// to bind list items


        lvDay.setOnItemClickListener(new OnItemClickListener(){
            public void onItemClick(AdapterView<?> parent, View view, int position,long id) {

            }
        });
        bUpdateTimeTable.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                DatabaseHelper db = new DatabaseHelper(getApplicationContext());

                // At first delete the previous data then retrieve the new one 
                    db.deteleAllAppointments();
                    appointments.clear();
                    items.clear();

                    TimeTableWebService ttws = new TimeTableWebService();
                    ttws.execute("http://mmu4bader.com/AppointmentJSon/getAppointments1");
                    ttws.getResult(getApplicationContext());
                    //bindList();
            }

        });
        bChangeDate.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                showDialog(DATE_DIALOG_ID);
                Toast.makeText(getApplicationContext(), "date", Toast.LENGTH_SHORT).show();
            }
        });
        // get the current date
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);

        // display the current date (this method is below)
        updateDisplay();


    }
    private DatePickerDialog.OnDateSetListener mDateSetListener =
            new DatePickerDialog.OnDateSetListener() {

                public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
                    mYear = year;
                    mMonth = monthOfYear;
                    mDay = dayOfMonth;
                    updateDisplay();
                }
    };
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this,mDateSetListener,mYear, mMonth, mDay);
        }
        return null;
    }
    private void updateDisplay() {
        // TODO Auto-generated method stub

    }
    private void bindList(int DayIndex){
        DatabaseHelper db = new DatabaseHelper(getApplicationContext());
        ArrayList<Appointment> tempAppointments = db.getAllAppointments();

        for(int i=0;i<tempAppointments.size();i++)
        {
            Date currentDate = new Date();// get current date
            Date eventDate = tempAppointments.get(i).mStartDate;
        //  int dateEqual = getZeroTimeDate(currentDate).compareTo(getZeroTimeDate(eventDate));

        //  Log.d("event day index",eventDate.getDay()+" "+eventDate);
        //  Log.d("current day index",currentDate.getDay()+" "+currentDate);

            if(eventDate.getDay()==DayIndex)
            {
                items.add(String.valueOf(tempAppointments.get(i).mTitle));
                appointments.add(tempAppointments.get(i));

            }

        }

        adap = new EfficientAdapter(this);
        adap.notifyDataSetChanged();
        lvDay.setAdapter(adap);
    }
    public static Date getZeroTimeDate(Date fecha) {
        Date res = fecha;
        Calendar calendar = Calendar.getInstance();

        calendar.setTime( fecha );
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        res = calendar.getTime();

        return res;
    }
    public void onResume() {
        super.onResume();
    }
    public class EfficientAdapter extends BaseAdapter implements Filterable{

        LayoutInflater inflater;
        Context context;

        public EfficientAdapter(Context context){
            inflater = LayoutInflater.from(context);
            this.context = context;
        }


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


        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return items.get(position);
        }


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


        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            ArrayList<String> listDates = new ArrayList<String>();
            ArrayList<String>listTitle = new ArrayList<String>();
            listDates.add("17/5/1987");
            listTitle.add("Gaza");
            if(convertView == null){
                convertView = inflater.inflate(R.layout.adaptor_timetable_content, null);
                holder = new ViewHolder();

                holder.gvDates = (GridView)convertView.findViewById(R.id.gvDates);
                holder.textLineTitle = (TextView)convertView.findViewById(R.id.textLineTitle);
                holder.textLineTime = (TextView)convertView.findViewById(R.id.textLineTime);

                convertView.setOnClickListener(new OnClickListener() {
                    private int pos = position;

                        public void onClick(View v) {
                        /*  Intent i = new Intent(context,RoutingMapActivity.class);
                            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            i.putExtra("Department", depts.get(pos).toBunlde());
                            getApplicationContext().startActivity(i);  */                       
                            }
                        });


                convertView.setTag(holder);
            }else{
                holder = (ViewHolder)convertView.getTag();
            }
            // Bind the data efficiently with the holder.
            try{

                holder.textLineTitle.setText(String.valueOf(appointments.get(position).mTitle));
                holder.textLineTime.setText(String.valueOf(appointments.get(position).mStartDate));
                holder.gvDates.setAdapter(new TimesAdapter(activity,listDates,listTitle));
                Log.d("-----adapter",listDates.size()+"");
                holder.gvDates.setOnItemClickListener(new OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                        // show time info.
                    }
                });

            }catch(Exception ex){

            }
            return convertView;
        }


        public Filter getFilter() {
            // TODO Auto-generated method stub
            return null;
        }


    }
    static class ViewHolder {
        TextView textLineTitle;
        TextView textLineTime;
        GridView gvDates;
    }

}

1 个答案:

答案 0 :(得分:3)

您的TimesAdapter不包含任何数据。 getCount()方法始终返回0,告诉操作系统没有条目。