Display ListView Item(ImageView) to another activity - android

I am having a simple listview with an ImageView and a textView.
Now whenever I click on my listItem I need to send the ImageView to another activity.
I have read many SO answers but couldn't find a better solution for this.
This is my lisview_items.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/photo"
android:layout_width="150dip"
android:layout_height="100dip"
android:paddingRight="15dp"
android:paddingLeft="15dp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="160dp"
android:layout_marginTop="10dp"
android:descendantFocusability="blocksDescendants">
<TextView android:id="#+id/name"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
<RelativeLayout>
CustomAdapter:
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.item_details_view, null);
holder = new ViewHolder();
holder.txt_itemName = (TextView) convertView.findViewById(R.id.name);
holder.itemImage = (ImageView) convertView.findViewById(R.id.photo);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txt_itemName.setText(itemDetailsrrayList.get(position).getName());
holder.itemImage.setImageResource(imgid[itemDetailsrrayList.get(position).getImageNumber() - 1]);
}
MyListView OnClick:
ArrayList<ItemDetails> image_details = GetSearchResults();
final ListView lv1 = (ListView) findViewById(R.id.list);
lv1.setAdapter(new ItemListBaseAdapter(this, image_details));
OnItemClickListener itemClickListener = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = lv1.getItemAtPosition(position);
ItemDetails obj_itemDetails = (ItemDetails)o;
Toast.makeText(ListViewImagesActivity.this, "You have chosen : " + " " + obj_itemDetails.getName(), Toast.LENGTH_LONG).show();
ProductName =obj_itemDetails.getName();
}
};
I am using sharedpreferences for sending product name as shown below and it works absolutely fine
SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
data.edit().putString("SelectedProduct", ProductName).commit();
In the similar way how do I send the image as it is in ArrayList as shown above..Can anyone shed me some light on this usage

You can store the image path on the ItemDetails object like you are doing with the name. Then you can pass the path via an Intent to the new Activity.
If the picture is available at compile time, you can pass a reference to its id, like Gabe said, instead of the path to its location.

If you are actually switching to that activity, you can pass the resource id of the image in the intent extras and then use getResources().getDrawable(ID) there.

You should the following :
final ArrayList<ItemDetails> image_details = GetSearchResults();
final ListView lv1 = (ListView) findViewById(R.id.list);
lv1.setAdapter(new ItemListBaseAdapter(this, image_details));
OnItemClickListener itemClickListener = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
ItemDetails selectedItem = image_details.get(position);
//To-DO get you data from the ItemDetails Getter
// selectedItem.getImage() or selectedItem.getName() .. etc
// the send the data using intent when opening another activity
Intent intent = new Intent(CurrentActivity.this, DisplayImageActivity.class);
intent.putExtra("image",selectedItem.getImage());
intent.putExtra("name",selectedItem.getName());
startActivty(intent);
}
};
Then in your DisplayImageActivity in the OnCreate
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = getIntent();
String image = i.getStringExtra("image");
String name = i.getStringExtra("name");
}

Related

how to setOnItemClickListener on listview and call the second activity

