带有Imageview textview和另一个listview的Android Listview

时间:2017-10-31 05:43:28

标签: json

我想用imageview,textview和另一个listview显示listview。所有值都来自Json,两个列表都是动态的。我试过这个但是父列表工作正常,但第二个listview第一个posintion进入每个所有父子列表postion。请举例说明代码。

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

        <include
            android:id="@+id/tool_bar"
            layout="@layout/topbar" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/tool_bar"
            android:orientation="vertical"
            android:padding="20dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:background="#d9d9d9">

                <TextView
                    android:id="@+id/emp_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="10dp"
                    android:text="Employee Name: Oscar B.Snyder"
                    android:textSize="18dp" />

                <TextView
                    android:id="@+id/period_date"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/emp_name"
                    android:layout_marginLeft="10dp"
                    android:textSize="16dp"
                    android:text="Period Date:06/26/2017 - 07/02/2017" />

                <ListView
                    android:layout_margin="10dp"
                    android:id="@+id/list_main"
                    android:dividerHeight="2dp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/period_date"
                    android:layout_marginTop="10dp"></ListView>
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>



  // child xml

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

            <LinearLayout
                android:layout_marginTop="10dp"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:background="#a4a4a4">

                    <TextView
                        android:id="@+id/period_date"
                        android:layout_width="match_parent"
                        android:layout_height="40dp"
                        android:layout_below="@+id/emp_name"
                        android:padding="10dp"
                        android:layout_weight="30"
                        android:text="Period Date:06/26/2017 - 07/02/2017" />

                    <TextView
                        android:id="@+id/total_hours"
                        android:text="Total: 40.12"
                        android:layout_weight="70"
                        android:padding="10dp"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
                </LinearLayout>


                <ListView
                    android:id="@+id/list_sub"
                    android:layout_width="match_parent"
                    android:layout_height="250dp"></ListView>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_below="@+id/emp_name"
                    android:layout_marginTop="20dp"
                    android:background="#a4a4a4"
                    android:padding="10dp"
                    android:text="Employee Notes" />

                <EditText
                    android:id="@+id/edt_emp_note"
                    android:layout_width="match_parent"
                    android:layout_height="55dp"
                    android:background="#ffffff" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_below="@+id/emp_name"
                    android:layout_marginTop="10dp"
                    android:background="#a4a4a4"
                    android:padding="10dp"
                    android:text="Supervisor Notes" />

                <EditText
                    android:id="@+id/edt_supervisor_notes"
                    android:layout_width="match_parent"
                    android:layout_height="55dp"
                    android:background="#ffffff" />
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>

// second listview xml

<?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="20dp"
    android:orientation="horizontal"
    android:weightSum="100">
    <TextView
        android:id="@+id/txt_dayName"
        android:text="Sunday Oct 1"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="19" />

    <TextView
        android:id="@+id/txt_hours"
        android:text="0.0"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="27" />

    <Button
        android:id="@+id/btn_view"
        android:text="View"
        android:padding="10dp"
        android:layout_margin="5dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="27" />

    <Button
        android:id="@+id/btn_add"
        android:text="Add"
        android:layout_margin="5dp"
        android:padding="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="27" />

</LinearLayout>

//适配器类

public class AdapterDate extends BaseAdapter {

    private ArrayList<Week> dayArray = new ArrayList<Week>();
    private Context context;
    private LayoutInflater inflater;
    private HashMap<String, String> bodyParam = new HashMap<>();
    // private HashMap<Week, List<WeekDays>> dayHoursArray;
    private ArrayList<WeekDays> dayHoursArray = new ArrayList<>();
    // sub list
    AdapterSubdate adapterSubdate;
    // private ArrayList<WeekDays> dayHoursArray = new ArrayList<WeekDays>();
    public AdapterDate(Context context, ArrayList<Week> dayArray, ArrayList<WeekDays> dayHoursArray) {
        this.dayArray = dayArray;
        this.dayHoursArray = dayHoursArray;
        this.context = context;
        inflater = LayoutInflater.from(this.context);
    }
    @Override
    public int getCount() {
        return dayArray.size();
    }

    @Override
    public Object getItem(int position) {

        return dayArray.get(position);
    }

    @Override
    public long getItemId(int position) {

        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final MyViewHolder mViewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.week_child, null);
            mViewHolder = new MyViewHolder();

