RaagaSudha
RaagaSudha

Reputation: 397

How to create a listview for displaying the data in android?

In I am working on quiz application. In my project after the test is over I need to keep review button. When review button is clicked all the questions which appeared for test has to be displayed again with correct answer one color ,wrong answer one color and timeout answer other color. So I kept all the questions,answers and selected answer in an arraylist. Now in review page I need to display the question,options and explanation in a listview. Hence for that I have created a listview. But it showing null pointer exception.

Mycode:

public class testreview extends Activity {

private ListView listViewScore = null;
private ListViewAdapter listViewAdapter = null;
public static ArrayList<ArrayList<String>> ourstringList1 = Select.stringList1;
    private ArrayList<ArrayList<String>> usernameArrLst = ourstringList1;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);
    listViewScore=(ListView)findViewById(R.id.list);
    usernameArrLst = new ArrayList<ArrayList<String>>();
    listViewAdapter = new ListViewAdapter();
    listViewScore.setAdapter(listViewAdapter);
}

class ListViewAdapter extends BaseAdapter{

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if(usernameArrLst==null){
            return 0;
        }

        return usernameArrLst.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return usernameArrLst.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        // TODO Auto-generated method stub
        View rowView=view;
        if(rowView==null){
            LayoutInflater layoutinflate =LayoutInflater.from(testreview.this);
            rowView=layoutinflate.inflate(R.layout.listrow, parent, false);
        }

        TextView textViewName=(TextView)rowView.findViewById(R.id.tv_case);
        textViewName.setText((CharSequence) usernameArrLst.get(position));

        return rowView;
    }

}

}

Upvotes: 0

Views: 137

Answers (1)

The iCoder
The iCoder

Reputation: 1444

Check the array size before giving to the list adapter by simply .size() method and try to print it on log. And I think not sure, Select is your another class having values stored in StringList1 ArrayList() then try print that also.

Upvotes: 1

Related Questions