S
S
shuhermayer2014-03-20 15:25:49
Android
shuhermayer, 2014-03-20 15:25:49

How to attach an ArrayList to a custom ListView?

There is a custom ListIvew. Now it is filled from String[]
There is an XML parser, it parses into ArrayList
How to fasten an ArrayList to a ListView in MYArrayAdapter.class so that the adapter is not called each time for each iteration during parsing?
That is, so that the necessary attributes are assigned to the elements of the ListView by the
parser

ArrayList<String> list = new ArrayList<String>();

        try{
            XmlPullParser parser = getResources().getXml(R.xml.products);

            while(parser.getEventType()!=XmlPullParser.END_DOCUMENT){
                if(parser.getEventType() == XmlPullParser.START_TAG && parser.getName().equals("product")){
                    list.add(parser.getAttributeValue(0) + " (" + parser.getAttributeValue(2) + ")\n" + parser.getAttributeValue(1));
                }
                parser.next();
            }
        }catch(Throwable t){
            Toast.makeText(this, "Error loading XML.", Toast.LENGTH_SHORT).show();
        }

        //ListView listView = (ListView)findViewById(R.id.listView);
        //listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list));

Redefined ArrayAdapter:
MYArrayAdapter
package com.ex.listactivity;

...

public class MyArrayAdapter extends ArrayAdapter<String> {
    private final Activity context;
    private final String] names;

    public MyArrayAdapter(Activity context, String] names) {
        super(context, R.layout.rowlayout, names);
        this.context = context;
        this.names = names;
    }


    static class ViewHolder {
        public ImageView imageView;
        public TextView textView;
        public EditText textEdit;
        public Button btn;
    }

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

        final ViewHolder holder;
     
        View rowView = convertView;
        if (rowView == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.rowlayout, null, true);
            holder = new ViewHolder();
            holder.textView = (TextView) rowView.findViewById(R.id.label);
            holder.imageView = (ImageView) rowView.findViewById(R.id.icon);
            holder.textEdit = (EditText) rowView.findViewById(R.id.editText);
            holder.btn = (Button) rowView.findViewById(R.id.button);
            rowView.setTag(holder);




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

        holder.textView.setText(names[position]);
        // Изменение иконки для Windows и iPhone
        String s = names[position];

        return rowView;
    }

    private class onClickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {

        }
    }


}

call from MainActivity
String] values = new String] {"айтем 1", "айтем 2", "айтем 3", "айтем 4", "айтем 5"};
        setListAdapter(new MyArrayAdapter(this, values));

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JaLoveAst1k, 2014-03-21
@JaLoveAst1k

String[] values ​​= list.toArray(new String[list.size()]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question