I m raeding csv file and showin it using the listview and textview so i want set setOnItemClickListener on list view of text...and when i click on item then other activity will get open..so please tell how do i code for this..
Here is my code...
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//String listItem = (String) listView.getItemAtPosition(position);
String listItem = (String) parent.getAdapter().getItem(3);
Toast.makeText(getApplicationContext(), listItem.toString(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), Editinfo.class);
intent.putExtra("spinner", chosenOption);
startActivity(intent);
}
});
}
Here is catlog report..
12-08 11:17:28.630 20936-20936/com.technostark.com.myloginactivity W/ResourceType: Skipping entry 0x108035a in package table 0 because it is not complex!
Update
<RelativeLayout
xmlns:android= "schemas.android.com/apk/res/android";
xmlns:tools= "schemas.android.com/tools";
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/shape">
<ListView android:id="#+id/editlistview"
android:layout_width="match_parent"
android:layout_height= "wrap_content" />
</RelativeLayout>
Here is textview xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/et_Studname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="Student Name"
android:textColorHint="#660000"
android:textSize="20dp"
android:textColor="#660000"
android:clickable="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:ems="20" />
</LinearLayout>
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, movieList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView c = (TextView) view.findViewById(R.id.rating);
TextView d = (TextView) view.findViewById(R.id.title);
TextView e = (TextView) view.findViewById(R.id.releaseYear);
//Toast.makeText(Video_Main_List.this,Config.YOUTUBE_VIDEO_CODE, Toast.LENGTH_SHORT).show();
Intent launch = new Intent(Video_Main_List.this,Youtube_Player_Activity.class);
launch.putExtra("c", c.getText().toString());
launch.putExtra("d", d.getText().toString());
launch.putExtra("e", e.getText().toString());
startActivity(launch);
}});
// you can also take value from listview items and send into second activity that i mansion above.
ListView list = (ListView) findViewById(R.id.listview);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String listItem = (String) parent.getAdapter().getItem(3);
Toast.makeText(getApplicationContext(), listItem.toString(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, Editinfo.class);
intent.putExtra("spinner", chosenOption);
startActivity(intent);
}
});

How to access listview items generated by ArrayAdapter by CLICKING ON BUTTON

I'm using a ListView with ArrayAdapter. There is a Button along with TextView in the listview.xml. I want to retrieve data from that TextView by Clicking over corresponding buttons.
My listview.xml is as follows:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="90dp"
android:id="#+id/btConfirmfriend"
android:text="Confirm Friend"
/>
</RelativeLayout>
My PendingRequest Class is as follows:
public class PendingRequest extends AddFriend {
TextView tvPending;
Button btConfirmfriend;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pendingrequest);
final String[] s= getIntent().getExtras().getStringArray("pending");
System.out.println(s[1]);
final ListAdapter adapter = new ArrayAdapter<String>(this,R.layout.listview,R.id.label,s);
final ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
final View inflatedView = getLayoutInflater().inflate(R.layout.listview, null);
btConfirmfriend = (Button) inflatedView.findViewById(R.id.btConfirmfriend);
btConfirmfriend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ServerConnection sc = new ServerConnection();
//here I want to retrieve the value from List
//System.out.println(s[adapter.]);
int status = sc.confirmRequest(s[listView.getSelectedItemPosition()]);
AlertDialog.Builder alert = new AlertDialog.Builder(PendingRequest.this);
if (status == 1) {
alert.setMessage("Friend has been Added");
alert.show();
} else {
alert.setMessage("Sorry for inconvinient!!Try Again");
alert.show();
}
}
});
}
}
My pendingrequest.xml file is:-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="#drawable/background"
android:layout_height="match_parent">
<Button
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="100dp"
android:background="#e9abab"
android:id="#+id/btSend"
android:text="Pending Requests"
/>
<ListView
android:id="#+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
On clicking over the button nothing happening.onClick() method is not executing.Please help.
Since you need a click listener on a view inside your list item, you need to write your own adapter for that -
class CustomAdapter extends ArrayAdapter<String> {
public CustomAdapter(Context context, String[] list) {
super(context, 0, list);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// Get the data item for this position
String item = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
// Lookup view for data population
TextView label = (TextView) convertView.findViewById(R.id.label);
Button btConfirmfriend = (Button) convertView.findViewById(R.id.btConfirmfriend);
// Populate the data into the template view using the data object
label.setText(item);
btConfirmfriend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getItem(position); //Get your selected text
}
});
return convertView;
}
}
And this is how you will set the adapter -
final CustomAdapter adapter = new CustomAdapter(this,s);
final ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setAdapter(adapter);

ImageView set image Resource doesn't show wanted image.png

