0

I want to show listview with imageview,textview and another listview.All values are from Json and both list are dynamic.I have tried this but parent list working propery but second listview first posintion coming in every all parent child list postion.please give example with code.

    <?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>

// Adapter Class

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;

    }
}

// second adapter

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}]}]}]}
Suman
  • 1
  • 1
  • 1
    It will be better if you provide the code. At this point as I understood, you can solve this problem by creating the second listview dynamically in Java code. (I assume that you have created the second list view in XML file.) – Shashidhara Oct 31 '17 at 06:04
  • Thanks for Your reply,I have attached the all code.Please help – Suman Oct 31 '17 at 07:41
  • As I know you cannot nest the listview. You can go for [ExpandableListView](https://developer.android.com/reference/android/widget/ExpandableListView.html) or You can follow the answer from [this](https://stackoverflow.com/questions/29306500/how-to-use-nested-listview-in-android) – Shashidhara Oct 31 '17 at 14:59

1 Answers1

0

Ahhaa! so long question...

I have created a separate sample application instead of describing here and uploaded on github. Here link Sample Project

download it, inspect code! I have tried to explain each section of code using comments.

Hope that help :)

Dee Nix
  • 170
  • 1
  • 13
  • Thank you so much for the Sample Project,I have seen the project but there is no implementation of inner listview that is( DayHrs array list inside WeekDays arraylist). please see that – Suman Nov 01 '17 at 06:01
  • put `DayHrs array` as a parent list and `WeekDays` as a child list. Change `class ParentListViewData` and `class ChildListViewData` as per your requirement. – Dee Nix Nov 01 '17 at 07:01
  • Ok,but there there will be child list and two edit text in every parent list position.Please see the child xml. – Suman Nov 01 '17 at 09:30