Daniel Gómez Martín
- Forum posts: 6
Jul 2, 2013, 12:31:25 AM via Website
Jul 2, 2013 12:31:25 AM via Website
Hello! I'm trying to develop an Android Launcher. First I want to "call" all apps in a Grid View. All works great, but appears a List View and I want a Grid. This is my code, where is the problem?
MainActivity.java
AppInfo.java
Utilities.java
Sorry if I put so much code but I don't know where is the problem.
MainActivity.java
1public class MainActivity extends Activity {
2 private GridView mGrid;
3
4 @Override
5 public void onCreate(Bundle savedInstanceState) {
6 super.onCreate(savedInstanceState);
7 // set layout for the main screen
8 setContentView(R.layout.layout_main);
9 // load list application
10 mGrid = (GridView)findViewById(R.id.lvApps);
11 // create new adapter
12 AppInfoAdapter adapter = new AppInfoAdapter(this, Utilities.getInstalledApplication(this), getPackageManager());
13 // set adapter to list view
14 mGrid.setAdapter(adapter);
15 // implement event when an item on list view is selected
16 mGrid.setOnItemClickListener(new OnItemClickListener() {
17 @Override
18 public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
19 // get the list adapter
20 AppInfoAdapter appInfoAdapter = (AppInfoAdapter)parent.getAdapter();
21 // get selected item on the list
22 ApplicationInfo appInfo = (ApplicationInfo)appInfoAdapter.getItem(pos);
23 // launch the selected application
24 Utilities.launchApp(parent.getContext(), getPackageManager(), appInfo.packageName);
25 }
26 });
27 }
28}
2 private GridView mGrid;
3
4 @Override
5 public void onCreate(Bundle savedInstanceState) {
6 super.onCreate(savedInstanceState);
7 // set layout for the main screen
8 setContentView(R.layout.layout_main);
9 // load list application
10 mGrid = (GridView)findViewById(R.id.lvApps);
11 // create new adapter
12 AppInfoAdapter adapter = new AppInfoAdapter(this, Utilities.getInstalledApplication(this), getPackageManager());
13 // set adapter to list view
14 mGrid.setAdapter(adapter);
15 // implement event when an item on list view is selected
16 mGrid.setOnItemClickListener(new OnItemClickListener() {
17 @Override
18 public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
19 // get the list adapter
20 AppInfoAdapter appInfoAdapter = (AppInfoAdapter)parent.getAdapter();
21 // get selected item on the list
22 ApplicationInfo appInfo = (ApplicationInfo)appInfoAdapter.getItem(pos);
23 // launch the selected application
24 Utilities.launchApp(parent.getContext(), getPackageManager(), appInfo.packageName);
25 }
26 });
27 }
28}
AppInfo.java
1public class AppInfoAdapter extends BaseAdapter {
2 private Context mContext;
3 private List<ApplicationInfo> mListAppInfo;
4 private PackageManager mPackManager;
5
6 public AppInfoAdapter(Context c, List<ApplicationInfo> list, PackageManager pm) {
7 mContext = c;
8 mListAppInfo = list;
9 mPackManager = pm;
10 }
11
12 @Override
13 public int getCount() {
14 return mListAppInfo.size();
15 }
16
17 @Override
18 public Object getItem(int position) {
19 return mListAppInfo.get(position);
20 }
21
22 @Override
23 public long getItemId(int position) {
24 return position;
25 }
26
27 @Override
28 public View getView(int position, View convertView, ViewGroup parent) {
29 // get the selected entry
30 ApplicationInfo entry = mListAppInfo.get(position);
31
32 // reference to convertView
33 View v = convertView;
34
35 // inflate new layout if null
36 if(v == null) {
37 LayoutInflater inflater = LayoutInflater.from(mContext);
38 v = inflater.inflate(R.layout.layout_appinfo, null);
39 }
40
41 // load controls from layout resources
42 ImageView ivAppIcon = (ImageView)v.findViewById(R.id.ivIcon);
43 TextView tvAppName = (TextView)v.findViewById(R.id.tvName);
44 TextView tvPkgName = (TextView)v.findViewById(R.id.tvPack);
45
46 // set data to display
47 ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
48 tvAppName.setText(entry.loadLabel(mPackManager));
49 tvPkgName.setText(entry.packageName);
50
51 // return view
52 return v;
53 }
54
55}
2 private Context mContext;
3 private List<ApplicationInfo> mListAppInfo;
4 private PackageManager mPackManager;
5
6 public AppInfoAdapter(Context c, List<ApplicationInfo> list, PackageManager pm) {
7 mContext = c;
8 mListAppInfo = list;
9 mPackManager = pm;
10 }
11
12 @Override
13 public int getCount() {
14 return mListAppInfo.size();
15 }
16
17 @Override
18 public Object getItem(int position) {
19 return mListAppInfo.get(position);
20 }
21
22 @Override
23 public long getItemId(int position) {
24 return position;
25 }
26
27 @Override
28 public View getView(int position, View convertView, ViewGroup parent) {
29 // get the selected entry
30 ApplicationInfo entry = mListAppInfo.get(position);
31
32 // reference to convertView
33 View v = convertView;
34
35 // inflate new layout if null
36 if(v == null) {
37 LayoutInflater inflater = LayoutInflater.from(mContext);
38 v = inflater.inflate(R.layout.layout_appinfo, null);
39 }
40
41 // load controls from layout resources
42 ImageView ivAppIcon = (ImageView)v.findViewById(R.id.ivIcon);
43 TextView tvAppName = (TextView)v.findViewById(R.id.tvName);
44 TextView tvPkgName = (TextView)v.findViewById(R.id.tvPack);
45
46 // set data to display
47 ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
48 tvAppName.setText(entry.loadLabel(mPackManager));
49 tvPkgName.setText(entry.packageName);
50
51 // return view
52 return v;
53 }
54
55}
Utilities.java
1public class Utilities {
2
3 /*
4 * Get all installed application on mobile and return a list
5 * @param c Context of application
6 * @return list of installed applications
7 */
8 public static List<ApplicationInfo> getInstalledApplication(Context c) {
9 return c.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
10 }
11
12 /*
13 * Launch an application
14 * @param c Context of application
15 * @param pm the related package manager of the context
16 * @param pkgName Name of the package to run
17 */
18 public static boolean launchApp(Context c, PackageManager pm, String pkgName) {
19 // query the intent for lauching
20 Intent intent = pm.getLaunchIntentForPackage(pkgName);
21 // if intent is available
22 if(intent != null) {
23 try {
24 // launch application
25 c.startActivity(intent);
26 // if succeed
27 return true;
28
29 // if fail
30 } catch(ActivityNotFoundException ex) {
31 // quick message notification
32 Toast toast = Toast.makeText(c, "Application Not Found", Toast.LENGTH_LONG);
33 // display message
34 toast.show();
35 }
36 }
37 // by default, fail to launch
38 return false;
39 }
40}
2
3 /*
4 * Get all installed application on mobile and return a list
5 * @param c Context of application
6 * @return list of installed applications
7 */
8 public static List<ApplicationInfo> getInstalledApplication(Context c) {
9 return c.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
10 }
11
12 /*
13 * Launch an application
14 * @param c Context of application
15 * @param pm the related package manager of the context
16 * @param pkgName Name of the package to run
17 */
18 public static boolean launchApp(Context c, PackageManager pm, String pkgName) {
19 // query the intent for lauching
20 Intent intent = pm.getLaunchIntentForPackage(pkgName);
21 // if intent is available
22 if(intent != null) {
23 try {
24 // launch application
25 c.startActivity(intent);
26 // if succeed
27 return true;
28
29 // if fail
30 } catch(ActivityNotFoundException ex) {
31 // quick message notification
32 Toast toast = Toast.makeText(c, "Application Not Found", Toast.LENGTH_LONG);
33 // display message
34 toast.show();
35 }
36 }
37 // by default, fail to launch
38 return false;
39 }
40}
Sorry if I put so much code but I don't know where is the problem.
— modified on Jul 2, 2013, 12:36:16 AM
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.