java - Adding TextViews dynamically -
i'm working on project asks true-false questions on android. while asking, collects questions, given answers , correct answers results[]
object array. after user finished questions resultsactivity
starts , i'm struggling show results in following format:
line 1: first question
line 2: correct answer, user's answer
line 3: second question
....
i'll attach resultsactivity
, content_results.xml
. me on right path this?
public class resultsactivity extends appcompatactivity { private textview questiontext; private textview resulttext; private textview giventext; private linearlayout rcontent; private linearlayout rsecondarycontent; private void resultquestionfiller(int i){ rcontent = (linearlayout) findviewbyid(r.id.results_content); questiontext = new textview(this); viewgroup.layoutparams fullwidth = new viewgroup.layoutparams(viewgroup.layoutparams.wrap_content, viewgroup.layoutparams.wrap_content); questiontext.settext(results.true_false_results[i].getquestionresourceid()); rcontent.addview(questiontext, fullwidth); } private void resultanswerfiller(int i){ rsecondarycontent = (linearlayout) findviewbyid(r.id.results_content_secondary); resulttext = new textview(this); viewgroup.layoutparams splitwidth = new viewgroup.layoutparams(100, viewgroup.layoutparams.wrap_content); if (results.true_false_results[i].istfgivenanswer() == results.true_false_results[i].istfcorrectanswer()){ resulttext.settext(r.string.right_answer); resulttext.settextcolor(color.parsecolor("#009933")); } else{ resulttext.settext(r.string.false_answer); resulttext.settextcolor(color.red); } giventext = new textview(this); giventext.settext(string.valueof(results.true_false_results[i].istfcorrectanswer())); rsecondarycontent.addview(giventext,splitwidth); rsecondarycontent.addview(resulttext, splitwidth); } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_results); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); (int = 0; < results.true_false_results.length; i++ ){ resultquestionfiller(i); resultanswerfiller(i); } getsupportactionbar().setdisplayhomeasupenabled(true); }
and xml
:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_vertical_margin" android:paddingright="@dimen/activity_vertical_margin" android:paddingtop="@dimen/activity_vertical_margin" android:orientation="vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="hu.david.szablyteszt.resultsactivity" tools:showin="@layout/activity_results"> <scrollview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/scrollview"> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:id="@+id/results_content"> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/results_content_secondary"/> </linearlayout> </scrollview> </linearlayout>
with code, keep getting following results:
line 1: correct answers, users answer in line.
line 2: question 1
line 3: question 2
...
using listview or recyclerview more efficient. here code achieve want using recyclerview:
create object hold question, user answer , correct answer:
public class questionanswerobject { private string question; private string useranswer; private string correctanswer; public questionanswerobject(string question, string useranswer, string correctanswer) { this.question = question; this.useranswer = useranswer; this.correctanswer = correctanswer; } public string getquestion() { return question; } public string getuseranswer() { return useranswer; } public string getcorrectanswer() { return correctanswer; } }
activitymain layout:
<relativelayout 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="${relativepackage}.${activityclass}"> <android.support.v7.widget.recyclerview android:id="@+id/my_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical" /> </relativelayout>
recyclerview item layout:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="?android:attr/selectableitembackground" android:orientation="vertical"> <textview android:id="@+id/question" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingtop="5dp" android:text="large text" android:textappearance="?android:attr/textappearancelarge" /> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <textview android:id="@+id/useranswer" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:paddingbottom="5dp" android:paddingleft="@dimen/activity_horizontal_margin" android:text="small text" android:textappearance="?android:attr/textappearancesmall" /> <textview android:id="@+id/correctanswer" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:paddingbottom="5dp" android:paddingleft="@dimen/activity_horizontal_margin" android:text="small text" android:textappearance="?android:attr/textappearancesmall" /> </linearlayout>
recyclerview adapter:
public class myrecyclerviewadapter extends recyclerview.adapter<myrecyclerviewadapter.objectholder> { private arraylist<questionanswerobject> questionanswerobjects; public static class objectholder extends recyclerview.viewholder { textview question; textview useranswer; textview correctanswer; public objectholder(view itemview) { super(itemview); question = (textview) itemview.findviewbyid(r.id.question); useranswer = (textview) itemview.findviewbyid(r.id.useranswer); correctanswer = (textview) itemview.findviewbyid(r.id.correctanswer); } } public myrecyclerviewadapter(arraylist<questionanswerobject> data) { questionanswerobjects = data; } @override public objectholder oncreateviewholder(viewgroup parent, int viewtype) { view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.recyclerview_item, parent, false); objectholder dataobjectholder = new objectholder(view); return dataobjectholder; } @override public void onbindviewholder(objectholder holder, int position) { holder.question.settext(questionanswerobjects.get(position).getquestion()); holder.useranswer.settext(questionanswerobjects.get(position).getuseranswer()); holder.correctanswer.settext(questionanswerobjects.get(position).getcorrectanswer()); } @override public int getitemcount() { return questionanswerobjects.size(); } }
mainactivity
public class mainactivity extends activity { private recyclerview mrecyclerview; private recyclerview.adapter madapter; private recyclerview.layoutmanager mlayoutmanager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mrecyclerview = (recyclerview) findviewbyid(r.id.my_recycler_view); mrecyclerview.sethasfixedsize(true); mlayoutmanager = new linearlayoutmanager(this); mrecyclerview.setlayoutmanager(mlayoutmanager); arraylist<questionanswerobject> data = new arraylist<>(); data.add(new questionanswerobject("question1", "useranswer1", "correctanswer1")); data.add(new questionanswerobject("question2", "useranswer2", "correctanswer2")); data.add(new questionanswerobject("question3", "useranswer3", "correctanswer3")); madapter = new myrecyclerviewadapter(data); mrecyclerview.setadapter(madapter); recyclerview.itemdecoration itemdecoration = new divideritemdecoration(this, linearlayoutmanager.vertical); mrecyclerview.additemdecoration(itemdecoration); } }
Comments
Post a Comment