如何将两个textViews添加到自定义适配器?

时间:2014-01-23 06:13:11

标签: android android-listview android-arrayadapter

我试图添加两个textViews来列出item.that数据来自列表。当列表打印时,它显示正确。但在列表视图中,它打印不正确。任何人都可以帮助我吗?

这是自定义适配器类。

@Override
    public View getView(int arg0, View convertView, ViewGroup arg2) {
        // TODO Auto-generated method stub

        final String text1 = listData.get(0);
        final String text2 = listData.get(1);


        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.ratings_list, null);
        }

        TextView lblListHeader1 = (TextView) convertView.findViewById(R.id.textView1);
        lblListHeader1.setText(text1);

        TextView lblListHeader2 = (TextView) convertView.findViewById(R.id.textView2);
        lblListHeader2.setText(text2);

        return convertView;
    }

这是活动代码。

public void ListDrwaer() {

        try {
            JSONObject jsonResponse = new JSONObject(strJson1);
            JSONArray jsonMainNode = jsonResponse.optJSONArray("ratings");

            for (int i = 0; i < jsonMainNode.length(); i++) {
                JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                String restName = jsonChildNode.optString("rest_name");
                listData = new ArrayList<String>();
                if (restName.equalsIgnoreCase(name)) {
                    String userName = jsonChildNode.optString("user_name");
                    String rate = jsonChildNode.optString("rate");
                    String ratOut = "Rate :  " + rate;

                    listData.add(userName);
                    listData.add(ratOut);
                    Log.d("Data", userName + rate);

                }
            }

        } catch (JSONException e) {
            Toast.makeText(getApplicationContext(), "Error..." + e.toString(),
                    Toast.LENGTH_LONG).show();
        }

        RatingsAdapter adapter = new RatingsAdapter(getApplicationContext(),
                listData);
        listView.setAdapter(adapter);
    }

我想添加用户名,低于它的费率。

这应该是输出列表。

01-23 11:59:09.102: D/Data(4873): omali  3.5
01-23 11:59:09.102: D/Data(4873): sunil  2
01-23 11:59:09.102: D/Data(4873): kuma@fh.com  1.5
01-23 11:59:09.102: D/Data(4873): fhhhy@ghj.com  0.5

4 个答案:

答案 0 :(得分:2)

首先,你构建列表的方式,它永远不会工作,因为你在每次迭代中删除它并创建一个新的,所以当你创建适配器时,在列表中你只有最后一项。

我愿意:

ArrayList<Pair<String,String>> listData = new ArrayList<Pair<String,String>>(); //added


public void ListDrwaer() {

        try {
            JSONObject jsonResponse = new JSONObject(strJson1);
            JSONArray jsonMainNode = jsonResponse.optJSONArray("ratings");

            for (int i = 0; i < jsonMainNode.length(); i++) {
                JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                String restName = jsonChildNode.optString("rest_name");
                //removed listData = new ArrayList<String>();
                if (restName.equalsIgnoreCase(name)) {
                    String userName = jsonChildNode.optString("user_name");
                    String rate = jsonChildNode.optString("rate");
                    String ratOut = "Rate :  " + rate;


                    listData.add(new Pair<String,String>(userName,ratOut ));//added
                    //removed listData.add(userName);
                    //removed listData.add(ratOut);
                    Log.d("Data", userName + rate);

                }
            }

        } catch (JSONException e) {
            Toast.makeText(getApplicationContext(), "Error..." + e.toString(),
                    Toast.LENGTH_LONG).show();
        }

        RatingsAdapter adapter = new RatingsAdapter(getApplicationContext(),
                listData);
        listView.setAdapter(adapter);
    } 

然后,在自定义适配器类中,只需通过

检索该数据
@Override
    public View getView(int arg0, View convertView, ViewGroup arg2) {
        // TODO Auto-generated method stub

             //only this 3 lines change
             Pair<String,String> item= listData.get(arg0);
        final String text1 = item.first;
        final String text2 = item.second;


        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.ratings_list, null);
        }

        TextView lblListHeader1 = (TextView) convertView.findViewById(R.id.textView1);
        lblListHeader1.setText(text1);

        TextView lblListHeader2 = (TextView) convertView.findViewById(R.id.textView2);
        lblListHeader2.setText(text2);

        return convertView;
    }

答案 1 :(得分:0)

无论项目的位置如何,您始终打印错误的数据

final String text1 = listData.get(0);
final String text2 = listData.get(1);

所以最好选择用户名 ratOut 的不同列表并显示数据

final String text1 = userNameListData.get(arg0);
final String text2 = ratOutListData.get(arg0);

此处 arg0 是职位

答案 2 :(得分:0)

更改为:

   final String text1 = listData.get(arg0*2);
    final String text2 = listData.get(arg0*2+1);

并覆盖:

@Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listData.size()/2;
    }

当您在一个arraylist中添加文本时,0,1属于第一项,2,3属于第二项,依此类推。

列表视图项的大小也是arraylist大小的一半。

试试这个。

也改变了这个:

   listData = new ArrayList<String>();//<--out side for loop

 for (int i = 0; i < jsonMainNode.length(); i++) {
                JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                String restName = jsonChildNode.optString("rest_name");
                if (restName.equalsIgnoreCase(name)) {
                    String userName = jsonChildNode.optString("user_name");
                    String rate = jsonChildNode.optString("rate");
                    String ratOut = "Rate :  " + rate;

                    listData.add(userName);
                    listData.add(ratOut);
                    Log.d("Data", userName + rate);

                }
            }

答案 3 :(得分:0)

替换它:

final String text1 = listData.get(0);
final String text2 = listData.get(1);

使用:

final String text1 = listData.get(2*arg0);
final String text2 = listData.get((2*arg0)+1);
相关问题