2017-03-28 153 views
-1

我使用ListView來填充JSON數據。我想要顯示所有相應的值,但在ListView中只設置了一個JSON數據。爲什麼所有的值都沒有設置爲ListItem。ListView只顯示一個項目?

test_layout

<?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="match_parent" 
    android:orientation="vertical"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#2E353D" 
     android:orientation="vertical"> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="40dp" 
      android:layout_gravity="center_horizontal" 
      android:orientation="horizontal"> 

      <ImageView 
       android:layout_width="30dp" 
       android:layout_height="match_parent" 
       android:padding="3dp" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_vertical" 
       android:layout_marginLeft="8dp" 
       android:text="Class Schedule" 
       android:textColor="#fff" 
       android:textSize="17dp" 
       android:textStyle="bold" /> 

     </LinearLayout> 
    </LinearLayout> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <LinearLayout 
       android:id="@+id/sunday" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="#008b8b" 
       android:orientation="vertical"> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center_horizontal" 
        android:padding="6dp" 
        android:text="SUNDAY" 
        android:textColor="#fff" 
        android:textStyle="bold" /> 
      </LinearLayout> 


      <LinearLayout 
       android:id="@+id/sunday_class" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical"> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="#A4ACB0" 
        android:orientation="horizontal" 
        android:padding="5dp"> 

        <TextView 
         android:layout_width="0dp" 
         android:layout_height="match_parent" 
         android:layout_weight="1" 
         android:gravity="center_horizontal" 
         android:padding="5dp" 
         android:text="Subject" 
         android:textSize="12dp" 
         android:textStyle="bold" /> 

        <TextView 
         android:layout_width="0dp" 
         android:layout_height="match_parent" 
         android:layout_weight="1" 
         android:gravity="center_horizontal" 
         android:padding="5dp" 
         android:text="Teacher" 
         android:textSize="12dp" 
         android:textStyle="bold" /> 

        <TextView 
         android:layout_width="0dp" 
         android:layout_height="match_parent" 
         android:layout_weight="1" 
         android:gravity="center_horizontal" 
         android:padding="5dp" 
         android:text="Time" 
         android:textSize="12dp" 
         android:textStyle="bold" /> 
       </LinearLayout> 

       <ListView 
        android:id="@+id/list_class_schedule" 
        android:layout_width="match_parent" 
        android:layout_height="fill_parent" /> 

       <View 
        android:layout_width="match_parent" 
        android:layout_height="1dp" 
        android:background="#C5CED1" /> 
      </LinearLayout> 

     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

FragmentClassTest

public class FragmentClassTest extends Fragment implements View.OnClickListener { 


    LinearLayout sunday, sundaySchedule, monday, mondayClass, tuesday, tuesdayClass, wednesday, wednesdayClass, thrusday, thrusdayClass, friday, fridayClass; 

    public static final String Navigation_URL = "http://192.168.100.5:84/api/employeeApi/timeTableByClass"; 
    String master_id; 

    String day; 


    ListView listViewStudentClass; 


