Gaurav
Gaurav

Reputation: 333

ListView/Adapter is not displaying the last item

this is a small application which asks some questions and then it will show answers. When we finish answering all the questions, new activity appears and shows us the all the answers except the last one. I searched this problem and saw answers but none of them is working in my case. Thanks for any kind of help. This is my code:

XML code for the main layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.itrio.iqtest.Result_QA"
    android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Answers"
            android:textSize="22sp"
            android:textStyle="bold"
            android:layout_margin="20dp"/>

        <ListView
            android:id="@+id/result_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </ListView>
</LinearLayout>

Another XML custom view.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:padding="10dp">

    <TextView
        android:id="@+id/question_result"
        android:layout_height="wrap_content"
       android:layout_width="fill_parent"
        android:textSize="18sp"
        android:textStyle="bold">

    </TextView>

    <View
        android:layout_width="match_parent"
        android:layout_height="10dp"/>

    <TextView
        android:textStyle="italic"
        android:id="@+id/answer_result"
        android:layout_width="fill_parent"
        android:textSize="18sp"
        android:layout_marginBottom="10dp"
        android:layout_height="wrap_content">
    </TextView>

</LinearLayout>

Here's the code for questions:

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Collections;

public class Genius extends AppCompatActivity {
    private TextView question_view;
    private Button mNext;

    int currentQ = 0;
    RadioGroup genius_rg;
    RadioButton op1, op2, op3, op4,checked;
    private int score;
    int turns=0;
    boolean check= false, shuffle = true;
    String[] questions = new String[8];
    String[] answers = new String[8];



    //Keys
    private static final String Question_Index_key = "Index value of Questions";
    private static final String Checked_Radio = "Selected radio Button";
    private static final String Is_Radio_Checked = "ISC";
    private static final String Do_Not_Shuffle_Again = "Dont' shuffle";
    public static final String Result_Questions = "questions to display at result activity";
    public static final String Result_Answers = "answers to display at result activity";




    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(Question_Index_key,currentQ);
        outState.putInt(Checked_Radio,genius_rg.getCheckedRadioButtonId());
        outState.putBoolean(Is_Radio_Checked,check);
        outState.putBoolean(Do_Not_Shuffle_Again, shuffle);
    }

    @Override
       public void onBackPressed() {
        super.onBackPressed();
        Dialog alert_exit = new QuizExitAlert(Genius.this);
        alert_exit.show();

    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    //Changes 'back' button action
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        Dialog alert_exit = new QuizExitAlert(Genius.this);
        alert_exit.show();

    }
    return true;
}
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_genius);

        if (savedInstanceState != null) {
            currentQ = savedInstanceState.getInt(Question_Index_key);
            if (savedInstanceState.getBoolean(Is_Radio_Checked)) {
                checked = (RadioButton)     findViewById(savedInstanceState.getInt(Checked_Radio));
                checked.setChecked(true);
            }
            shuffle = savedInstanceState.getBoolean(Do_Not_Shuffle_Again);
        }

        question_view = (TextView) findViewById(R.id.question_genius);

        genius_rg = (RadioGroup) findViewById(R.id.radio_genius);

        op1 = (RadioButton) findViewById(R.id.g_op1);
        op2 = (RadioButton) findViewById(R.id.g_op2);
        op3 = (RadioButton) findViewById(R.id.g_op3);
        op4 = (RadioButton) findViewById(R.id.g_op4);

        Collections.shuffle(bank);

        question_view.setText(bank.get(currentQ).getQ());
        op1.setText(bank.get(currentQ).getOp1());
        op2.setText(bank.get(currentQ).getOp2());
        op3.setText(bank.get(currentQ).getOp3());
        op4.setText(bank.get(currentQ).getOp4());


        mNext = (Button) findViewById(R.id.next_genius);
        mNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                turns++;

                if (turns >= 8) {
                    new AlertDialog.Builder(Genius.this)
                            .setTitle("Done!!!")
                            .setMessage("You have answered all the questions.")
                            .setPositiveButton("Submit", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Intent result = new Intent(Genius.this,Result_QA.class);
                                    result.putExtra(Result_Questions, questions);
                                    result.putExtra(Result_Answers, answers);
                                startActivity(result);
                                }    
                            }).show();

                }else {
                    if (op1.isChecked() || op2.isChecked() || op3.isChecked() || op4.isChecked()) {
                        checked = (RadioButton) findViewById(genius_rg.getCheckedRadioButtonId());
                        if (bank.get(currentQ).getAns() == checked.getText()) {
                            score += 10;
                       }
                        questions[currentQ] = bank.get(currentQ).getQ();
                        answers[currentQ] = bank.get(currentQ).getAns();

                        genius_rg.clearCheck();
                        currentQ++;

                        question_view.setText(bank.get(currentQ).getQ());

                        op1.setText(bank.get(currentQ).getOp1());
                        op2.setText(bank.get(currentQ).getOp2());
                        op3.setText(bank.get(currentQ).getOp3());
                        op4.setText(bank.get(currentQ).getOp4());

                    } else {
                        Toast.makeText(Genius.this, "Select an option to proceed", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

}

Here's the code for LisView Activity:

import android.os.Bundle;
import android.app.Activity;
import android.widget.ListView;

public class Result_QA extends Activity {


    ListView result;
    ReslutListViewAdapter reslutListViewAdapter;
    String[] ques, ans;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result__q);

        Bundle b= getIntent().getExtras();
        ques = b.getStringArray(new Genius().Result_Questions);
        ans = b.getStringArray(new Genius().Result_Answers);

        result = (ListView) findViewById(R.id.result_view);
        reslutListViewAdapter = new ReslutListViewAdapter(this, ques, ans);

        System.out.println("adapter => "+reslutListViewAdapter.getCount());
        result.setAdapter(reslutListViewAdapter);
    }
}

Code for ListViewAdapter class:

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ReslutListViewAdapter extends BaseAdapter {

    Activity context;
    String[] questions;
    String[] answers;

    public ReslutListViewAdapter(Activity con, String[] ques, String[] ans){
        super();
        context = con;
        questions = ques;
        answers = ans;
    }

    @Override
    public int getCount() {
        return questions.length;
    }
    private class ViewHolder {
        TextView mQ;
        TextView mA;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        LayoutInflater inflater =  context.getLayoutInflater();

        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.result_listadapter, null);
            holder = new ViewHolder();
            holder.mQ = (TextView) convertView.findViewById(R.id.question_result);
            holder.mA = (TextView) convertView.findViewById(R.id.answer_result);
        convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.mQ.setText(questions[position]);
        holder.mA.setText(answers[position]);

        return convertView;
    }
}

Upvotes: 0

Views: 104

Answers (1)

Jeevanandhan
Jeevanandhan

Reputation: 1073

First of all listview is old one Recyclerview had came, which makes our work easier and lot more improvements have been done in RecyclerView to make developer work easier.

Coming to this issue, ListView doesn't support wrap_content. So change the height and width of ListView to match_parent. Then in adapter xml file the parent layout height should be wrap_content, but you have mentioned it as match_parent, so change that also to wrap_content. Hopefully, this should solve your problem. If the above solution didn't work, then try by removing the padding and margin in the adapter XML layout(from the parent layout in result_listadapter.xml file).

FYI fill_parent is deprecated and we are supposed to use match_parent instead of that. So avoid using the fill_parent.

Hope this helps:)

Upvotes: 1

Related Questions