            mViewHolder.weekDate = (TextView) convertView.findViewById(R.id.period_date);
            mViewHolder.totalHours = (TextView) convertView.findViewById(R.id.total_hours);

            mViewHolder.ls = (ListView) convertView.findViewById(R.id.list_sub);


            mViewHolder.edtEmpNote = (EditText) convertView.findViewById(R.id.edt_emp_note);
            mViewHolder.edtSupNote = (EditText) convertView.findViewById(R.id.edt_supervisor_notes);

            convertView.setTag(mViewHolder);

        } else {
            mViewHolder = (MyViewHolder) convertView.getTag();
        }

        //mViewHolder.imgCompany.setImageResource(Integer.parseInt(companyList.get(position).getCompany_logo()));
        // Picasso.with(context).load(dayHoursArray.get(position).getCompany_logo()).into(mViewHolder.imgCompany);
        mViewHolder.weekDate.setText("Week Date: " + dayArray.get(position).getWeekDate());
        mViewHolder.totalHours.setText("Total " + dayArray.get(position).getTotalHours());
        // mViewHolder.ls.setAdapter(dayArray.get(position).getAdapterSubdate());


        adapterSubdate = new AdapterSubdate(context, dayHoursArray);
        mViewHolder.ls.setAdapter(adapterSubdate);


        mViewHolder.edtEmpNote.setText(dayArray.get(position).getEmpNote());
        mViewHolder.edtSupNote.setText(dayArray.get(position).getSupNote());

        return convertView;

    }

    public static class MyViewHolder {
        TextView weekDate, totalHours;
        EditText edtEmpNote, edtSupNote;
        ListView ls;

    }
}

//第二个适配器

public class AdapterSubdate extends BaseAdapter {

    private ArrayList<WeekDays> dayHoursArray = new ArrayList<WeekDays>();
    private Context context;
    private LayoutInflater inflater;
    private HashMap<String, String> bodyParam = new HashMap<>();


    public AdapterSubdate(Context context, ArrayList<WeekDays> dayHoursArray) {
        this.dayHoursArray = dayHoursArray;
        this.context = context;
        inflater = LayoutInflater.from(this.context);

    }

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

    @Override
    public Object getItem(int position) {

        return null;
    }

    @Override
    public long getItemId(int position) {

        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final MyViewHolder mViewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.week_sub_child, null);
            mViewHolder = new MyViewHolder();

            mViewHolder.dayName=(TextView)convertView.findViewById(R.id.txt_dayName);
            mViewHolder.hours=(TextView)convertView.findViewById(R.id.txt_hours);

            mViewHolder.btnAdd=(Button)convertView.findViewById(R.id.btn_add);
            mViewHolder.btnView=(Button)convertView.findViewById(R.id.btn_view);

            convertView.setTag(mViewHolder);

        } else {
            mViewHolder = (MyViewHolder) convertView.getTag();
        }

        //mViewHolder.imgCompany.setImageResource(Integer.parseInt(companyList.get(position).getCompany_logo()));
       // Picasso.with(context).load(dayHoursArray.get(position).getCompany_logo()).into(mViewHolder.imgCompany);
        mViewHolder.dayName.setText(dayHoursArray.get(position).getDayName());
        mViewHolder.hours.setText(dayHoursArray.get(position).getHours());
        return convertView;

    }

    public static class MyViewHolder {
        TextView dayName,hours;
        Button btnAdd,btnView;

    }


}

// post method