In my gridViewAdapter I set the ImageResource to the .png object but it doesn't show in activity. Here's the adapter's function getView:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.photoSubscriptTextView = (TextView) row.findViewById(R.id.photoSubscriptTextView);
holder.photoInstanceImageView = (ImageView) row.findViewById(R.id.galleryMonumentPhotoImageView);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
final Photo photo = data.get(position);
row.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent;
if(photo.getPodpis().equals(getContext().getString(R.string.photo_add))){
intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
((Activity) context).startActivityForResult(Intent.createChooser(intent, "Select Picture"), 100);
}
else {
intent = new Intent(context, PhotoGallery.class);
intent.putExtra("photo", photo);
context.startActivity(intent);
}
}
});
holder.photoSubscriptTextView.setText(photo.getPodpis());
if(photo.getUri() != null)
aQuery.id(holder.photoInstanceImageView).image(photo.getUri());
else if(photo.getPodpis().equals(getContext().getResources().getString(R.string.photo_add))) {
holder.photoInstanceImageView.setImageResource(R.drawable.image_add);
}
return row;
And the xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="vertical"
android:padding="5dp"
android:clickable="true"
android:focusable="true">
<!--android:background="#drawable/grid_color_selector"-->
<ImageView
android:id="#+id/galleryMonumentPhotoImageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/image_add">
</ImageView>
<TextView
android:id="#+id/photoSubscriptTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:textSize="12sp" >
</TextView>
</LinearLayout>
The main layout contains only the Relative Layout and GridView.
Can anybody tell me why image from resources that I set in adapter doesn't show in activity?
Just because you put the onClick() method inside of getView() does not mean it has access to temporary variables created within getView(). The item clicked will likely be a different one than the getView() method last processed. So, there is no connection between them.
You should instead put a onItemClick() listener on the ListView itself in your activity onCreate() or onCreateView() code. That method has the position of the item that was clicked, and you can then get that item from your ArrayList using the position.
Like this:
mListView.setOnItemClickListener(new ListView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long row_id) {
final Photo photo = data.get(position);
// etc.
}
}

How to get value from edittext in gridview

