我的自定义列表仅显示最后检索到的项目。有人可以帮忙吗?
自定义列表的代码:
public class CustomList_Events extends ArrayAdapter<Event>{
private final Activity context;
private final List<Event> eventsList;
public CustomList_Events(Activity context, List<Event> eventsList) {
super(context, R.layout.list_single);
this.context = context;
this.eventsList = eventsList;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
if(eventsList.size() <= 0)
{
return 0;
}
return eventsList.size();
}
@Override
public Event getItem(int position) {
// TODO Auto-generated method stub
return eventsList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
//set up the inflater...
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
//reference the widgets...
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
TextView txtDate = (TextView) rowView.findViewById(R.id.txt2);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
Log.i("CustomList", "Start customList");
txtTitle.setText(eventsList.get(position).getEvent_title());
txtDate.setText(eventsList.get(position).getStart_date());
new GetImageAsyncTask(imageView).execute(Constants.HOST_NAME + "/"+ Constants.CMS_NAME+ "/" +eventsList.get(position).getSmall_picture_path());
Log.i("Event_CustomList", "Added "+eventsList.get(position).getEvent_title());
Log.i("CustomList", "End customList");
return rowView;
}
}
调用它的片段中的代码:
public static class EventsSectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
SharedPreferences settings = null;
Editor editor;
ListView list;
TextView noEventsTv;
public EventsSectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
recLifeCycle_with_savedInstanceState(savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_events, container, false);
list = (ListView) rootView.findViewById(R.id.list);
noEventsTv = (TextView) rootView.findViewById(R.id.norecordsTV);
new GetEventsAsyncTask((MainActivity) getActivity()).execute(nyp.edu.sg.alumnigo.SharedPreferences.getUserName(getActivity()));
settings = PreferenceManager.getDefaultSharedPreferences(getActivity());
return rootView;
}
public void updateEventList(final List<Event> eventList)
{
Log.i("updateEventList:eventlist.count", String.valueOf(eventList.size()));
if (eventList.size() > 0)
{
Log.i("CustomList", "Start customlist inflation");
CustomList_Events adapter = new CustomList_Events(getActivity(), eventList);noEventsTv.setVisibility(View.GONE);
list.setVisibility(View.VISIBLE);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String eventSel = eventList.get(position).getEvent_id();
event_id = eventSel;
// direct to event details fragment
startEventDetailsFragment();
}
});
adapter.notifyDataSetChanged();
list.invalidateViews();
}
else
{
noEventsTv.setVisibility(View.VISIBLE);
list.setVisibility(View.GONE);
}
}
这是它的logcat:
10-13 10:58:51.566: I/CustomList(23269): Start customlist inflation
10-13 10:58:51.586: I/CustomList(23269): Start customList
10-13 10:58:51.596: I/Event_CustomList(23269): Added Home Coming 2014
10-13 10:58:51.596: I/Event_CustomList(23269): Added Talk on Big Data
10-13 10:58:51.596: I/Event_CustomList(23269): Added Breakfast Get-together
10-13 10:58:51.596: I/Event_CustomList(23269): Added SIT Movie Night
10-13 10:58:51.596: I/Event_CustomList(23269): Added 1-Day Durian Trip
10-13 10:58:51.596: I/Events thumbnail(23269): retrieve thumbnail
10-13 10:58:51.596: I/Event_CustomList(23269): Added S.E.A Aquarium
10-13 10:58:51.596: I/CustomList(23269): End customList
10-13 10:58:51.606: I/CustomList(23269): Start customList
10-13 10:58:51.606: I/Event_CustomList(23269): Added Home Coming 2014
10-13 10:58:51.606: I/Event_CustomList(23269): Added Talk on Big Data
10-13 10:58:51.606: I/Event_CustomList(23269): Added Breakfast Get-together
10-13 10:58:51.606: I/Event_CustomList(23269): Added SIT Movie Night
10-13 10:58:51.606: I/Event_CustomList(23269): Added 1-Day Durian Trip
10-13 10:58:51.606: I/Event_CustomList(23269): Added S.E.A Aquarium
10-13 10:58:51.606: I/CustomList(23269): End customList
10-13 10:58:52.046: I/Events thumbnail(23269): retrieve thumbnail
10-13 10:58:52.226: I/Events thumbnail(23269): retrieve thumbnail
10-13 10:58:52.366: I/Events thumbnail(23269): retrieve thumbnail
10-13 10:58:52.486: I/Events thumbnail(23269): retrieve thumbnail
10-13 10:58:52.666: I/Events thumbnail(23269): retrieve thumbnail
10-13 10:58:52.796: I/Events thumbnail(23269): retrieve thumbnail
10-13 10:58:53.016: I/Events thumbnail(23269): retrieve thumbnail
10-13 10:58:53.036: I/CustomList(23269): Start customList
10-13 10:58:53.036: I/Event_CustomList(23269): Added Home Coming 2014
10-13 10:58:53.036: I/Event_CustomList(23269): Added Talk on Big Data
10-13 10:58:53.036: I/Event_CustomList(23269): Added Breakfast Get-together
10-13 10:58:53.036: I/Event_CustomList(23269): Added SIT Movie Night
10-13 10:58:53.036: I/Event_CustomList(23269): Added 1-Day Durian Trip
10-13 10:58:53.036: I/Event_CustomList(23269): Added S.E.A Aquarium
10-13 10:58:53.036: I/CustomList(23269): End customList
自定义列表的结果
我做错了吗?或者我是否需要在代码中添加更多内容?
编辑:
这是异步任务:
public Boolean postData(String user_id) throws JSONException {
Boolean error = false;
HttpClient httpclient = new DefaultHttpClient();
// specify the URL you want to post to
try {
HttpGet httpget = new HttpGet(Constants.HOST_NAME+"/"+Constants.SERVICE_NAME+"/api/Event?userId=" + user_id);
BufferedReader reader;
StringBuffer sb;
String line = "";
String NL="";
String json;
HttpResponse response = httpclient.execute(httpget);
if(response.getStatusLine().getStatusCode()==200)
{
reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
sb = new StringBuffer("");
line = "";
NL = System.getProperty("line.separator");
while ((line = reader.readLine()) != null)
{
sb.append(line + NL);
}
reader.close();
json = sb.toString();
Log.i("event json",json);
Log.i("Events", "retrieved events");
try
{
JSONArray jsonArray = new JSONArray(json);
for (int i = 0, length = jsonArray.length(); i < length; i++)
{
JSONObject attribute = jsonArray.getJSONObject(i);
Event eventObj = new Event();
eventObj.setEvent_id(attribute.getString("event_id"));
eventObj.setEvent_title(attribute.getString("event_title"));
eventObj.setEvent_desc(attribute.getString("event_desc"));
eventObj.setStart_date(attribute.getString("start_date"));
eventObj.setEnd_date(attribute.getString("end_date"));
eventObj.setStart_time(attribute.getString("start_time"));
eventObj.setEnd_time(attribute.getString("end_time"));
eventObj.setLocation(attribute.getString("location"));
eventObj.setPicture_path(attribute.getString("picture_path"));
eventObj.setSmall_picture_path(attribute.getString("small_picture_path"));
eventList.add(eventObj);
eventObj = null;
}
}
catch (JSONException e)
{
e.printStackTrace();
error = true;
}
}
else
{
error = true;
}
}
catch (ClientProtocolException e)
{
// process execption
error = true;
}
catch (IOException e)
{
// process execption
error = true;
}
return error;
}
这是返回的计数:
10-13 10:58:51.566: I/updateEventList:eventlist.count(23269): 6
更新:
正如我所见,似乎我的所有物品都堆叠在一起,导致它只显示最后一项。
答案 0 :(得分:0)
尝试替换代码的这一部分:
@Override
public View getView(int position, View view, ViewGroup parent) {
//set up the inflater...
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
//reference the widgets...
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
TextView txtDate = (TextView) rowView.findViewById(R.id.txt2);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
Log.i("CustomList", "Start customList");
txtTitle.setText(eventsList.get(position).getEvent_title());
txtDate.setText(eventsList.get(position).getStart_date());
new GetImageAsyncTask(imageView).execute(Constants.HOST_NAME + "/"+ Constants.CMS_NAME+ "/" +eventsList.get(position).getSmall_picture_path());
Log.i("Event_CustomList", "Added "+eventsList.get(position).getEvent_title());
Log.i("CustomList", "End customList");
return rowView;
}
到此:
@Override
public View getView(int position, View rowView, ViewGroup parent) {
if(rowView == null){
LayoutInflater inflater = context.getLayoutInflater();
rowView= inflater.inflate(R.layout.list_single, null, true);
}
//reference the widgets...
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
TextView txtDate = (TextView) rowView.findViewById(R.id.txt2);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
Log.i("CustomList", "Start customList");
txtTitle.setText(eventsList.get(position).getEvent_title());
txtDate.setText(eventsList.get(position).getStart_date());
new GetImageAsyncTask(imageView).execute(Constants.HOST_NAME + "/"+ Constants.CMS_NAME+ "/" +eventsList.get(position).getSmall_picture_path());
Log.i("Event_CustomList", "Added "+eventsList.get(position).getEvent_title());
Log.i("CustomList", "End customList");
return rowView;
}
答案 1 :(得分:0)
在我的自定义列表布局中,我有一个背景。删除后,它工作。