我正在使用ListView来填充JSON数据。我想显示所有相应的值,但ListView中只设置了一个JSON数据。为什么所有的值都没有设置为ListItem。
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;
}
}
确切的问题是什么?我几天都无法解决。
注意更新的解决方案以前我在ScrollVIew中有ListView这是仅显示一个Item的主要原因。所以从不 包括listView里面的listView,listView是可自动滚动的 需要scrollView
答案 0 :(得分:1)
convertView.setTag(viewHolder);
,请在{p>内设置(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
答案 1 :(得分:0)
在致电makeJsonObjectRequest()
ArrayList<ClassScheduleStudent> student_list_class_schedule = new ArrayList<>();
ClassScheduleStudentAdapter customFeeListStudentAdapter = new ClassScheduleStudentAdapter(getActivity(), student_list_student_fee);
listViewStudentClass.setAdapter(customFeeListStudentAdapter);
从响应中移除ArrayList<ClassScheduleStudent> student_list_class_schedule = new ArrayList<>();
这一行。
当你在for循环中获得完整的数组列表时,然后调用
classScheduleStudentAdatpter.setList(student_list_class_schedule)
classScheduleStudentAdatpter.notifyDatasetChanged();
内部适配器,
private void setList( ArrayList<ClassScheduleStudent> student_class_schedule){
this.student_class_schedule = student_class_schedule;
}
答案 2 :(得分: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;
}