0

I am trying to get an integer array into ListView using an adapter but do not seem to know where to begin. I want the user to be able to open the listview, select a number from 1-99 and have the selected number be deducted from the current year and result displayed in a TextView.

Any help in the right direction would be fantastic!

2
  • Hey, you can find some tutorials on youtube. Search for 'The New Boston'. Commented Feb 11, 2015 at 4:34
  • Thanks! I have heard differing opinions on those videos however they definitely are helpful indeed! Commented Feb 20, 2015 at 0:54

1 Answer 1

1
first you have to create xml of listview given below



<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ScrollView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical" >

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

</RelativeLayout>


make one arraylist<String> list=new arraylist<string>;
add value 1-99 using for loop
and then set the array adpter on that listview as like




package com.example.arraydemo;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
    ArrayList<String> list;
    ListView li;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list=new ArrayList<String>();
        li=(ListView)findViewById(R.id.listView1);
        for(int i=1;i<=99;i++){
        list.add(""+i);
        }
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                this, 
                android.R.layout.simple_list_item_1,
                list );

        li.setAdapter(arrayAdapter); 
        li.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position,
                    long id) {
                // TODO Auto-generated method stub

            }
        });
    }


}



and ontemclicklistener you get current date from system deduct position of listview from that date 
Sign up to request clarification or add additional context in comments.

1 Comment

So sorry for the delay on the thanks! But thank you for helping me into the right direction. I am going to go work on this little program I want to build and see if I can get it to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.