11.安卓(gridview的使用)

分享到:

<GridView
android:listSelector="@drawable/item_background_selector"
android:id="@+id/gv_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dip"
android:numColumns="3">//3列
</GridView>

 

 

 

private GridView gv_main;

gv_main.setAdapter(adapter);
gv_main.setOnItemClickListener(this);
gv_main.setOnItemLongClickListener(new OnItemLongClickListener() {

public boolean onItemLongClick(AdapterView<?> parent,
final View view, int position, long id) {
if (position == 0) {
AlertDialog.Builder buider = new Builder(MainActivity.this);
buider.setTitle("设置");
buider.setMessage("请输入要更改的名称");
final EditText et = new EditText(MainActivity.this);
et.setHint("请输入文本");
buider.setView(et);
buider.setPositiveButton("确定", new OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
String name = et.getText().toString().trim();
if ("".equals(name)) {
Toast.makeText(getApplicationContext(),
"内容不能为空", 0).show();
return;
} else {
Editor editor = sp.edit();
editor.putString("lost_name", name);
// 完成数据的提交
editor.commit();
TextView tv = (TextView) view
.findViewById(R.id.tv_main_name);
tv.setText(name);
}

}
});
buider.setNegativeButton("取消", new OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
}
});

buider.create().show();
}
return false;
}
});
}

/**
* 当gridview的条目被点击的时候 对应的回调 parent : girdview view : 当前被点击的条目 Linearlayout
* position : 点击条目对应的位置 id : 代表的行号
*/

public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
switch (position) {
case 0: // 手机防盗
Log.i(TAG, "进入手机防盗");
Intent lostIntent = new Intent(MainActivity.this, LostProtectedActivity.class);
startActivity(lostIntent);
//finish();//一 关整个程序就死咯
break;
}

}

 

 

 

数据适配器:

package com.example.mobilesafe.dapter;

import com.example.mobilesafe1.R;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MainUIAdapter extends BaseAdapter {
private static final String TAG = "MainUIAdapter";
private Context context;
private LayoutInflater inflater;
private static ImageView iv_icon;
private static TextView tv_name;
private SharedPreferences sp;

public MainUIAdapter(Context context) {
this.context = context;
inflater = LayoutInflater.from(context);
sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
}

private static String[] names = { "手机防盗", "通讯卫士", "软件管理", "任务管理", "流量管理",
"手机杀毒", "系统优化", "高级工具", "设置中心" };
private static int[] icons = { R.drawable.widget05, R.drawable.widget02,
R.drawable.widget01, R.drawable.widget07, R.drawable.widget05,
R.drawable.widget04, R.drawable.widget06, R.drawable.widget03,
R.drawable.widget08 };

public int getCount() {

return names.length;
}

public Object getItem(int position) {

return position;
}

public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
// getview的方法被调用了多少次?
// 9
// gridview 控件bug
// won't fix
// 使用静态的变量引用 减少内存中申请的引用的个数
if(null == convertView)
{
convertView = inflater.inflate(R.layout.mainscreen_item, null);
}
iv_icon = (ImageView) convertView.findViewById(R.id.iv_main_icon);
tv_name = (TextView) convertView.findViewById(R.id.tv_main_name);
iv_icon.setImageResource(icons[position]);
tv_name.setText(names[position]);
if(position==0){

String name = sp.getString("lost_name", null);
if(name!=null){
tv_name.setText(name);
}
//convertView = inflater.inflate(R.layout.mainscreen_item, null);
//return convertView;
}
return convertView;
}

}

 

 

 

昵    称:
验证码:

相关文档: