Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 24th, 2012  |  syntax: None  |  size: 5.94 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to display the textviews dynamically in a list view in android?
  2. public class Review extends Activity {
  3.  
  4.     static ArrayList selectedoptionids = Test1.listarray;
  5.     static ArrayList<ArrayList<String>> questionslist = Test2.stringList1;
  6.     static ArrayList<ArrayList<String>> alloptionlist = Test2.optionstablelist;
  7.     static ArrayList<ArrayList<String>> all = new ArrayList<ArrayList<String>>();
  8.  
  9.     ListView list;
  10.     Button next;
  11.     String op1, op2, op3, op4, op5;
  12.     ArrayList<String> arr1;
  13.     ArrayList<String> arr2;
  14.  
  15.     int a, i;
  16.     static int k = 0;
  17.     static int p = 1;
  18.  
  19.     static List<mainlist> entirelist = new ArrayList<mainlist>();
  20.     String  quest;
  21.  
  22.     /** Called when the activity is first created. */
  23.     @Override
  24.     public void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.review);
  27.  
  28.         list = (ListView) findViewById(R.id.listlist);
  29.  
  30.         View header = getLayoutInflater().inflate(
  31.                 R.layout.listview_header_text, null);
  32.         list.addHeaderView(header, null, false);
  33.  
  34.         View footerView = getLayoutInflater().inflate(
  35.                 R.layout.listview_footer_text, null);
  36.         list.addFooterView(footerView, null, false);
  37.  
  38.         next = (Button) findViewById(R.id.next);
  39.  
  40.         eachquestion();
  41.  
  42.         lvAdapter adapter = new lvAdapter(this, entirelist) {
  43.             public boolean areAllItemsEnabled() {
  44.                 return false;
  45.             }
  46.  
  47.             public boolean isEnabled(int position) {
  48.                 return false;
  49.             }
  50.         };
  51.         list.setAdapter(adapter);
  52.     }
  53.  
  54.  
  55.     public void eachquestion() {
  56.         arr1 = new ArrayList<String>();
  57.  
  58.         for (i = p - 1; i < p + 3; i++) {
  59.  
  60.             arr1 = questionslist.get(i);
  61.             arr2 = new ArrayList<String>();
  62.  
  63.             for (int j = 0; j < 5; j++) {
  64.  
  65.                 arr2 = alloptionlist.get(0);
  66.                     if (j == 0)
  67.                         op1 = arr2.get(2);
  68.  
  69.                      else if (j == 1)
  70.                         op2 = arr2.get(2);
  71.  
  72.                      else if (j == 2)
  73.                         op3 = arr2.get(2);
  74.                 }
  75.  
  76.             }
  77.  
  78.     entirelist.add(new mainlist(quest, op1, op2, op3));
  79.  
  80.         }
  81.     }
  82.  
  83. }
  84.        
  85. public class mainlist {
  86.     String question,option1,option2,option3;
  87.  
  88.     public mainlist(String question,String option1,String option2,String option3) {
  89.         super();
  90.         this.question = question;
  91.         this.option1 = option1;
  92.         this.option2= option2;
  93.         this.option3 = option3;
  94.         }
  95.  
  96.     public String getquestion() {
  97.         return question;
  98.     }
  99.     public void setquestion(String question) {
  100.         this.question = question;
  101.     }
  102.  
  103.     public String getoption1() {
  104.         return option1;
  105.     }
  106.     public void setoption1(String option1) {
  107.         this.option1 = option1;
  108.     }
  109.  
  110.     public String getoption2() {
  111.         return option2;
  112.     }
  113.     public void setoption2(String option2) {
  114.         this.option2 = option2;
  115.     }
  116.  
  117.     public String getoption3() {
  118.         return option3;
  119.     }
  120.     public void setoption3(String option3) {
  121.         this.option3 = option3;
  122.     }
  123. }
  124.        
  125. public class lvAdapter extends BaseAdapter implements OnClickListener {
  126.     private Context context;
  127.      List<mainlist> list11 = Review.entirelist;
  128.  
  129.     public lvAdapter(Context context, List<mainlist> list11 ) {
  130.         this.context = context;
  131.         this.list11 = list11 ;
  132.     }
  133.     public int getCount() {
  134.         return list11.size();
  135.     }
  136.     public Object getItem(int position) {
  137.         return list11.get(position);
  138.     }
  139.  
  140.     public long getItemId(int position) {
  141.         return position;
  142.     }
  143.  
  144.  
  145.     public View getView(int position, View convertView, ViewGroup viewGroup) {
  146.  
  147.         mainlist inst  = list11.get(position);
  148.  
  149.         if (convertView == null)
  150.         {
  151.             LayoutInflater inflater = (LayoutInflater) context
  152.                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  153.             convertView = inflater.inflate(R.layout.reviewrow, null);
  154.         }
  155.  
  156.         TextView question = (TextView) convertView.findViewById(R.id.textView2);
  157.         tvPhone.setText(inst.getquestion());
  158.  
  159.         TextView option1 = (TextView) convertView.findViewById(R.id.op1);
  160.         option1.setText(inst.getoption1());
  161.  
  162.         TextView option2 = (TextView) convertView.findViewById(R.id.op2);
  163.         option2.setText(inst.getoption2());
  164.  
  165.         TextView option3 = (TextView) convertView.findViewById(R.id.op3);
  166.         option3.setText(inst.getoption3());
  167.  
  168.         return convertView;
  169.     }
  170.  
  171.     @Override
  172.     public void onClick(View v) {
  173.  
  174.     }
  175. }
  176.        
  177. public class Quiz
  178. {
  179.     private String question;
  180.     private ArrayList<String> options;   // no need to get separate variable for every option
  181.     public Quiz(String q, ArrayList<String> o)
  182.     {
  183.        super();  this.question = q; this.options = o;
  184.     }
  185.     //setter and getter.. like setOptions() getOptions() setQuestion() etc.. :/
  186. }
  187.        
  188. List<Quiz> entirelist = new ArrayList<Quiz>();
  189.        
  190. ArrayList<String> options = new ArrayList<String>();
  191. options.add(op1);
  192. options.add(op2);
  193. options.add(op3);
  194. entirelist.add(new Quiz(quest,options));
  195.        
  196. public View getView(int position, View convertView, ViewGroup viewGroup) {
  197.         if (convertView == null)
  198.         {
  199.             LayoutInflater inflater = (LayoutInflater) context
  200.                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  201.             convertView = inflater.inflate(R.layout.reviewrow, null);
  202.         }
  203.         Quiz currentQuiz = list11.get(position);
  204.         TextView question = (TextView) convertView.findViewById(R.id.textView2);
  205.         tvPhone.setText(currentQuiz.getquestion());
  206.  
  207.         // now add options dynamically..
  208.         ArrayList<String> options = currentQuiz.getOptions();
  209.         for(String option : options)
  210.         {
  211.             TextView optionTextView = new TextView(context);
  212.             optionTextView.setText(option);
  213.             convertView.add(optionTextView);
  214.         }
  215.         return convertView;
  216. }