I have used grid view in my project
My activity layout file
<GridView
android:id="#+id/gridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:numColumns="1"
android:stretchMode="spacingWidth"
android:layout_below="#+id/spinner1"
android:layout_marginLeft="20dip"
android:layout_marginRight="10dip"
android:layout_marginTop="20dip"
android:visibility="visible" >
</GridView>
My grid layout(grid_change_prices.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="320dp"
android:layout_height="match_parent"
android:clickable="false"
>
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:background="#ff0000" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dip"
android:background="#ffffff"
android:paddingTop="0dip" >
<TextView
android:id="#+id/cplabel"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:paddingLeft="5dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/cpprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/add_price"
android:layout_marginTop="0dp"
android:textSize="12dp"
android:inputType="number" />
</TableRow>
</TableLayout>
</RelativeLayout>
My Output from emulator
check this URL
http://i.stack.imgur.com/Uc6cs.png
My code file
public void fillgrid(int categoryid,String car){
gridView = (GridView) findViewById(R.id.gridView);
final DatabaseClass2 db = new DatabaseClass2(this);
db.openDataBase();
Cursor c = db.getprice(categoryid);
startManagingCursor(c);
String[] fromColumns = {"_id",DatabaseClass2.colPrice};
int[] toViews = {R.id.cplabel, R.id.cpprice};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.grid_change_prices, c, fromColumns, toViews);
//c.moveToNext();
gridView.setAdapter(adapter); }
Now my query is
When i click modify button, I want to update my database with the new values
i tried to get values from edit text by using .gettext() .
but it only returns value from first edittext
How i got the values from other edittexts, with the row id as well
so that i update database with new price of same design..?
Why are you using a GridView for such simple work?
better if you use ListView and it would be easier to populate the list and getting values from other EditText's.
I can give you an idea... just see this code what I'm doing in this :
public class RecordExpenseAdapter extends ArrayAdapter<HashMap<String, String>>
{
Intent intent;
Context context;
int layoutResourceId;
ArrayList<HashMap<String, String>> RecordList = new ArrayList<HashMap<String, String>>();
public RecordExpenseAdapter(Context context, int layoutResourceId,ArrayList<HashMap<String, String>> RecordList)
{
super(context, layoutResourceId, RecordList);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.RecordList = RecordList;
this.intent = new Intent(context, ScanReceipt.class);
//Log.d("ASD:","comp intialize");
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ImageHolder holder = null;
try{
if(row == null)
{
Log.d("ASD:","insdie getview");
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ImageHolder();
holder.categoryName = (TextView)row.findViewById(R.id.textView1);
holder.date = (TextView)row.findViewById(R.id.textView2);
holder.amount = (TextView)row.findViewById(R.id.textView3);
holder.record_rid = (TextView)row.findViewById(R.id.ridRecord);
holder.cid=(TextView)row.findViewById(R.id.cid);
holder.vendor=(TextView)row.findViewById(R.id.vendor);
holder.description=(TextView)row.findViewById(R.id.desc);
holder.attachURL=(TextView)row.findViewById(R.id.userFile);
holder.attachImage=(ImageView)row.findViewById(R.id.attachment);
holder.scanReceipt = (ImageView)row.findViewById(R.id.imageView2);
row.setTag(holder);
holder.scanReceipt.setTag(position);
holder.scanReceipt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
Log.e("Scan Receipt Button List", "1");
ImageView iv = (ImageView) v;
Log.e("Scan Receipt Button List", "2");
int pos = (Integer) iv.getTag();
Log.e("Scan Receipt Button List", "3");
ScanReceipt.rid = RecordList.get(pos).get("rid").toString();
Log.e("Scan Receipt Button List", "4");
//Toast.makeText(context, "Button Clicked", Toast.LENGTH_LONG).show();
RecordExpenseAdapter.this.doIt();
}catch(Exception e){
Log.e("Error on click on Scan Receipt Button List", e.getMessage().toString());
}
}
});
// if(picture.get("userfile")=="no")
// {
// holder.attachImage.setVisibility(View.INVISIBLE);
// }
// else
// {
// holder.attachImage.setVisibility(View.VISIBLE);
// }
row.setTag(holder);
}
else
{
holder = (ImageHolder)row.getTag();
}
HashMap<String, String> picture = RecordList.get(position);
holder.categoryName.setText(picture.get("category_name"));
holder.date.setText(picture.get("date"));
holder.amount.setText(picture.get("amount"));
holder.record_rid.setText(picture.get("rid"));
holder.cid.setText(picture.get("cid"));
holder.vendor.setText(picture.get("vendor"));
holder.description.setText(picture.get("description"));
holder.attachURL.setText(picture.get("image"));
Log.e("Record Expense Adapter 0 : IMAGE", picture.get("image").toString());
Log.e("RID : " + position, picture.get("rid").toString());
// Log.e("error value", picture.get("userfile"));
//Log.e("error value", "value is error");
// if(picture.get("userfile")=="no")
// {
// holder.attachImage.setVisibility(View.INVISIBLE);
// }
//
// else
// {
// holder.attachImage.setVisibility(View.VISIBLE);
// }
if(picture.get("image").toString().trim().equalsIgnoreCase("no")){
holder.scanReceipt.setVisibility(View.VISIBLE);
holder.attachImage.setVisibility(View.INVISIBLE);
}else{
holder.scanReceipt.setVisibility(View.GONE);
holder.attachImage.setVisibility(View.VISIBLE);
}
}
catch(Exception e){
}
return row;
}
public void doIt(){
Log.e("Adapter 0", "In do It");
context.startActivity(intent);
}
static class ImageHolder
{
TextView categoryName;
TextView date;
TextView amount;
TextView record_rid;
TextView vendor;
TextView cid;
TextView description;
TextView attachURL;
ImageView attachImage,scanReceipt;
}
}
in above code, I have implemented the click listener on ImageView scanReceipt. It's the same imageview each time i.e. the same layout is inflated for each row, which means the same id is inflated for all the rows but still this click event will perform a different task for different row.
what you need to do is : simply use the EditText as I'm using the ImageView. Implement the TextWatcher as I'm implementing the click listener.
you have to use custom list for that say this-
List<CustomClass> list = new ArrayList<CustomClass>();
in this custom class, put the public variables so that you can access those by object of CustomClass and add that object to this above created list.
Once you get the values in this list, you can use it any way you want.
I hope it would help... :)
#Prakash
thanxx for idea dude
Now, I attached textwatcher to edittext field and managed to get rowid
with the help of this
price_text.setTag(c.getPosition());
And retreive that position in afterTextChanged
price_text.getTag()
But now how could i save that values Like you said in above post....

