package com.example.ex_day00_mb;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.ClipData.Item;
import android.content.Intent;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
int[] tupian = {R.drawable.cn01,R.drawable.cn02,R.drawable.cn03,R.drawable.cn04,
R.drawable.cn05,R.drawable.cn06,R.drawable.cn07,R.drawable.cn08,
R.drawable.cn09,R.drawable.cn10,R.drawable.cn11,R.drawable.cn12};
String[] text = {"时钟","机器人管家","二维码扫描","蓝牙","世界地图",
"wifi共享软件","计算器","日历","录像机","照相机","指南针","小书童"};
String[] text2 = {"下载信息:版本 v3.0 受欢迎:55% 大小3.3M","下载信息:版本 v5.2 受欢迎:88% 大小 9.3M,下载信息:版本 v8.2 受欢迎:88% 大小 2.3M","下载信息:版本 v8.2 受欢迎:98% 大小 2.3M," +
"下载信息:版本 v3.2 受欢迎:98% 大小 2.3M","下载信息:版本 v3.2 受欢迎:98% 大小 2.3M," +
"下载信息:版本 v3.2 受欢迎:98% 大小 2.3M","下载信息:版本 v3.2 受欢迎:98% 大小 2.3M," +
"下载信息:版本 v3.2 受欢迎:98% 大小 2.3M","下载信息:版本 v3.2 受欢迎:98% 大小 2.3M," +
"下载信息:版本 v3.2 受欢迎:88% 大小 2.3M","下载信息:版本 v8.2 受欢迎:98% 大小 2.3M," +
"下载信息:版本 v3.2 受欢迎:98% 大小 2.3M","下载信息:版本 v3.2 受欢迎:98% 大小 2.3M," +
"下载信息:版本 v3.2 受欢迎:98% 大小 2.3M","下载信息:版本 v8.2 受欢迎:98% 大小 2.3M," +
"下载信息:版本 v3.2 受欢迎:98% 大小 2.3M","下载信息:版本 v3.2 受欢迎:98% 大小 2.3M," +
"下载信息:版本 v5.2 受欢迎:98% 大小 2.3M","下载信息:版本 8.2 受欢迎:98% 大小 2.3M," +
"下载信息:版本 v3.2 受欢迎:98% 大小 2.3M","下载信息:版本 v3.2 受欢迎:98% 大小 2.3M"};
class Mydate
{
int tupian;
String text;
String text2;
public Mydate(int tupian, String text, String text2) {
super();
this.tupian = tupian;
this.text = text;
this.text2 = text2;
}
}
ArrayList<Mydate> list = new ArrayList<Mydate>();
MyAdapter myAdapter = null;
int pos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listview = (ListView)findViewById(R.id.listView1);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
for (int i = 0; i < tupian.length; i++) {
list.add(new Mydate(tupian[i],text[i],text2[i]));
}
myAdapter = new MyAdapter();
listview.setAdapter(myAdapter);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Mydate mydate = list.get(position);
Intent intent = new Intent();
intent.putExtra("name", mydate.text+"下载");
intent.putExtra("tupian", mydate.tupian);
intent.setClass(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
listview.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
pos = position;
listview.showContextMenu();
return true;
}
});
listview.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.setHeaderTitle(list.get(pos).text);
menu.add(0, 1, 0, "编辑");
menu.add(0,2,0,"***");
menu.add(0, 3, 0, "取消");
}
});
}
@Override
public boolean onContextItemSelected(MenuItem item) {
Mydate mydate = list.get(pos);
switch (item.getItemId()) {
case 1:
Toast.makeText(this, "��"+mydate.text, Toast.LENGTH_LONG).show();
break;
case 2:
Toast.makeText(this, "�h除"+mydate.text, Toast.LENGTH_LONG).show();
break;
case 3:
Toast.makeText(this, "取消"+mydate.text, Toast.LENGTH_LONG).show();
break;
default:
break;
}
return super.onContextItemSelected(item);
}
class User
{
ImageView imageview = null;
TextView textview = null;
TextView textview2 = null;
public User()
{
}
}
class MyAdapter extends BaseAdapter
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
User user = new User();
LayoutInflater layoutInflater = null;
View inflate = null;
// ImageView imageview = null;
// TextView textview = null;
// TextView textview2 = null;
if (convertView == null) {
//得到布局转换器
layoutInflater = getLayoutInflater();
//把布局文件转换成布局对象
inflate = layoutInflater.inflate(R.layout.itemp, null);
user.textview = (TextView)inflate.findViewById(R.id.textView1);
user.imageview = (ImageView)inflate.findViewById(R.id.imageView1);
user.textview2 = (TextView)inflate.findViewById(R.id.textView2);
//把�ο蟠嫫��
inflate.setTag(user);
}else
{
//用已经完全消失的布局对象去替代即将出现的布局对象
inflate = convertView;
user = (User)inflate.getTag();
}
Mydate mydate = list.get(position);
user.imageview.setImageResource(mydate.tupian);
user.textview.setText(position+1+"."+mydate.text);
Log.e("MyAdapter",""+position+convertView);
user.textview2.setText(mydate.text2);
return inflate;
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return 0;
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
list.add(new Mydate(R.drawable.cn13,"小书童","我爱android"));
myAdapter.notifyDataSetChanged();
break;
case R.id.button2:
if(list.size()>0)
{
list.remove(list.size()-1);
myAdapter.notifyDataSetChanged();
}
break;
default:
break;
}
}
}
//notifyDataSetChanged() //更新List,增加数据