    String access_token; 
    String TeacherName, SubjectName, Time; 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.test_layout, container, false); 
     setHasOptionsMenu(true); 

     sunday = (LinearLayout) view.findViewById(R.id.sunday); 
     monday = (LinearLayout) view.findViewById(R.id.monday); 
     tuesday = (LinearLayout) view.findViewById(R.id.tuesday); 
     wednesday = (LinearLayout) view.findViewById(R.id.wednesday); 
     thrusday = (LinearLayout) view.findViewById(R.id.thrusday); 
     friday = (LinearLayout) view.findViewById(R.id.friday); 


     sundaySchedule = (LinearLayout) view.findViewById(R.id.sunday_class); 
     mondayClass = (LinearLayout) view.findViewById(R.id.monday_class); 
     tuesdayClass = (LinearLayout) view.findViewById(R.id.tuesday_class); 
     wednesdayClass = (LinearLayout) view.findViewById(R.id.wednesday_class); 
     thrusdayClass = (LinearLayout) view.findViewById(R.id.thrusday_class); 
     fridayClass = (LinearLayout) view.findViewById(R.id.friday_class); 


     listViewStudentClass = (ListView) view.findViewById(R.id.list_class_schedule); 


     sundaySchedule.setVisibility(View.VISIBLE); 
     mondayClass.setVisibility(View.GONE); 
     tuesdayClass.setVisibility(View.GONE); 
     wednesdayClass.setVisibility(View.GONE); 
     thrusdayClass.setVisibility(View.GONE); 
     fridayClass.setVisibility(View.GONE); 


     sunday.setOnClickListener(this); 
     monday.setOnClickListener(this); 
     tuesday.setOnClickListener(this); 
     wednesday.setOnClickListener(this); 
     thrusday.setOnClickListener(this); 
     friday.setOnClickListener(this); 


     SessionManagement session = new SessionManagement(getContext()); 
     session.checkLogin(); 
     master_id = session.getMasterId(); 
     access_token = session.getAccesstToken(); 


     makeJsonObjectRequest(); 
     return view; 
    } 

    ClassScheduleStudent classScheduleStudent; 


    private void makeJsonObjectRequest() { 

     RequestQueue requestQueue = Volley.newRequestQueue(getContext()); 
     String URL = Navigation_URL + "?Sid=" + master_id; 
     System.out.println(URL); 

     StringRequest stringRequest = new StringRequest(Request.Method.GET, URL, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         try { 


          ArrayList<ClassScheduleStudent> student_list_class_schedule = new ArrayList<>(); 
          JSONArray jArray = new JSONArray(response); 


          // studentFeeInformation = new StudentFeeInformation(response); 
          for (int i = 0; i < jArray.length(); i++) { 


           JSONObject jsonObject = jArray.getJSONObject(i); 
           day = jsonObject.getString("DayOfTheWeek"); 


           if (day.equals("1")) { 
            JSONObject studentDetails_obj = jsonObject.getJSONObject("teachers"); 
            JSONObject studentDetails_course = jsonObject.getJSONObject("courses"); 
            SubjectName = (String) studentDetails_course.get("CourseName"); 
            TeacherName = studentDetails_obj.getString("NAME"); 
            Time = jsonObject.getString("StartTime"); 

            // Log.d("ListData", "@@@ TeacherName:" + TeacherName); 
            student_list_class_schedule.add(new ClassScheduleStudent(SubjectName, TeacherName, Time)); 
           } 

          } 

          // Log.d("ListData", "@@@ student_list_class_schedule size :" + student_list_class_schedule.size()); 
          ClassScheduleStudentAdapter classScheduleStudentAdatpter = new ClassScheduleStudentAdapter(getActivity(), student_list_class_schedule); 
          listViewStudentClass.setAdapter(classScheduleStudentAdatpter); 


         } catch (JSONException e) { 
          Toast.makeText(getContext(), "Fetch failed!", Toast.LENGTH_SHORT).show(); 
          e.printStackTrace(); 
         } 
        } 

       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       Toast.makeText(getContext(), error.toString(), Toast.LENGTH_LONG).show(); 
      } 
     }) { 

      @Override 
      public Map<String, String> getHeaders() throws AuthFailureError { 
       Map<String, String> headers = new HashMap<String, String>(); 
       headers.put("Authorization", "Bearer " + access_token); 
       headers.put("Content-Type", "application/x-www-form-urlencoded"); 
       return headers; 
      } 
/* 
      @Override 
      protected Map<String, String> getParams() throws com.android.volley.AuthFailureError { 
       Map<String, String> map = new HashMap<String, String>(); 

       map.put("id", master_id); 
       map.put("accessID", accessID); 
       map.put("currentUser", master_id); 
       return map; 

      } */ 
     }; 


     requestQueue.add(stringRequest); 

    } 


    @Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
      case R.id.sunday: 
       if (sundaySchedule.getVisibility() == View.GONE) { 
        sundaySchedule.setVisibility(View.VISIBLE); 
       } else if (sundaySchedule.getVisibility() == View.VISIBLE) { 
        sundaySchedule.setVisibility(View.GONE); 
       } 
       break; 
      case R.id.monday: 
       if (mondayClass.getVisibility() == View.GONE) { 
        mondayClass.setVisibility(View.VISIBLE); 
       } else if (mondayClass.getVisibility() == View.VISIBLE) { 
        mondayClass.setVisibility(View.GONE); 
       } 
       break; 
      case R.id.tuesday: 
       if (tuesdayClass.getVisibility() == View.GONE) { 
        tuesdayClass.setVisibility(View.VISIBLE); 
       } else if (tuesdayClass.getVisibility() == View.VISIBLE) { 
        tuesdayClass.setVisibility(View.GONE); 
       } 
       break; 
      case R.id.wednesday: 
       if (wednesdayClass.getVisibility() == View.GONE) { 
        wednesdayClass.setVisibility(View.VISIBLE); 
       } else if (wednesdayClass.getVisibility() == View.VISIBLE) { 
        wednesdayClass.setVisibility(View.GONE); 
       } 
       break; 
      case R.id.thrusday: 
       if (thrusdayClass.getVisibility() == View.GONE) { 
        thrusdayClass.setVisibility(View.VISIBLE); 
       } else if (thrusdayClass.getVisibility() == View.VISIBLE) { 
        thrusdayClass.setVisibility(View.GONE); 
       } 
       break; 
      case R.id.friday: 
       if (fridayClass.getVisibility() == View.GONE) { 
        fridayClass.setVisibility(View.VISIBLE); 
       } else if (fridayClass.getVisibility() == View.VISIBLE) { 
        fridayClass.setVisibility(View.GONE); 
       } 
       break; 
     } 
    } 


    @Override 
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
     // TODO Auto-generated method stub 
     super.onCreateOptionsMenu(menu, inflater); 
     inflater.inflate(R.menu.dashboard, menu); 
    } 

}

