[Java for Android App Development] DannyGM16:
Hello XDA users! 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

public class MainActivity extends Activity {
private GridView mGrid;

[MENTION=439709]override[/MENTION]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set layout for the main screen
setContentView(R.layout.layout_main);
// load list application
mGrid = (GridView)findViewById(R.id.lvApps);
// create new adapter
AppInfoAdapter adapter = new AppInfoAdapter(this, Utilities.getInstalledApplication(this), getPackageManager());
// set adapter to list view
mGrid.setAdapter(adapter);
// implement event when an item on list view is selected
mGrid.setOnItemClickListener(new OnItemClickListener() {
[MENTION=439709]override[/MENTION]
public void onItemClick(AdapterView parent, View view, int pos, long id) {
// get the list adapter
AppInfoAdapter appInfoAdapter = (AppInfoAdapter)parent.getAdapter();
// get selected item on the list
ApplicationInfo appInfo = (ApplicationInfo)appInfoAdapter.getItem(pos);
// launch the selected application
Utilities.launchApp(parent.getContext(), getPackageManager(), appInfo.packageName);
}
});
}
}

AppInfo.java

public class AppInfoAdapter extends BaseAdapter {

private Context mContext;

private List mListAppInfo;

private PackageManager mPackManager;

public AppInfoAdapter(Context c, List list, PackageManager pm) {

mContext = c;

mListAppInfo = list;

mPackManager = pm;

}
[MENTION=439709]override[/MENTION]

public int getCount() {

return mListAppInfo.size();

}

[MENTION=439709]override[/MENTION]

public Object getItem(int position) {

return mListAppInfo.get(position);

}

[MENTION=439709]override[/MENTION]

public long getItemId(int position) {

return position;

}

[MENTION=439709]override[/MENTION]

public View getView(int position, View convertView, ViewGroup parent) {

// get the selected entry

ApplicationInfo entry = (ApplicationInfo) mListAppInfo.get(position);

// reference to convertView

View v = convertView;

// inflate new layout if null

if(v == null) {

LayoutInflater inflater = LayoutInflater.from(mContext);

v = inflater.inflate(R.layout.layout_appinfo, null);

}

// load controls from layout resources

ImageView ivAppIcon = (ImageView)v.findViewById(R.id.ivIcon);

TextView tvAppName = (TextView)v.findViewById(R.id.tvName);

// set data to display

ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));

tvAppName.setText(entry.loadLabel(mPackManager));

// return view

return v;

}

}

Utilities.java

public class Utilities {

/*

* Get all installed application on mobile and return a list

* [MENTION=955119]param[/MENTION]cContext of application

* [MENTION=2056652]return[/MENTION]list of installed applications

*/

public static List<ApplicationInfo> getAllInstalledApplications(Context context) {
List<ApplicationInfo> installedApps = context.getPackageManager().getInstalledApplications(PackageManager.PERMISSION_GRANTED);
List<ApplicationInfo> launchableInstalledApps = new ArrayList<ApplicationInfo>();
for(int i =0; i<installedApps.size(); i++){
if(context.getPackageManager().getLaunchIntentForPackage(installedApps.get(i).packageName) != null){
//If you're here, then this is a launch-able app
launchableInstalledApps.add(installedApps.get(i));
}
}
return launchableInstalledApps;
}

/*

* Launch an application

* [MENTION=955119]param[/MENTION]cContext of application

* [MENTION=955119]param[/MENTION]pmthe related package manager of the context

* [MENTION=955119]param[/MENTION]pkgNameName of the package to run

*/

public static boolean launchApp(Context c, PackageManager pm, String pkgName) {

// query the intent for lauching

Intent intent = pm.getLaunchIntentForPackage(pkgName);

// if intent is available

if(intent != null) {

try {

// launch application

c.startActivity(intent);

// if succeed

return true;

// if fail

} catch(ActivityNotFoundException ex) {

// quick message notification

Toast toast = Toast.makeText(c, "Application Not Found", Toast.LENGTH_LONG);

// display message

toast.show();

}

}

// by default, fail to launch

return false;

}

}

Sorry if I put so much code but I don't know where is the problem.

Enviado desde mi GT-S5570I usando Tapatalk 2