如何在android

时间:2017-01-06 08:30:31

标签: android listview

我正在创建一个包含listview的android应用程序。
在listview中,我有一个可扩展的高度列表视图。对于父列表视图,我设置了一个限制,仅显示两个列表项。但在可扩展的列表视图中,我没有设置任何限制 我想知道如何在listview中为子视图设置限制(即可扩展列表视图)。下面是listview的一个适配器的代码:

  public class Daybook_adapter extends BaseAdapter {
    Context context;
    Activity activity;


    //DaybookSwipeMenuListView daybookdetailviewlist;
    public Daybook_adapter(Activity activity, ArrayList<Daybook> list) {

        //this.context = context;
        this.activity = activity;
        entriesdaybook = list;

    }


    @Override
    public int getCount() {

        return entriesdaybook.size();
    }

    @Override
    public Object getItem(int position) {

        return entriesdaybook.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {


        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.model_daybook, null);

        }


        final Daybook m = entriesdaybook.get(position);
        final TextView tv_date = (TextView) convertView.findViewById(R.id.tv_daybook_date);
        final TextView tv_cashin = (TextView) convertView.findViewById(R.id.tv_daybook_cashin);
        final TextView tv_cashout = (TextView) convertView.findViewById(R.id.tv_daybook_cashout);
        final TextView tv_totalamt = (TextView) convertView.findViewById(R.id.daybook_total_amt);
        final ImageView img_pdf = (ImageView) convertView.findViewById(R.id.img_printpdf);
        //final String s = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String s = m.getDate();
        emptyyz = (LinearLayout) convertView.findViewById(R.id.empty);
        // daybookdetailviewlist = (DaybookSwipeMenuListView) convertView.findViewById(R.id.detaillist_daybook);
        daybookdetailviewlist = (ExpandableHeightListView) convertView.findViewById(R.id.detaillist_daybook);

        String[] spiliter = s.split("-");
        String year = spiliter[0];
        String month = spiliter[1];
        String date = spiliter[2];
        if (month.startsWith("01")) {
            tv_date.setText(date + "Jan" + year);
        } else if (month.startsWith("02")) {
            tv_date.setText(date + "Feb" + year);
        } else if (month.startsWith("03")) {
            tv_date.setText(date + "Mar" + year);
        } else if (month.startsWith("04")) {
            tv_date.setText(date + "Apr" + year);
        } else if (month.startsWith("05")) {
            tv_date.setText(date + "May" + year);
        } else if (month.startsWith("06")) {
            tv_date.setText(date + "Jun" + year);
        } else if (month.startsWith("07")) {
            tv_date.setText(date + "Jul" + year);
        } else if (month.startsWith("08")) {
            tv_date.setText(date + "Aug" + year);
        } else if (month.startsWith("09")) {
            tv_date.setText(date + "Sep" + year);
        } else if (month.startsWith("10")) {
            tv_date.setText(date + "Oct" + year);
        } else if (month.startsWith("11")) {
            tv_date.setText(date + "Nov" + year);
        } else if (month.startsWith("12")) {
            tv_date.setText(date + "Dec" + year);
        }


   /* if (m.getUsertype().startsWith("singleworker")) {
        tv_cashin.setText("\u20B9" + "0");
    } else if (m.getUsertype().startsWith("groupworker")) {
        tv_cashin.setText("\u20B9" + "0");
    }*/

        tv_cashin.setText("\u20B9" + m.getCashin());
        tv_cashout.setText("\u20B9" + m.getCashout());
        double one = Double.parseDouble(m.getCashin());
        double two = Double.parseDouble(m.getCashout());
        double three = one + two;
        tv_totalamt.setText("\u20B9" + String.valueOf(three));
        // new DaybooklistAsynccTask().execute(s);
        databaseHandler = new DatabaseHandler(activity);
        daybooklists = databaseHandler.getAllDaywisedaybookdetails(s);
        adapter = new Daybooklist_adapter(activity, daybooklists);
        if (adapter != null) {
            if (adapter.getCount() > 0) {
                emptyy.setVisibility(View.GONE);
                daybookdetailviewlist.setAdapter(adapter);

            }
        } else {
            daybookdetailviewlist.setEmptyView(emptyyz);
        }
        daybookdetailviewlist.setExpanded(true);


        img_pdf.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                final android.support.v7.app.AlertDialog.Builder alert = new android.support.v7.app.AlertDialog.Builder(
                        activity);
                alert.setTitle(activity.getResources().getString(R.string.app_name));
                alert.setMessage("Click yes to Print Report for" + m.getDate());
                alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent pdfreport = new Intent(activity, Activity_Daybookpdf.class);
                        pdfreport.putExtra("date", m.getDate());
                        activity.startActivity(pdfreport);

                    }
                });
                alert.setNegativeButton("No", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();


                    }
                });

                alert.show();
                return false;
            }
        });


        return convertView;

    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                activity.getResources().getDisplayMetrics());
    }

    public void setTransactionList(ArrayList<Daybook> newList) {
        entriesdaybook = newList;
        notifyDataSetChanged();
    }



}

