自定义列表视图不调用Getview()方法。

时间:2013-12-13 07:44:20

标签: android android-custom-view

我不知道为什么,但我的自定义列表视图不起作用。当我调用ListView.setadapter时,我的适配器不会调用getView()方法。我连续两天想出这个问题但是我不知道为什么。没有错误没有工作。帮帮我吧。这很严重

这是我的自定义适配器代码:

 import java.util.ArrayList;
    import android.app.Activity;
    import android.content.Context;
    import android.content.res.Resources;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageButton;
    import android.widget.ImageView;
    import android.widget.TableRow;
    import android.widget.TextView;

public class CustomAdapter extends BaseAdapter{

    private Activity activity;//for parameter
    private ArrayList data;//for parameter
    public Resources res;//for parameter
    private static LayoutInflater inflater=null;
    ListModel getList ;
    int i=0;

     public CustomAdapter(Activity a, ArrayList data,Resources resLocal) {


            activity = a;
            this.data=data;
            res = resLocal;

            /***********  Layout inflator to call external xml layout () **********************/
            inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        }
    @Override
    public int getCount() {
        if (data.size()<=0)
            return 1;
        return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public static class ViewHolder{

        public ImageView image;
        public TextView txt_memberName;
        public TextView txt_memberStatus;
        public ImageButton imgbtn_senka;
        public ImageButton imgbtn_fuenka;
        public TableRow tblrow_status;
        public TableRow tblrow_btn;


    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder = new ViewHolder();;
        View vi=convertView;
        Log.i("tag", "INVIEW");

        holder.txt_memberName=(TextView)vi.findViewById(R.id.txtmemberName);
        holder.txt_memberStatus=(TextView)vi.findViewById(R.id.txtmemberStatus);
        holder.image=(ImageView)vi.findViewById(R.id.imgPlayer);
        holder.imgbtn_fuenka = (ImageButton) vi.findViewById(R.id.btnfusenka);
        holder.imgbtn_senka = (ImageButton) vi.findViewById(R.id.btnsenka);
        holder.tblrow_status = (TableRow)vi.findViewById(R.id.tblrowstatus);
        holder.tblrow_status = (TableRow)vi.findViewById(R.id.tblrowbtn);

        if(convertView==null){

            /********** Inflate tabitem.xml file for each row ( Defined below ) ************/
            vi = inflater.inflate(R.layout.tabitem, null);


            holder.txt_memberName=(TextView)vi.findViewById(R.id.txtmemberName);
            holder.txt_memberStatus=(TextView)vi.findViewById(R.id.txtmemberStatus);
            holder.image=(ImageView)vi.findViewById(R.id.imgPlayer);
            holder.imgbtn_fuenka = (ImageButton) vi.findViewById(R.id.btnfusenka);
            holder.imgbtn_senka = (ImageButton) vi.findViewById(R.id.btnsenka);
            holder.tblrow_status = (TableRow)vi.findViewById(R.id.tblrowstatus);
            holder.tblrow_status = (TableRow)vi.findViewById(R.id.tblrowbtn);

           /************  Set holder with LayoutInflater ************/
            vi.setTag(holder);
        }
        else
            holder=(ViewHolder)vi.getTag();

        if(data.size()<=0)
        {
            Log.i("Tag", "No Data");

        }

        else
        {
            /***** Get each Model object from Arraylist ********/
            getList=null;
            getList = (ListModel) data.get(position);
            if(getList.getMemberStatus()==2){
                Log.i("null", "2");

                //holder.tblrow_btn.setVisibility(View.GONE);
            }
            /************  Set Model values in Holder elements ***********/
             holder.txt_memberName.setText(getList.getMemberName());
             holder.txt_memberStatus.setText(getList.getImage());
             holder.image.setImageResource(res.getIdentifier("com.androidexample.customlistview:drawable/"+getList.getImage(),null,null));
             holder.txt_memberName.setText(getList.getMemberName());
             holder.image.setImageResource(res.getIdentifier("com.androidexample.customlistview:drawable/"+getList.getImage(),null,null));

        }
        return vi;


    }

 }

这是我称之为自定义适配器对象

的地方
 import java.util.ArrayList;
    import org.apache.http.NameValuePair;
    import org.apache.http.message.BasicNameValuePair;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    import android.os.Bundle;
    import android.os.StrictMode;
    import android.app.Activity;
    import android.content.Intent;
    import android.content.res.Resources;
    import android.util.Log;
    import android.view.Menu;
    import android.widget.ListView;
    import android.widget.TextView;

public class MainActivity extends Activity {

    ListView list;
    JSONObject Jobj;
    TextView txt_Date;
    TextView txt_Location;
    String team_ID;
    CustomAdapter custAdpt;
    public  MainActivity main = null;
    ListModel model = new ListModel();
    public  ArrayList<ListModel> ListModelArr = new ArrayList<ListModel>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_list_view_android_example);
        txt_Date = (TextView) findViewById(R.id.txt_date);
        txt_Location = (TextView) findViewById(R.id.txt_location);
        Intent intent = getIntent();
        txt_Date.setText(intent.getStringExtra("startTime"));
        txt_Location.setText(intent.getStringExtra("location"));
        team_ID = intent.getStringExtra("teamID");
        setListData();
        Resources res =getResources();
        list=(ListView)findViewById(R.id.participationList);
        MainActivity main = this;
        custAdpt = new CustomAdapter(main, ListModelArr, res);
        list.setAdapter(custAdpt);



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void setListData(){

        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
        .detectDiskReads().detectDiskWrites().detectNetwork()
        .penaltyLog().build());

        String response = null; // to accept Json String from php
        String memberName = null; // for member name to display in list
        String photo = null ; // for photo path
        int memberStatus = 0; // to check member status
        JSONArray memberData=null; // to retrieve member data from json response


         ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("teamID",team_ID));
        postParameters.add(new BasicNameValuePair("startTime","2013-09-18 10:00:00"));// parameter for php


        try {

            response = CustomHttpClient.executeHttpPost(
                       "http://10.0.2.2/football365/sankashaList.php",
                        postParameters);

            Jobj = new JSONObject(response);
            memberData=Jobj.getJSONArray("memberdata");

            for(int i = 0 ; i < memberData.length(); i++){

                memberName = (memberData.getJSONObject(i).getString("name").toString());
                memberStatus = Integer.parseInt((memberData.getJSONObject(i).getString("memberStatus").toString()));
                photo = (memberData.getJSONObject(i).getString("photo").toString());
                model.setMemberName(memberName);
                model.setMemberStatus(memberStatus);
                model.setImage(photo);
                ListModelArr.add(model);
            }
        }
        catch(JSONException e){

            Log.e("log_tag", "Error parsing data "+e.toString());
        }
        catch (Exception e) {

            Log.e("log_tag", "Error parsing data "+e.toString());
        }


    }

}

这是我定义 Getter &amp; Setter 方法

 public class ListModel {

    private String imgMember;
    private String memberName;
    private int memberStatus;

    ///Setters
    public void setMemberName(String memberName)
    {
        this.memberName = memberName;
    }

    public void setImage(String imgMember)
    {
        this.imgMember = imgMember;
    }


    public void setMemberStatus(int memberStatus)
    {
        this.memberStatus=memberStatus;
    }

    ///Getters

    public String getMemberName()
    {
        return this.memberName;
    }

    public String getImage()
    {
        return this.imgMember;
    }


    public int getMemberStatus()
    {
        return this.memberStatus;
    }
}

2 个答案:

答案 0 :(得分:0)

更改

@Override
public int getCount() {
if (data.size()<=0)
    return 1;
return 0;
}

@Override
public int getCount() {
if (data.size()<=0)
    return 1;
return data.size();
}

答案 1 :(得分:0)

但这是一种方式。我有同样的问题。实际上,问题是您没有通知适配器您已经更改了适配器中的数据。在适配器中试用此代码:

@Override
public int getCount() {
return data.size();
}

在更改适配器中的数据时调用 notifyDataSetChanged();