alex
alex

Reputation: 398

pass a text file to an arrayadapter

My question is as follows. I would like to be able to read a text file into an array adapter. I have one TextView and 3 RadioButtons. So far I have managed to pass a string array which can be used to update the TextView. I have an array list as shown below

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

    for(int i=1; i <= 3; i++)
    {
        Reader reader = new ResultsReader("C:/Users/ALEXDEV/workspace/Questions/src/quiz"+i+".txt");
        reader.read();

        String str = ((ResultsReader)reader).getInput();
        String data[] = str.split("<.>");

        info = q new info();
        q.question = data[0];
        q.choice1 = data[2];
        q.choice2 = data[3];
        q.choice3 = data[4];
        list.add(q);
    }

    for(Question qs: list) { 

      System.out.println("Q: "+qs.question); 
        System.out.println("Q: "+qs.choice1); 
        System.out.println("Q: "+qs.choice2); 
        System.out.println("Q: "+qs.choice3);   
    }

-

Q: The question
Q: answer one
Q: answer two
Q: answer three

So what i want todo is pass the arraylist to the arrayadapter so I can use the getView method to return the various results

So far I have tried to pass list which is the arraylist to the MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, list);

public class CustomAdapter extends ArrayAdapter {

private ArrayList<Question> list;
private Context context;

public CustomAdapter(Context context, int textViewResourceId, ArrayList<Question> list) {
    super(context, textViewResourceId,list);
    this.context = context;
    this.list = list;
    // TODO Auto-generated constructor stub

}

public View getView(int position, View convertView, ViewGroup parent)
{
     View view = convertView;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           view = inflater.inflate(R.layout.basic, null);
        }

        Question element = list.get(position);


        if (element!= null) {
            // My layout has only one TextView
            TextView itemView = (TextView) view.findViewById(R.id.questionText);

            if (itemView != null) {
                // do whatever you want with your string and long
                itemView.setText(String.format("%s %d", element.question, element.answer));
            }

         }


        return view;

}

Upvotes: 0

Views: 969

Answers (1)

Barak
Barak

Reputation: 16393

You'll need to create a custom adapter to feed the listview.

You can create a custom ArrayAdapter (example here) or BaseAdapter (example here).

In either case, you will be overriding the getView method of the adapter to process your data into the various widgets in your layout.

EDIT

If the question is actually "how do I call the adapter?" then:

private CustomAdapter my_adapter;

my_adapter = new CustomAdapter(this, R.layout.row, list);
setListAdapter(my_adapter);

And follow one of the links above to create your CustomAdapter.

Upvotes: 0

Related Questions