wolfcat90
02/07/13, 12:25:02
Hola! soy nuevo en este foro y necesito vuestra ayuda. He estado desarrollando una aplicacion tipo Launcher, y he empezado por crear un GridView y mostrar ahi todas las apps. Mi problema es que en vez de un GridView aparece un ListView. Alguien puede ayudarme? este es mi codigo:
MainActivity.java
public class MainActivity extends Activity {
private GridView mGrid;
@Override
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() {
@Override
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<ApplicationInfo> mListAppInfo;
private PackageManager mPackManager;
public AppInfoAdapter(Context c, List<ApplicationInfo> list, PackageManager pm) {
mContext = c;
mListAppInfo = list;
mPackManager = pm;
}
@Override
public int getCount() {
return mListAppInfo.size();
}
@Override
public Object getItem(int position) {
return mListAppInfo.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// get the selected entry
ApplicationInfo entry = 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);
TextView tvPkgName = (TextView)v.findViewById(R.id.tvPack);
// set data to display
ivAppIcon.setImageDrawable(entry.loadIcon(mPackMan ager));
tvAppName.setText(entry.loadLabel(mPackManager));
tvPkgName.setText(entry.packageName);
// return view
return v;
}
}
Utilities.java
public class Utilities {
/*
* Get all installed application on mobile and return a list
* @paramcContext of application
* @returnlist of installed applications
*/
public static List<ApplicationInfo> getInstalledApplication(Context c) {
return c.getPackageManager().getInstalledApplications(Pac kageManager.GET_META_DATA);
}
/*
* Launch an application
* @paramcContext of application
* @parampmthe related package manager of the context
* @parampkgNameName 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;
}
}
layout_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/Example Button"
android:text="Caca"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<GridView
android:id="@+id/lvApps"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
layout_appinfo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="5dip"
>
<ImageView
android:id="@+id/ivIcon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="5dip"
android:scaleType="center"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
>
<TextView
android:id="@+id/tvName"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
/>
</LinearLayout>
</LinearLayout>
Para no ocupar mucho mas espacio he eliminado los imports y el package name.
Espero vuestra ayuda! ;)
MainActivity.java
public class MainActivity extends Activity {
private GridView mGrid;
@Override
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() {
@Override
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<ApplicationInfo> mListAppInfo;
private PackageManager mPackManager;
public AppInfoAdapter(Context c, List<ApplicationInfo> list, PackageManager pm) {
mContext = c;
mListAppInfo = list;
mPackManager = pm;
}
@Override
public int getCount() {
return mListAppInfo.size();
}
@Override
public Object getItem(int position) {
return mListAppInfo.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// get the selected entry
ApplicationInfo entry = 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);
TextView tvPkgName = (TextView)v.findViewById(R.id.tvPack);
// set data to display
ivAppIcon.setImageDrawable(entry.loadIcon(mPackMan ager));
tvAppName.setText(entry.loadLabel(mPackManager));
tvPkgName.setText(entry.packageName);
// return view
return v;
}
}
Utilities.java
public class Utilities {
/*
* Get all installed application on mobile and return a list
* @paramcContext of application
* @returnlist of installed applications
*/
public static List<ApplicationInfo> getInstalledApplication(Context c) {
return c.getPackageManager().getInstalledApplications(Pac kageManager.GET_META_DATA);
}
/*
* Launch an application
* @paramcContext of application
* @parampmthe related package manager of the context
* @parampkgNameName 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;
}
}
layout_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/Example Button"
android:text="Caca"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<GridView
android:id="@+id/lvApps"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
layout_appinfo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="5dip"
>
<ImageView
android:id="@+id/ivIcon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="5dip"
android:scaleType="center"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
>
<TextView
android:id="@+id/tvName"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
/>
</LinearLayout>
</LinearLayout>
Para no ocupar mucho mas espacio he eliminado los imports y el package name.
Espero vuestra ayuda! ;)