20170117 ListView 功能練習
//ListView功能練習_簡單台南導覽
//20170104劉彥廷
MainActivity.java
package com.example.user.myapplication21;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
ListView listView;
TextView textView3;
//String 是屬性 要放外面
//資料
String[] spots = new String[]{"安平老街", "億載金城", "奇美博故館", "赤崁樓",
"關子嶺", "天后宮", "七股鹽山", "安平港", "延平郡王祠", "五妃廟",
"正統鹿耳門聖母廟", "大天后宮", "安平小吃街"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//GUI 元件
listView = (ListView) findViewById(R.id.listView);
textView3 = (TextView) findViewById(R.id.textView3);
//adapter
ArrayAdapter adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, spots);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
textView3.setText(spots[position]);
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="com.example.user.myapplication21.MainActivity"
android:orientation="vertical">
<TextView
android:text="@string/TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:textSize="24sp"
android:textColor="#000000" />
<ListView
android:layout_width="match_parent"
android:layout_height="406dp"
android:id="@+id/listView" />
<TextView
android:text="@string/TextView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView3"
android:textColor="#000000"
android:textSize="24sp" />
</LinearLayout>
//20170104劉彥廷
MainActivity.java
package com.example.user.myapplication21;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
ListView listView;
TextView textView3;
//String 是屬性 要放外面
//資料
String[] spots = new String[]{"安平老街", "億載金城", "奇美博故館", "赤崁樓",
"關子嶺", "天后宮", "七股鹽山", "安平港", "延平郡王祠", "五妃廟",
"正統鹿耳門聖母廟", "大天后宮", "安平小吃街"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//GUI 元件
listView = (ListView) findViewById(R.id.listView);
textView3 = (TextView) findViewById(R.id.textView3);
//adapter
ArrayAdapter adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, spots);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
textView3.setText(spots[position]);
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="com.example.user.myapplication21.MainActivity"
android:orientation="vertical">
<TextView
android:text="@string/TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:textSize="24sp"
android:textColor="#000000" />
<ListView
android:layout_width="match_parent"
android:layout_height="406dp"
android:id="@+id/listView" />
<TextView
android:text="@string/TextView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView3"
android:textColor="#000000"
android:textSize="24sp" />
</LinearLayout>
string.xml
<resources>
<string name="app_name">台南導覽(簡單版)</string>
<string name="TextView">我最喜愛的旅遊景點</string>
<string name="TextView1">我最喜歡的都市</string>
<string name="TextView2">顯示結果</string>
<string name="TextView3">顯示結果</string>
</resources>
留言
張貼留言