2 Replies - 287 Views - Last Post: 23 April 2014 - 12:16 PM

#1 Slinky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-April 14

Checkbox list showing indexes instead of text

Posted 22 April 2014 - 01:18 PM

Hi

I have managed to come up with a solution to my previous problem of the checkboxes not checking. The only problem I have left, is:

I have 2 screens, one screen displays a list of groceries with checkboxes and the second screen displays the checked items in a list. The items on the first screen show and the checkboxes function perfectly, but when I click the button to display the items in a list on the second screen, it shows the index number of the list instead of the string item name.

I would appreciate some assistance on this problem and like to know where am I going wrong.

Below is my Java coding for both screens and he xml coding for both screens.

Java Code - Screen 1

package com.allmycode.lists;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
  static CheckBox[] checkBoxes = new CheckBox[5];
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    LinearLayout layout = 
        (LinearLayout) findViewById(R.id.linearLayout);
    for (int i = 0; i < 5; i++) {
      checkBoxes[i] = new CheckBox(this);
      layout.addView(checkBoxes[i]);
    }
    checkBoxes[0].setText("Coke");
    checkBoxes[1].setText("Ice Cream");
    checkBoxes[2].setText("Fruit");
    checkBoxes[3].setText("Biscuits");
    checkBoxes[4].setText("Yoghurt");
  }
  
  public void onShowListClick(View view) {
    Intent intent =
        new Intent(this, MyListActivity.class);
    startActivity(intent);
  }
}



XML Code - Screen 1

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=

      "http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </TextView>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onclick="onShowListClick"
        android:text="@string/show_list" >
    </Button>
</LinearLayout>




Java Code - Screen 2

package com.allmycode.lists;

import java.util.ArrayList;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class MyListActivity extends ListActivity {

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    ArrayList<Integer> listItems =
        new ArrayList<Integer>();
    
    
    for (int i = 0; i < 5; i++) {
      if (MainActivity.checkBoxes[i].isChecked()) {
        listItems.add(i);
      }
    }

    setListAdapter(new ArrayAdapter<Integer>(this,
        R.layout.my_list_layout, listItems));
  }
}



XML Code - Screen 2

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android=
      "http://schemas.android.com/apk/res/android"
    android:id="@+id/identView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</TextView>



This post has been edited by Atli: 22 April 2014 - 01:36 PM
Reason for edit:: Title updated. Please use descriptive titles for your threads.


Is This A Good Question/Topic? 0
  • +

Replies To: Checkbox list showing indexes instead of text

#2 Slinky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-April 14

Re: Checkbox list showing indexes instead of text

Posted 23 April 2014 - 09:21 AM

Gee... Is there anyone here that can help???
Was This Post Helpful? 0
  • +
  • -

#3 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 866
  • View blog
  • Posts: 2,657
  • Joined: 29-July 11

Re: Checkbox list showing indexes instead of text

Posted 23 April 2014 - 12:16 PM

You are populating our listview with an arrayadapter that is populated by a lit of integers. It's not surprising that you are seeing a list of ints... :lol:

Make listItems an ArrayList of String and populate it with the proper names.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1