1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| package cccc.cccc;
import java.util.List;
import com.example.gridviewstudy.R;
import android.annotation.SuppressLint; import android.content.Context; import android.content.Intent; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView;
public class GridViewAdapter extends BaseAdapter { private List<AppInfo> appInfos = null; private Context mContext = null; private LayoutInflater inflater = null; public GridViewAdapter(List<AppInfo> appInfos,Context context) { this.appInfos = appInfos; this.mContext = context; inflater = LayoutInflater.from(mContext); }
@Override public int getCount() { return appInfos.size(); }
@Override public Object getItem(int position) { return null; }
@Override public long getItemId(int position) { return 0; }
@SuppressLint("InflateParams") @Override public View getView(final int position, View convertView, ViewGroup parent) { Holder holder; if(convertView == null){ convertView = inflater.inflate(R.layout.gridview_item,null); holder = new Holder(); holder.icon = (ImageView)convertView.findViewById(R.id.imageView1); holder.name = (TextView)convertView.findViewById(R.id.textview1); convertView.setTag(holder); }else{ holder = (Holder)convertView.getTag(); } holder.icon.setImageDrawable(appInfos.get(position).getDrawable()); holder.name.setText(appInfos.get(position).getAppName()); convertView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { startAPP(appInfos.get(position).getAppPackage()); } }); return convertView; } public class Holder{ ImageView icon; TextView name; } public void startAPP(String appPackageName){ try{ Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(appPackageName); mContext.startActivity(intent); }catch(Exception e){ e.printStackTrace(); } } }
|