更改Listview适配器行基于NickName android

时间:2013-12-31 07:02:01

标签: android android-listview

在我的listViewAdapter中,如果昵称是dexter,则列表视图行vi = inflater.inflate(R.layout.sujet_ro,null);如果nickName不等于dexter,则会在屏幕上显示另一行。这在我的代码中显示。当我运行代码时,如果名称不是dexter,它将在if条件中打开,永远不会转到else。我做了什么或错过了什么请帮助。

 @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View vi = convertView;

            player = data.get(position).getPlayer();

            try {

                jsonObject = new JSONObject(player);

                if (jsonObject.has("@nickName"))
                    nickName = jsonObject.getString("@nickName");

                //System.out.println("nickName: "+ nickName);



            }catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        if (convertView == null) {

                if (nickName.equalsIgnoreCase("dexter")) {
                    vi = inflater.inflate(R.layout.sujet_ro, null);

                    System.out.println("nickName: "+ "dexter");

                } else {

                    vi = inflater.inflate(R.layout.row_droite, null);
                    System.out.println("nickName: "+ "mckay");
                }


    holder = new ViewHolder();

                holder.txMessage = (TextView) vi.findViewById(R.id.txMessage);
                holder.txLeft= (TextView) vi.findViewById(R.id.txLeft);
                holder.txRight=  (TextView) vi.findViewById(R.id.txRight);


                vi.setTag(holder);


    }else{

                holder = (ViewHolder) vi.getTag();
            }

1 个答案:

答案 0 :(得分:1)

convertView第一次调用null会返回getView(),否则会返回已经夸大的布局视图但是您需要更改布局取决于某些数据,因此您需要对布局进行充气每次像我提到的那样。我不知道如何使用viewholder来满足此类要求。

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = null;
        player = data.get(position).getPlayer();
        try {
            jsonObject = new JSONObject(player);
            if (jsonObject.has("@nickName"))
                nickName = jsonObject.getString("@nickName");
                //System.out.println("nickName: "+ nickName);
        }catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }                
        if (nickName.equalsIgnoreCase("dexter")) {
            vi = inflater.inflate(R.layout.sujet_ro, null);
            System.out.println("nickName: "+ "dexter");
        } else {
            vi = inflater.inflate(R.layout.row_droite, null);
            System.out.println("nickName: "+ "mckay");
        }
        // Note: Before accessing your textviews try to check whether both layouts has same textview with below id
        //(TextView) vi.findViewById(R.id.txMessage);
        //(TextView) vi.findViewById(R.id.txLeft);
        //(TextView) vi.findViewById(R.id.txRight);