ListView的Adapter问题

移动开发 码拜 8年前 (2016-05-16) 1272次浏览
需求很简单,就是通过网络获取数据,然后用ListView显示出来。
本人是自定义的一个Adapter,现在获取数据,创建Adapter都没有问题。
在setAdaper时出了问题,代码是这样的:

		new Thread(){
			@Override
			public void run() {
				super.run();
				String url = Utility.getUrlRoot(getApplicationContext()) + UrlTypeBind.GetAllTeamList;
				TeamInfoReqHttp tifrh = new TeamInfoReqHttp(new TeamInfo() , url);
				teamList = tifrh.GetAllTeamList();
				handler.post(new Runnable() {
					@Override
					public void run() {
						adapter = new TeamListAdapter(TeamListActivity.this,teamList);
						lv_TeamList.setAdapter(adapter);
					}
				});
			}
		}.start();

问题是现在数据无法显示,并且LOG里不停的打印GC回收的信息,貌似是有内存泄露,但是找不到原因。
目前数据只有两条,原因是数据是正常的,网络访问就不贴了。
ListView的Adapter问题
Adapter和对应的layout如下

public class TeamListAdapter extends BaseAdapter {
    private List<TeamInfo> teamList;
    private Context context;
    private LayoutInflater inflater;
    public TeamListAdapter(Context context,List<TeamInfo> teamList){
        this.context = context;
        this.teamList = teamList;
        this.inflater = LayoutInflater.from(context);
    }
    public TeamListAdapter(Context context){
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }
    public void setData(List<TeamInfo> teamList){
        this.teamList = teamList;
    }
    @Override
    public int getCount() {
        return teamList.size();
    }
    @Override
    public Object getItem(int i) {
        return teamList.get(i);
    }
    @Override
    public long getItemId(int i) {
        return i;
    }
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holder holder = null;
        if(view == null){
            view = inflater.inflate(R.layout.team_list_item,null);
            holder = new Holder(view);
            view.setTag(holder);
        }
        else {
            holder = (Holder) view.getTag();
        }
        TeamInfo team = teamList.get(i);
        holder.tv_TeamName.setText(team.getTeamName());
        holder.tv_MemberCount.setText(team.getMemberCount() + "人");
        holder.tv_StartTime.setText(Utility.DayConvertString(team.getStartTime()) +
                " " + new SimpleDateFormat("HH:mm").format(team.getStartTime()));
        return view;
    }
    class Holder{
        TextView tv_TeamName;
        TextView tv_StartTime;
        LinearLayout ll_TeamMember;
        TextView tv_MemberCount;
        public Holder(View view){
            tv_TeamName = (TextView) view.findViewById(R.id.tv_team_name);
            tv_StartTime = (TextView) view.findViewById(R.id.tv_team_start_time);
            tv_MemberCount = (TextView) view.findViewById(R.id.tv_team_member_count);
            ll_TeamMember = (LinearLayout) view.findViewById(R.id.ll_team_member);
        }
    }
}
<?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="160dp"
    android:layout_marginTop="10dp"
    android:background="@null">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alpha="0.15"
        android:background="#d5c3ff"></RelativeLayout>
    <TextView android:id="@+id/tv_team_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="10dp"
        android:textColor="#ffffff" />
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp">
        <ImageView android:id="@+id/iv_time_tag"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/time_tag"/>
        <ImageView android:id="@+id/iv_time_ico"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ico_time"
            android:layout_marginLeft="3dp"
            android:layout_centerVertical="true"/>
        <TextView android:id="@+id/tv_team_start_time"
            android:layout_marginLeft="5dp"
            android:layout_width="wrap_content"
            android:layout_centerVertical="true"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/iv_time_ico"
            android:textSize="14sp"/>
    </RelativeLayout>
    <LinearLayout android:id="@+id/ll_team_member"
        android:layout_below="@+id/tv_team_name"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:gravity="left|center_vertical"
        android:orientation="horizontal">
    </LinearLayout>
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="15dp"
        android:layout_marginBottom="10dp" >
        <ImageView
            android:id="@+id/iv_ico_people"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ico_people"/>
        <TextView android:id="@+id/tv_team_member_count"
            android:layout_toRightOf="@id/iv_ico_people"
            android:layout_marginLeft="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </RelativeLayout>
</RelativeLayout>

有没有人遇到过相似问题,或教教本人怎么查也好

解决方案

30

应该是在主线程更新ui,使用listview来setadapter

10

4点半还在敲代码,好拼啊

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明ListView的Adapter问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)