protected void onPostExecute(String result) {//result parameter having the response value of CallSoap
        super.onPostExecute(result);
        // mProgressDialog.dismiss();
        Log.e("DayWiseTimeSheet->", result);
        try {
            JSONObject jsonObject = new JSONObject(result);

            if (jsonObject.getString("status").equalsIgnoreCase("true")) {

                JSONArray jsonArray = jsonObject.getJSONArray("DayWiseTimeSheet");
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jobject = jsonArray.getJSONObject(i);


                    empName=(jobject.getString("EmpName"));
                    periodRange=(jobject.getString("PeriodRange"));
                    statusCode=(jobject.getString("statusCode"));
                    // set text
                    txtEmpName.setText("Employee: "+empName);
                    txtPeriodDate.setText("Period Range: "+periodRange);

                    JSONArray weekDays=jobject.getJSONArray("WeekDays");
                    for (int j=0;j<weekDays.length();j++){
                        JSONObject days=weekDays.getJSONObject(j);

                        Week week=new Week();
                        week.setWeekDate(days.getString("WeekDate"));
                        week.setTotalHours(days.getString("TotalHours"));
                        week.setEmpNote(days.getString("EmpNote"));
                        week.setSupNote(days.getString("SupNote"));
                        week.setDayStatus(days.getString("DayStatus"));

                        JSONArray day=days.getJSONArray("DayHrs");
                        for (int d=0; d < day.length(); d++){
                            JSONObject dayHrs=day.getJSONObject(d);



                            WeekDays wd=new WeekDays();
                            wd.setDayName(dayHrs.getString("DayName"));
                            wd.setHours(dayHrs.getString("Hours"));
                            wd.setActiveYN(dayHrs.getString("ActiveYN"));
                            dayHoursArray.add(wd);

                            adapterSubdate=new AdapterSubdate(SelectDayActivity.this,dayHoursArray);

                        }

                        dayArray.add(week);

                        Log.d("DayHours", String.valueOf(dayHoursArray));
                        Log.d("DayArray", String.valueOf(dayArray));
                        adapterDate=new AdapterDate(SelectDayActivity.this,dayArray,dayHoursArray);
                        ls.setAdapter(adapterDate);

                    }
                }
                mProgressDialog.dismiss();
            } else {
                mProgressDialog.dismiss();
                Toast.makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
            }

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

        }
    }

// json value

{"status":"true","message":"Day Wise Timesheet Generated Successfully","DayWiseTimeSheet":[{"EmpName":"Snyder B, Oscar","PeriodRange":"10-10-2017(10/01/2017 - 10/09/2017)","statusCode":7,"WeekDays":[{"WeekDate":"10/01/2017","TotalHours":0.00,"EmpNote":"","SupNote":"","DayStatus":7,"DayHrs":[{"DayName":"Sunday  Oct  1","Hours":0.00,"ActiveYN":true},{"DayName":"Monday  Oct  2","Hours":0.00,"ActiveYN":false},{"DayName":"Tuesday  Oct  3","Hours":0.00,"ActiveYN":false},{"DayName":"Wednesday  Oct  4","Hours":0.00,"ActiveYN":false},{"DayName":"Thursday  Oct  5","Hours":0.00,"ActiveYN":false},{"DayName":"Friday  Oct  6","Hours":0.00,"ActiveYN":false},{"DayName":"Saturday  Oct  7","Hours":0.00,"ActiveYN":false}]},{"WeekDate":"10/02/2017","TotalHours":40.50,"EmpNote":"","SupNote":"","DayStatus":7,"DayHrs":[{"DayName":"Monday  Oct  2","Hours":8.50,"ActiveYN":true},{"DayName":"Tuesday  Oct  3","Hours":8.00,"ActiveYN":true},{"DayName":"Wednesday  Oct  4","Hours":8.00,"ActiveYN":true},{"DayName":"Thursday  Oct  5","Hours":8.00,"ActiveYN":true},{"DayName":"Friday  Oct  6","Hours":8.00,"ActiveYN":true},{"DayName":"Saturday  Oct  7","Hours":0.00,"ActiveYN":true},{"DayName":"Sunday  Oct  8","Hours":0.00,"ActiveYN":true}]},{"WeekDate":"10/09/2017","TotalHours":40.00,"EmpNote":"","SupNote":"","DayStatus":7,"DayHrs":[{"DayName":"Monday  Oct  9","Hours":8.00,"ActiveYN":true},{"DayName":"Tuesday  Oct 10","Hours":8.00,"ActiveYN":true},{"DayName":"Wednesday  Oct 11","Hours":8.00,"ActiveYN":true},{"DayName":"Thursday  Oct 12","Hours":8.00,"ActiveYN":true},{"DayName":"Friday  Oct 13","Hours":8.00,"ActiveYN":true},{"DayName":"Saturday  Oct 14","Hours":0.00,"ActiveYN":true},{"DayName":"Sunday  Oct 15","Hours":0.00,"ActiveYN":true}]}]}]}

1 个答案:

答案 0 :(得分:0)

Ahhaa!这么久的问题......

我创建了一个单独的示例应用程序,而不是在这里描述并上传到github上。这里链接Sample Project

下载它,检查代码! 我试图用注释来解释代码的每个部分。

希望有所帮助:)