可扩展ListAdapter:

public class Daybooklist_adapter extends BaseAdapter {
Context context;
private Activity activity;
private LayoutInflater inflater;
private ArrayList<Daybooklist> daybooklists;
DatabaseHandler databaseHandler;

public Daybooklist_adapter(Activity activity, ArrayList<Daybooklist> daybooklists) {
    this.activity = activity;
    this.daybooklists = daybooklists;
}

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

@Override
public Object getItem(int i) {
    return daybooklists.get(i);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertview, ViewGroup parent) {
    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertview == null)
        convertview = inflater.inflate(R.layout.model_daybook_listentry, null);
    final TextView day_name = (TextView) convertview.findViewById(R.id.tv_daybook_name);
    final TextView day_description = (TextView) convertview.findViewById(R.id.tv_daybook_description);
    final TextView day_type = (TextView) convertview.findViewById(R.id.tv_daybook_type);
    final TextView day_amount = (TextView) convertview.findViewById(R.id.tv_daybook_amount);
    final TextView day_usertype = (TextView) convertview.findViewById(R.id.tv_usertype);
    final TextView day_time = (TextView) convertview.findViewById(R.id.tv_daybook_time);
    final ImageView day_check = (ImageView) convertview.findViewById(R.id.img_doneall);
    final TextView daybook_location = (TextView) convertview.findViewById(R.id.tv_daybook_location);
    databaseHandler = new DatabaseHandler(activity);
    // getting movie data for the row

