ListView / Adapter未显示最后一项

时间:2017-05-27 10:02:22

标签: java android listview

这是一个小应用程序,它会询问一些问题,然后它会显示答案。当我们完成所有问题的回答后,会出现新的活动并向我们显示除最后一个之外的所有答案。我搜索了这个问题,看到了答案,但没有一个在我的情况下工作。谢谢你的帮助。 这是我的代码:

主要布局的XML代码。

<?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>

另一个XML自定义视图。

<?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>

以下是问题代码:

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";


    ArrayList<Genius_Questions> bank= new ArrayList<Genius_Questions>(){
        {
            add( new Genius_Questions("There are seventy bottles on a table. If one falls down, then how many remain? (Voice)" , "7","6","0","68","6"));
            add(new Genius_Questions("How many numbers from 1 - 100, which have the letter 'A' in their spellings?", "13", "54", "2", "0", "0"));
            add(new Genius_Questions("If you choose an answer to this question at random, what is the chance you will be correct?", "25%", "0%", "50%", "25%", "0%"));
           // add(new Genius_Questions("Rearrange the letters of the name SOCRATES to give two other eight letter words. What will be the first letter of both words?","T","R","C","S",""));
            add(new Genius_Questions("A pet shop has 8 hamsters, 18 rabbits and 7 guinea pigs. How many dogs does it have?","15","10","6","4","4"));
            add(new Genius_Questions("What letter is next in this sequence?\n" +
                "O, T, T, F, F, S, S, E, __ ", "O", "E", "N", "G", "N"));
            add(new Genius_Questions("What letter is next in this sequence?\n" +
                "M, A, M, J, J, A, S, O,__", "P", "N", "D", "F", "N"));
            add(new Genius_Questions("Complete the following sentence: \"Read every third spot young...\"", "Vagrant", "Psycho", "Doppelganger", "Warrior", "Psycho"));
            add(new Genius_Questions("?sdrawkcab nettirw iP ni srebmun ruof tsrif eht era tahW", "414.3", "141.3", "3.141", "411.3", "141.3"));
            add(new Genius_Questions("What are ID ten T mistakes?", "IDIOTS", "IDENTITY", "I didn't get it", "Useless question.", "IDIOTS"));
            add(new Genius_Questions("Clark does 5 sets of pushup, 6 sets of sit-up, 7 sets of squat, and 8 sets of bench dip in a day. So what do you think which organ in his body would be the largest?", "Muscles", "Skeleton", "Skin", "Nerves", "Skin"));
            add(new Genius_Questions("It took 20,000 workers to build the Taj Mahal in 20 years. How many workers would be required to build it in 10 years?", "10,000", "40,000", "30,000", "Not Possible", "Not Possible"));
        }
    };

    @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();
                    }
                }
            }
        });
    }

}

以下是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);
    }
}

ListViewAdapter类的代码:

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;
    }
}

1 个答案:

答案 0 :(得分:1)

首先listview已经过了Recyclerview,这使我们的工作变得更轻松,并且在RecyclerView中进行了更多改进,以使开发人员的工作更轻松。

出现此问题时,ListView不支持wrap_content。因此,请将ListView的高度和宽度更改为match_parent。然后在适配器xml文件中,父布局高度应为wrap_content,但您已将其提及为match_parent,因此也将其更改为wrap_content。希望这可以解决您的问题。如果上述解决方案不起作用,请尝试删除适配器XML布局中的填充和边距(来自result_listadapter.xml文件中的父布局)。

FYI fill_parent已被弃用,我们应该使用match_parent代替。因此,请避免使用fill_parent

希望这会有所帮助:)