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

发布于 2024-12-10 14:29:13 字数 11619 浏览 0 评论 0原文

我想在 ListView 中添加一个 GirdView ?是否可以 ? 为每次编辑设置不同的适配器

我在以下代码(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;
    }

}

I want to add a GirdView inside a ListView ? Is it possible ? where I set a different adapter for each

edit // upon the following code (gridView) deosn't appear why?

this is thew adapter for 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;
    }

}

This is the class of ListView(that show a GridView) with it's adapter class

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;
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的往事 2024-12-17 14:29:13

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

Your TimesAdapter does not contain any data. The getCount() method always returns 0 which is telling the OS that there are no entries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文