2017-10-31 20 views
0

imageview、textview、および別のlistview.Allの値はlistviewを表示したいと思います。両方の値は動的です。私はこれを試していますが、親リストは正しく機能していますが、すべての親の子リストのpostion.pleaseコードで例を挙げてください。Androidview、Imageview textviewと別のリストビュー

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

// 2番目のリストビューの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; 

    } 
} 

// 2番目のアダプタ

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メソッド

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値

{"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

コードを提供する方が良いでしょう。この時点で私が理解したように、Javaコードで2番目のリストビューを動的に作成することで、この問題を解決できます。 (私はあなたがXMLファイルで2番目のリストビューを作成したと仮定します) – shashidhara

+0

あなたの返信ありがとう、私はすべてのコードを添付しました。 – Suman

+0

あなたがリストビューを入れ子にすることはできません。 [ExpandableListView](https://developer.android.com/reference/android/widget/ExpandableListView.html)に行くことも、[this](https://stackoverflow.com/questions/29306500)の回答に従うこともできます。/how-to-use-nested-listview-in-android) – shashidhara

答えて

0

Ahhaa!長い質問...

ここで説明する代わりに別のサンプルアプリケーションを作成し、githubにアップロードしました。ここにリンクSample Project

それをダウンロードし、検査コード! コメントを使用してコードの各セクションを説明しようとしました。

助けを借りてください:

+0

サンプルプロジェクトをありがとう、私はプロジェクトを見てきましたが、内部リストビューの実装はありません(DayHrs配列リストはWeekday arraylist内にあります)。 – Suman

+0

を参照してください。 'DayHrs array'を親リストとして、' WeekDays'を子リストとして入れてください。あなたの要件に応じて 'class ParentListViewData'と' class ChildListViewData'を変更してください。 –

+0

しかし、そこには、親リストの位置に子リストと2つの編集テキストがあります。子XMLを見てください。 – Suman

関連する問題