2015-02-11 28 views
0

我想使用適配器將整數數組轉換爲ListView,但似乎不知道從哪裏開始。 我希望用戶能夠打開列表視圖,從1-99中選擇一個數字,並從當前年份中扣除選定的數字,並將結果顯示在TextView中。Android ListView Integer數組

在正確的方向上的任何幫助將是太棒了!

+0

嘿,你可以在YouTube上找到一些教程。搜索「新波士頓」。 – Prudhvi 2015-02-11 04:34:49

+0

謝謝!我已經聽到了這些視頻的不同意見,但他們絕對有幫助! – tiptop 2015-02-20 00:54:39

回答

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 
+0

非常抱歉,謝謝你的延遲!但是,謝謝你幫助我走向正確的方向。我要去研究我想要構建的這個小程序,看看我能否實現它。 – tiptop 2015-02-20 00:54:09

+0

你的歡迎總是..... – 2015-02-20 04:15:30