ClassScheduleStudentAdapter

public class ClassScheduleStudentAdapter extends BaseAdapter { 


    Context mContext; 

    ArrayList<ClassScheduleStudent> student_class_schedule=null; 
    String TAG = "HomeTab_adapter"; 

    public ClassScheduleStudentAdapter(Context mContext, ArrayList<ClassScheduleStudent> student_class_schedule) { 
     super(); 
     this.mContext = mContext; 
     this.student_class_schedule = student_class_schedule; 
    } 

    @Override 
    public int getCount() { 
     System.out.println(student_class_schedule.size()); 
     return student_class_schedule.size(); 

    } 

    @Override 
    public Object getItem(int position) { 
     return student_class_schedule.get(position); 
     // return arg0; 
    } 

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

    @Override 
    public View getView(final int postion, View convertView, ViewGroup parent) { 
     // final ClassScheduleStudentAdapter.Holder viewHolder; 
     final Holder viewHolder; 

     if (convertView == null) { 
      // inflate the layout 
      LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      // LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 
      convertView = inflater.inflate(R.layout.class_schedule_listitem_student, parent, false); 

      // well set up the ViewHolder 
      // viewHolder = new ClassScheduleStudentAdapter.Holder(); 
      viewHolder=new Holder(); 

      viewHolder.student_profile_class_schedule_Subject = (TextView) convertView.findViewById(R.id.student_profile_subject); 
      viewHolder.student_profile_class_schedule_TeacherName = (TextView) convertView.findViewById(R.id.student_profile_teacherName); 
      viewHolder.student_profile_class_schedule_Time = (TextView) convertView.findViewById(R.id.student_profile_Time); 
      convertView.setTag(viewHolder); 


     } else { 
      // we've just avoided calling findViewById() on resource everytime 
      // just use the viewHolder 
      // viewHolder = (ClassScheduleStudentAdapter.Holder) convertView.getTag(); 
      viewHolder=(Holder)convertView.getTag(); 
     } 
     Log.d(TAG,"@@ postion:"+postion+" getTeacherName"+student_class_schedule.get(postion).getTeacherName()); 
     viewHolder.student_profile_class_schedule_Subject.setText(student_class_schedule.get(postion).getSubject()); 
     viewHolder.student_profile_class_schedule_TeacherName.setText(student_class_schedule.get(postion).getTeacherName()); 
     viewHolder.student_profile_class_schedule_Time.setText(student_class_schedule.get(postion).getTime()); 


     // convertView.setTag(viewHolder); 
     return convertView; 
    } 

    class Holder { 


     TextView student_profile_class_schedule_Subject; 
     TextView student_profile_class_schedule_TeacherName; 
     TextView student_profile_class_schedule_Time; 


    } 




} 

確切的問題是什麼?幾天後我無法解決。

注意更新的解決方案在以前,我有內部滾動型的ListView哪隻顯示一個項目。所以從來沒有 包括內滾動型的ListView,因爲主要的原因,是的ListView沒有autoscrollable滾動視圖 需要

