ListView
package com.test.android;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class HelloAndroid extends Activity {
private static final String[] m_arr = {"第一组","第二组","第三组"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv = (ListView)findViewById(R.id.ListView01);
ArrayAdapter<String> ada = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, m_arr);
lv.setAdapter(ada);
lv.setOnItemClickListener(new ListView.OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
dispToast("选择的是"+ m_arr[arg2]);
}
});
lv.setOnItemSelectedListener(new ListView.OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3){
dispToast("滚动的是" + m_arr[arg2]);
arg0.setVisibility(View.VISIBLE);
}
public void onNothingSelected(AdapterView<?> arg0){
//
}
});
}
public void dispToast(String str){
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class HelloAndroid extends Activity {
private static final String[] m_arr = {"第一组","第二组","第三组"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv = (ListView)findViewById(R.id.ListView01);
ArrayAdapter<String> ada = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, m_arr);
lv.setAdapter(ada);
lv.setOnItemClickListener(new ListView.OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
dispToast("选择的是"+ m_arr[arg2]);
}
});
lv.setOnItemSelectedListener(new ListView.OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3){
dispToast("滚动的是" + m_arr[arg2]);
arg0.setVisibility(View.VISIBLE);
}
public void onNothingSelected(AdapterView<?> arg0){
//
}
});
}
public void dispToast(String str){
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
}