    final Daybooklist m = daybooklists.get(position);
    if (m.getUsertype() != null && !m.getUsertype().isEmpty()) {
        if (m.getUsertype().startsWith("farmer") | m.getUsertype().startsWith("singleworker") | m.getUsertype().startsWith("groupworker") | m.getUsertype().startsWith("payvehicle")) {
            if (m.getUsertype().startsWith("farmer")) {
                day_name.setText(m.getName());
                day_description.setText(m.getDescription());
                String locat = String.valueOf(databaseHandler.getfarmerlocation(m.getMobileno()));
                locat = locat.replaceAll("\\[", "").replaceAll("\\]", "");
                Log.e("farmerlocation", locat);
                daybook_location.setText(locat);
                day_type.setText(m.getType());
                if (m.getName() != null && m.getName().startsWith("no")) {
                    day_name.setText(" ");
                } else if (m.getDescription() != null && m.getDescription().startsWith("no")) {
                    day_description.setText(" ");
                }

                day_amount.setText("\u20B9" + m.getExtraamt());
                if (m.getAmountout().startsWith("0.0") | m.getAmountout().startsWith("0")) {
                    day_amount.setTextColor(activity.getResources().getColor(R.color.green));
                    Log.e("Amountout", m.getAmountout());
                    day_check.setVisibility(View.INVISIBLE);
                } else {
                    day_amount.setTextColor(activity.getResources().getColor(R.color.album_title));
                    Log.e("Amountout", m.getAmountout());
                    day_check.setVisibility(View.VISIBLE);
                }

                day_time.setText(m.getCtime());
            } else {
                day_name.setText(m.getName());
                day_description.setText(m.getDescription());
                daybook_location.setText(m.getType());
                day_type.setText(m.getType());
                if (m.getName() != null && m.getName().startsWith("no")) {
                    day_name.setText(" ");
                } else if (m.getDescription() != null && m.getDescription().startsWith("no")) {
                    day_description.setText(" ");
                }

                day_amount.setText("\u20B9" + m.getExtraamt());
                if (m.getAmountout().startsWith("0.0") | m.getAmountout().startsWith("0")) {
                    day_amount.setTextColor(activity.getResources().getColor(R.color.green));
                    Log.e("Amountout", m.getAmountout());
                    day_check.setVisibility(View.INVISIBLE);
                } else {
                    day_amount.setTextColor(activity.getResources().getColor(R.color.album_title));
                    Log.e("Amountout", m.getAmountout());
                    day_check.setVisibility(View.VISIBLE);
                }

                day_time.setText(m.getCtime());
            }


        } else if (m.getUsertype().startsWith("advancefarmer") | m.getUsertype().startsWith("workeradvance") | m.getUsertype().startsWith("kgroupadvance") | m.getUsertype().startsWith("otherexpense") | m.getUsertype().startsWith("vehicle")) {
            if (m.getUsertype().startsWith("advancefarmer")) {
                day_name.setText(m.getName());
                day_description.setText(m.getDescription());
                day_type.setText(m.getType());
                String locat = String.valueOf(databaseHandler.getfarmerlocation(m.getMobileno()));
                locat = locat.replaceAll("\\[", "").replaceAll("\\]", "");
                Log.e("farmerlocation", locat);
                daybook_location.setText(locat);
                if (m.getName() != null && m.getName().startsWith("no")) {
                    day_name.setText(" ");
                } else if (m.getDescription() != null && m.getDescription().startsWith("no")) {
                    day_description.setText(" ");
                }
                Log.e("amountout", m.getAmountout());
                day_amount.setText("\u20B9" + m.getAmountout());
                day_time.setText(m.getCtime());
            } else {
                day_name.setText(m.getName());
                day_description.setText(m.getType());
                day_type.setText(m.getType());
                daybook_location.setText(m.getDescription());
                if (m.getName() != null && m.getName().startsWith("no")) {
                    day_name.setText(" ");
                } else if (m.getDescription() != null && m.getDescription().startsWith("no")) {
                    day_description.setText(" ");
                }
                Log.e("amountout", m.getAmountout());
                day_amount.setText("\u20B9" + m.getAmountout());
                day_time.setText(m.getCtime());
            }


        } else if (m.getUsertype().startsWith("buyer")) {
            day_name.setText(m.getName());
            day_description.setText(m.getDescription());
            day_amount.setText("\u20B9" + m.getAmountin());
            day_type.setText(" ");
            day_time.setText(m.getCtime());
            daybook_location.setText(m.getType());
        }
        if (m.getUsertype().startsWith("farmer")) {
            day_usertype.setText("F");
            day_usertype.setBackgroundResource(R.drawable.textview_farmer);
        } else if (m.getUsertype().startsWith("advancefarmer")) {
            day_usertype.setText("FA");
            day_usertype.setBackgroundResource(R.drawable.textview_farmer);
        } else if (m.getUsertype().startsWith("singleworker")) {
            day_usertype.setText("W");
            day_usertype.setBackgroundResource(R.drawable.textview_worker);
        } else if (m.getUsertype().startsWith("workeradvance")) {
            day_usertype.setText("WA");
            day_usertype.setBackgroundResource(R.drawable.textview_worker);
        } else if (m.getUsertype().startsWith("groupworker")) {
            day_usertype.setText("G");
            day_usertype.setBackgroundResource(R.drawable.textview_worker);
        } else if (m.getUsertype().startsWith("kgroupadvance")) {
            day_usertype.setText("GA");
            day_usertype.setBackgroundResource(R.drawable.textview_worker);
        } else if (m.getUsertype().startsWith("otherexpense")) {
            day_usertype.setText("E");
            day_usertype.setBackgroundResource(R.drawable.textview_otherexpense);
        } else if (m.getUsertype().startsWith("vehicle")) {
            day_usertype.setText("V");
            day_usertype.setBackgroundResource(R.drawable.textview_vehicle);

        } else if (m.getUsertype().startsWith("gsalary")) {
            day_usertype.setText("GS");
            day_usertype.setBackgroundResource(R.drawable.textview_worker);
        } else if (m.getUsertype().startsWith("isalary")) {
            day_usertype.setText("WS");
            day_usertype.setBackgroundResource(R.drawable.textview_worker);
        } else if (m.getUsertype().startsWith("payvehicle")) {
            day_usertype.setText("VP");
            day_usertype.setBackgroundResource(R.drawable.textview_vehicle);
        } else if (m.getUsertype().startsWith("buyer")) {
            day_usertype.setText("B");
            day_usertype.setBackgroundResource(R.drawable.textview_buyer);
        }
    }