+0

做我必須做的match_parent.match_parent不列入解決問題。在class_schedule_listitem_student父佈局 – Suman

+0

嘗試高度被WRAP_CONTENT而不是match_parent –

+0

對不起@NIkhilGupta我沒有得到你 – Suman

回答

1

的方法設置convertView.setTag(viewHolder);內如果(convertView == null)

檢查日誌以現在的位置值

@Override 
public View getView(final int postion, View convertView, ViewGroup parent) { 
    final Holder viewHolder; 
    if (convertView == null) { 
     // inflate the layout 
     LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     // LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 
     convertView = inflater.inflate(R.layout.class_schedule_listitem_student, parent, false); 

     // well set up the ViewHolder 
     viewHolder = new Holder(); 
     viewHolder.student_profile_class_schedule_Subject = (TextView) convertView.findViewById(R.id.student_profile_subject); 
     viewHolder.student_profile_class_schedule_TeacherName = (TextView) convertView.findViewById(R.id.student_profile_teacherName); 
     viewHolder.student_profile_class_schedule_Time = (TextView) convertView.findViewById(R.id.student_profile_Time); 
     convertView.setTag(viewHolder);//add set tag here 

    } else { 
     // we've just avoided calling findViewById() on resource everytime 
     // just use the viewHolder 
     viewHolder = (Holder) convertView.getTag(); 
    } 

    Log.d(TAG,"@@ postion:"+postion+" getTeacherName"+student_class_schedule.get(postion).getTeacherName()); 

    viewHolder.student_profile_class_schedule_Subject.setText(student_class_schedule.get(postion).getSubject()); 
    viewHolder.student_profile_class_schedule_TeacherName.setText(student_class_schedule.get(postion).getTeacherName()); 
    viewHolder.student_profile_class_schedule_Time.setText(student_class_schedule.get(postion).getTime()); 

    return convertView; 
} 

makeJsonObjectRequest:

private void makeJsonObjectRequest() { 

    RequestQueue requestQueue = Volley.newRequestQueue(getContext()); 
    String URL = Navigation_URL + "?Sid=" + master_id; 
    System.out.println(URL); 

    StringRequest stringRequest = new StringRequest(Request.Method.GET, URL, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        try { 
         ArrayList<ClassScheduleStudent> student_list_class_schedule = new ArrayList<>(); 
         JSONArray jArray = new JSONArray(response); 

         // studentFeeInformation = new StudentFeeInformation(response); 
         for (int i = 0; i < jArray.length(); i++) { 

          JSONObject jsonObject = jArray.getJSONObject(i); 
          day = jsonObject.getString("DayOfTheWeek"); 
          if(day.equals("1")) 
          { 
           JSONObject studentDetails_obj = jsonObject.getJSONObject("teachers"); 
           JSONObject studentDetails_course = jsonObject.getJSONObject("courses"); 
           SubjectName = (String) studentDetails_course.get("CourseName"); 
           TeacherName = studentDetails_obj.getString("NAME"); 
           Time = jsonObject.getString("StartTime"); 

           Log.d("ListData", "@@@ TeacherName:"+TeacherName); 
           student_list_class_schedule.add(new ClassScheduleStudent(SubjectName, TeacherName, Time)); 
          } 


         } 
         Log.d("ListData", "@@@ student_list_class_schedule size :"+student_list_class_schedule.size()); 
         ClassScheduleStudentAdapter classScheduleStudentAdatpter = new ClassScheduleStudentAdapter(getActivity(), student_list_class_schedule); 
         listViewStudentClass.setAdapter(classScheduleStudentAdatpter); 

        } catch (JSONException e) { 
         Toast.makeText(getContext(), "Fetch failed!", Toast.LENGTH_SHORT).show(); 
         e.printStackTrace(); 
        } 
       } 

      }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      Toast.makeText(getContext(), error.toString(), Toast.LENGTH_LONG).show(); 
     } 
    }) { 

     @Override 
     public Map<String, String> getHeaders() throws AuthFailureError { 
      Map<String, String> headers = new HashMap<String, String>(); 
      headers.put("Authorization", "Bearer " + access_token); 
      headers.put("Content-Type", "application/x-www-form-urlencoded"); 
      return headers; 
     } 
     /* 
     @Override 
     protected Map<String, String> getParams() throws com.android.volley.AuthFailureError { 
      Map<String, String> map = new HashMap<String, String>(); 

      map.put("id", master_id); 
      map.put("accessID", accessID); 
      map.put("currentUser", master_id); 
      return map; 

     } */ 
    }; 
    requestQueue.add(stringRequest); 
} 

