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); } }
评论