    convertview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (m.getUsertype().startsWith("farmer") | m.getUsertype().startsWith("advancefarmer")) {
                String name = m.getName();
                String mobno = m.getMobileno();
                Intent a = new Intent(activity, FarmerLedgerView_Activity.class);
                a.putExtra("farmername", name);
                a.putExtra("farmermobno", mobno);
                activity.startActivity(a);
            } else if (m.getUsertype().startsWith("singleworker") | m.getUsertype().startsWith("workeradvance")) {
                String name = m.getName();
                String mobno = m.getMobileno();
                Intent c = new Intent(activity, WorkerLedger_Activity.class);
                c.putExtra("workername", name);
                c.putExtra("workermobno", mobno);
                activity.startActivity(c);
            } else if (m.getUsertype().startsWith("groupworker") | m.getUsertype().startsWith("kgroupadvance")) {
                String name = m.getName();
                String mobno = m.getMobileno();
                Intent g = new Intent(activity, GroupWorkerLedger_Activity.class);
                g.putExtra("groupname", name);
                g.putExtra("workermobno", mobno);
                activity.startActivity(g);
            } else if (m.getUsertype().startsWith("isalary")) {
                String name = m.getName();
                String mobno = m.getMobileno();
                Intent d = new Intent(activity, Worker_paymenthistory_Activity.class);
                d.putExtra("workername", name);
                d.putExtra("workermobno", mobno);
                activity.startActivity(d);
            } else if (m.getUsertype().startsWith("bsalary")) {
                Intent aa = new Intent(activity, WorkerListActivity.class);
                activity.startActivity(aa);
            } else if (m.getUsertype().startsWith("vehicle") | m.getUsertype().startsWith("payvehicle")) {
                //// TODO: 19-Dec-16  enable this
                Intent s = new Intent(activity, VehicleList_activity.class);
                activity.startActivity(s);
            } else if (m.getUsertype().startsWith("otherexpense")) {
                Intent b = new Intent(activity, OtherExpenseList.class);
                activity.startActivity(b);
            } else if (m.getUsertype().startsWith("buyer")) {
                String name = m.getName();
                String mobno = m.getMobileno();
                Intent buyer = new Intent(activity, BuyerLedgerViewActivity.class);
                buyer.putExtra("buyername", name);
                buyer.putExtra("buyermobno", mobno);
                activity.startActivity(buyer);

            }
        }
    });
    return convertview;
}

private int dp2px(int dp) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
            context.getResources().getDisplayMetrics());
}

1 个答案:

答案 0 :(得分:0)

在Daybooklist_adapter中

 @Override
    public int getCount() {
    if(daybooklists.size()>2)
        return 2;
    else 
    return daybooklists.size();
    }