populating listview using arrayadapter

∥☆過路亽.° 提交于 2019-12-11 23:47:54

问题


I want to display two strings one below other as if it is a list. I tried using simple_list_item_1 to populate listview but nothing is visible on the screen.

((ListView)view).setAdapter(new ArrayAdapter<String> 
(RealScreen.getAndroidBaseContext(),android.R.layout.simple_list_item_1, label));

where label is an array which contains two items to be displayed. All I can see is a white screen. Also I want this list to be unclickable.

I want the output to be like this:

String 1
String 2

I am not working with the Activity, instead with the View. Here is the method which creates view

public void createView(JSONObject definition) {
    // TODO Auto-generated method stub
    super.createView(definition);
     final JSONArray rowArray = definition.optJSONArray(KeyConstants.KEY_ROWS);
     final JSONArray columnArray = definition.optJSONArray(KeyConstants.KEY_COLUMNS);
     if (rowArray != null) {
            label = new String[rowArray.length()];
            for (int i = 0; i < rowArray.length(); i++) {
                final JSONObject candidate = rowArray.optJSONObject(i);
                if(candidate!=null){
                    label[i]=candidate.optString(KeyConstants.KEY_LABEL_TEXT, null);
                }
            }
     }
    ((ListView)view).setAdapter(new ArrayAdapter<String>(RealScreen.getAndroidBaseContext(),android.R.layout.simple_list_item_1, label));
}

Thanks.


回答1:


First create a custom Adapter Class as below

class MyAdapter extends ArrayAdapter {

    private Context context;

    private ArrayList<String> list;

    public MyAdapter(Context context,
            ArrayList<String> list) 
            { 
        super(context, R.layout.layout_list);
        this.context = context;
        this.list = list;
    }




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

        ViewHolder holder = null;

        if (convertView == null) {
            holder = new ViewHolder();

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = inflater.inflate(R.layout.layout_list,
                    null);

            convertView.setTag(holder);
            holder.string1  = (TextView) convertView
                    .findViewById(R.id.stringgID1);



            holder.string2  = (TextView) convertView
                    .findViewById(R.id.stringID2);


        }
                    else
                     {
            holder = (ViewHolder) convertView.getTag();
        }


        holder.string1 .setText(list.get(postion));
        holder.string2 .setText(list.get(position));

        return convertView;
    }

    @Override
    public int getCount() {

        return list.size();
    }

    class ViewHolder {
        TextView string1 = null;
        TextView string2 = null;


    }

}

And then let your activity extend Activity and not ListActivity

listView.setAdapter(new MyAdapter(youtactivityame.this,label);

Doing this the label with any number of list items can be displayed.



来源:https://stackoverflow.com/questions/15061529/populating-listview-using-arrayadapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!