適配器佈局:

<TextView id="@+id/tv_Days">//For days sunday, monday ... 
<RelativeLayout id="@+id/Layout1">//for lable subject,teacher... 
<RelativeLayout id="@+id/Layout2">//for display all lable values 

模型類:

private day, subject,teacher; 

在getview適配器

private String previous_day=""; 
if(previous_day.equals("") || previous_day!=student_class_schedule.get(postion).getDay()) 
{ 

    tv_Days.setVisibility(View.VISIBLE) 
    Layout1.setVisibility(View.VISIBLE) 
} 
else 
{ 
    tv_Days.setVisibility(View.GONE) 
    Layout1.setVisibility(View.GONE) 
} 

. 
. 
//set other values 
+0

我得到同名重複在Log.Check編輯 – Suman

+0

添加您的logcat – user2025187

+0

edited.please檢查 – Suman

0

調用makeJsonObjectRequest()

ArrayList<ClassScheduleStudent> student_list_class_schedule = new ArrayList<>(); 
    ClassScheduleStudentAdapter customFeeListStudentAdapter = new ClassScheduleStudentAdapter(getActivity(), student_list_student_fee); 
            listViewStudentClass.setAdapter(customFeeListStudentAdapter); 

刪除前移到這一行從響應這一行。 當你獲得完整的數組列表之外的循環,然後調用

classScheduleStudentAdatpter.setList(student_list_class_schedule) 
classScheduleStudentAdatpter.notifyDatasetChanged(); 

內部適配器,

private void setList(ArrayList<ClassScheduleStudent> student_class_schedule){ 
this.student_class_schedule = student_class_schedule; 
} 
+0

我有做了@rohit但問題並沒有解決。編輯 – Suman

+0

你可以在getCount方法中打印adapter中的ArrayList的大小嗎? – Rohit

+0

數組的大小增加了。但它不顯示在VIew – Suman

0

可能的問題是,因爲你已經實現了錯誤觀點持有者模式

你如何實現

if(convertview == null){ 
.... 
.... 
}else{ 
viewHolder = (ViewHolderItem) convertView.getTag(); 
} 
convertView.setTag(viewHolder); 

,但它應該是這樣

if(convertview == null){ 
.... 
.... 
convertView.setTag(viewHolder); 
}else{ 
viewHolder = (ViewHolderItem) convertView.getTag(); 
} 

所以現在更換getView適配器

@Override 
public View getView(final int postion, View convertView, ViewGroup parent) { 
    final ClassScheduleStudentAdapter.Holder viewHolder; 
    if (convertView == null) { 
     // inflate the layout 
     LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     // LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 
     convertView = inflater.inflate(R.layout.class_schedule_listitem_student, parent, false); 

     // well set up the ViewHolder 
     viewHolder = new ClassScheduleStudentAdapter.Holder(); 
     viewHolder.student_profile_class_schedule_Subject = (TextView) convertView.findViewById(R.id.student_profile_subject); 
     viewHolder.student_profile_class_schedule_TeacherName = (TextView) convertView.findViewById(R.id.student_profile_teacherName); 
     viewHolder.student_profile_class_schedule_Time = (TextView) convertView.findViewById(R.id.student_profile_Time); 

     convertView.setTag(viewHolder); 

    } else { 
     // we've just avoided calling findViewById() on resource everytime 
     // just use the viewHolder 
     viewHolder = (Holder) convertView.getTag(); 
    } 

    viewHolder.student_profile_class_schedule_Subject.setText(student_class_schedule.get(postion).getSubject()); 
    viewHolder.student_profile_class_schedule_TeacherName.setText(student_class_schedule.get(postion).getTeacherName()); 
    viewHolder.student_profile_class_schedule_Time.setText(student_class_schedule.get(postion).getTime()); 


    return convertView; 
} 
+0

您是否檢查過我的解決方案? –

+0

我已經這樣做了。和以前一樣 – Suman

+0

但是在你的代碼中,它顯示錯誤! –