Issues Displaying a ListView of All Installed Apps and Implementing an OnItemClickListener

I am trying to write a very simple app that displays the name of every installed app on the device in a listview. I am using Google's ListView tutorial as a base.
Here is my code:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
final PackageManager pm = this.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final ArrayList<ResolveInfo> list =
(ArrayList<ResolveInfo>) pm.queryIntentActivities(intent,
PackageManager.PERMISSION_GRANTED);
for (ResolveInfo rInfo : list)
{
Log.i(TAG, ": Installed Applications " + rInfo.activityInfo.
applicationInfo.loadLabel(pm).toString());
}
final ArrayAdapter<ResolveInfo> adapter =
new ArrayAdapter<ResolveInfo>(this, R.layout.list_item, list)
{
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
convertView = LayoutInflater.from(parent.getContext()).
inflate(R.layout.list_item, parent, false);
final String text = list.get(position).activityInfo.
applicationInfo.loadLabel(pm).toString();
((TextView)convertView.findViewById(R.id.text)).setText(text);
final Drawable drawable = list.get(position).activityInfo.applicationInfo.loadIcon(pm);
((ImageView)convertView.findViewById(R.id.image)).setImageDrawable(drawable);
return convertView;
}
};
setListAdapter(adapter);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// On Item Click Activity
// This is where I want to send the Package Name of the app selected to be passed to a method.
}
});
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="#+id/image"
android:layout_width="50dp" android:layout_height="50dp" />
<TextView android:id="#+id/text"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:padding="10dp" android:textSize="16sp" />
</LinearLayout>
UPDATE: I now need to use an OnItemClickListener to pass the Package Name of the App selected to a method.
To display your activities' names correctly in the list, you should override the getView method of your ListAdapter, and set some of your local variables as final (to work with inner class):
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
final PackageManager pm = this.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final ArrayList<ResolveInfo> list =
(ArrayList<ResolveInfo>) pm.queryIntentActivities(intent,
PackageManager.PERMISSION_GRANTED);
for (ResolveInfo rInfo : list)
{
Log.i(TAG, ": Installed Applications " + rInfo.activityInfo.
applicationInfo.loadLabel(pm).toString());
}
final ArrayAdapter<ResolveInfo> adapter =
new ArrayAdapter<ResolveInfo>(this, R.layout.list_item, list)
{
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
convertView = super.getView(position, convertView, parent);
final String text = list.get(position).activityInfo.
applicationInfo.loadLabel(pm).toString();
((TextView)convertView).setText(text);
return convertView;
}
};
setListAdapter(adapter);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
}
This way you have your custom ArrayAdapter implementation, which displays the proper label of applicationinfo in the TextView.
You can also achieve this, if you create a new ArrayList<String>, and populate it inside the for cycle where you log the applications:
final ArrayList<String> labelList = new ArrayList<String>();
for (ResolveInfo rInfo : list)
{
Log.i(TAG, ": Installed Applications " + rInfo.activityInfo.
applicationInfo.loadLabel(pm).toString());
labelList.add(rInfo.activityInfo.
applicationInfo.loadLabel(pm).toString());
}
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.list_item, labelList);
setListAdapter(adapter);
Then you use this new labelList as the source of your adapter.
Update
To include the icon into the item renderers, the overridden getView method would look like:
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
convertView = LayoutInflater.from(parent.getContext()).
inflate(R.layout.list_item, parent, false);
final String text = list.get(position).activityInfo.
applicationInfo.loadLabel(pm).toString();
((TextView)convertView.findViewById(R.id.text)).setText(text);
final Drawable drawable = list.get(position).activityInfo.applicationInfo.loadIcon(pm);
((ImageView)convertView.findViewById(R.id.image)).setImageDrawable(drawable);
return convertView;
}
and your res/layout/list_item.xml layout file has to contain the text TextView and the image ImageView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="#+id/image"
android:layout_width="50dp" android:layout_height="50dp" />
<TextView android:id="#+id/text"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:padding="10dp" android:textSize="16sp" />
</LinearLayout>

Categories

Resources