找传奇、传世资源到传世资源站!

安卓listview_入门级实例_

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

安卓listview_入门级实例_ Android平台开发-第1张

package com.example.myfirstandroidpro;

import java.util.ArrayList;
import java.util.HashMap;

 

import android.os.Bundle;
import android.app.Activity;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
 private ListView MyListview;
 private final static int MENU_ITEM_COUNTER = 0;
	
	 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		MyListview =(ListView)findViewById(R.id.lv);
		 
		
		ArrayList<HashMap<String ,Object>> listitem=new ArrayList<HashMap<String,Object>>(); 
		
		for(int i=0;i<10;i )
		{
			HashMap<String,Object> map=new HashMap<String,Object>();			
 		  map.put("ItemTitle", "项目" i);
 		  map.put("ItemText", ".net小项目" i);
 		  listitem.add(map);		
		}
		SimpleAdapter listitemAdapter=new SimpleAdapter(this,listitem,R.layout.listview,
				new String[]{"ItemTitle","ItemText"},
				new int[]{R.id.topTextView,R.id.bottomTextView}												
				);
		MyListview.setAdapter(listitemAdapter);
		
	MyListview.setOnItemClickListener(new OnItemClickListener(){
	
  @Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
  	
  	RelativeLayout lr=(RelativeLayout)arg1; 
  	TextView mText=(TextView)lr.getChildAt(1); 
		Toast.makeText(MainActivity.this, "你點擊了第" arg2 "項的" mText.getText().toString(),
				1000).show();
	}
		
		
	});
	}

  
  
  @Override 
  public boolean onCreateOptionsMenu(Menu menu) { 
  	
  
    
    menu.add(0, MENU_ITEM_COUNTER, 0, ".net项目"); 
    menu.add(0, MENU_ITEM_COUNTER  1, 0, "java项目"); 
    menu.add(0, MENU_ITEM_COUNTER  2, 0, "嵌入式项目"); 
  
    return super.onCreateOptionsMenu(menu); 
  } 

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		TextView myTextView=(TextView)findViewById(R.id.infoTextView);
		switch (item.getItemId()) {
		
		case MENU_ITEM_COUNTER:
			myTextView.setText("You click "  item.getTitle().toString()
					 "Menu");
			break;
		case MENU_ITEM_COUNTER  1:
			myTextView.setText("You click "  item.getTitle().toString()
					 "Menu");
			break;
		case MENU_ITEM_COUNTER  2:
			myTextView.setText("You click "  item.getTitle().toString()
					 "Menu");
			break;
		 default: 

				break;
		}
		return super.onOptionsItemSelected(item);
		 
	}
 
 

	 

}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复