Android基础综合案例(九)Android UI设计

ListView组件和ArrayAdapter

一、要求

使用ListView组件和ArrayAdapter实现UI界面的简单布局。

二、具体代码的实现

1、avtivity_main.xml



    
    

2、MainActivity.java

package com.example.listview1;

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;
import android.widget.Toast;

public class MainActivity extends Activity {
	ListView listview;
	
	private String date[]={"java","C++","pHp","HTML","JavaWeb","SSH"}; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listview=(ListView) findViewById(R.id.listView1);
        ArrayAdapter adapter=new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1,date);
        listview.setAdapter(adapter); 

    listview.setOnItemClickListener(new OnItemClickListener() { 
        
        @Override 
        public void onItemClick(AdapterView adapterview, View view, int position, 
                long id) { 
        	 Toast.makeText(MainActivity.this,date[position],Toast.LENGTH_SHORT).show(); 
        } 
    }); 
} 


	@Override
    
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

三、实现界面如下:

 Android基础综合案例(九)Android UI设计_第1张图片



 

 

你可能感兴趣的:(Android基础综合案例)