Start an activity from a listview with searchbox - java

Im having trouble with my listview and search box. Searching works fine, except for the fact that it is case sensitive. However, my problem is, starting an activity right after clicking.
The start activity is based on the position of the text. Therefore, if I dont conduct any research the links work fine. Yet, if i research for specifics the listview works but the links are wrong, because they are based on the initial position of the listview and not sorted by categories.
import greendroid.app.GDActivity;
import greendroid.widget.ActionBarItem;
import greendroid.widget.NormalActionBarItem;
import java.util.ArrayList;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class m_m_aeritalia extends GDActivity {
EditText edittext;
ListView listview;
Button search;
String[] text = { "(Lockheed) F-104S Starfighter",
"Aermecchi / EMBRAER AMX", "G-222" };
int[] image = { R.drawable.tf1, R.drawable.tf7, R.drawable.ts26 };
int textlength = 0;
ArrayList<String> text_sort = new ArrayList<String>();
ArrayList<Integer> image_sort = new ArrayList<Integer>();
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setActionBarContentView(R.layout.m_listgeneral);
addActionBarItem(
getActionBar().newActionBarItem(NormalActionBarItem.class)
.setDrawable(R.drawable.ic_title_back),
R.id.action_bar_back);
edittext = (EditText) findViewById(R.id.EditText01);
listview = (ListView) findViewById(R.id.ListView01);
listview.setAdapter(new MyCustomAdapter(text, image));
listview.setClickable(true);
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if ("(Lockheed) F-104S Starfighter".equals(text[position])) {
// code specific to 2nd list item
Intent myIntent = new Intent(view.getContext(),
mcomingsoon.class);
startActivityForResult(myIntent, 0);
}
if ("Aermecchi / EMBRAER AMX".equals(text[position])) {
// code specific to 2nd list item
Intent myIntent = new Intent(view.getContext(),
mcomingsoon.class);
startActivityForResult(myIntent, 0);
}
if ("G-222".equals(text[position])) {
// code specific to 2nd list item
Intent myIntent = new Intent(view.getContext(),
fa_f4.class);
startActivityForResult(myIntent, 0);
}
}
});
edittext.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textlength = edittext.getText().length();
text_sort.clear();
image_sort.clear();
for (int i = 0; i < text.length; i++) {
if (textlength <= text[i].length()) {
if (text[i].indexOf(edittext.getText().toString()) != -1) {
text_sort.add(text[i]);
image_sort.add(image[i]);
}
}
}
listview.setAdapter(new MyCustomAdapter(text_sort, image_sort));
}
});
}
class MyCustomAdapter extends BaseAdapter {
String[] data_text;
int[] data_image;
MyCustomAdapter() {
}
MyCustomAdapter(String[] text, int[] image) {
data_text = text;
data_image = image;
}
MyCustomAdapter(ArrayList<String> text, ArrayList<Integer> image) {
data_text = new String[text.size()];
data_image = new int[image.size()];
for (int i = 0; i < text.size(); i++) {
data_text[i] = text.get(i);
data_image[i] = image.get(i);
}
}
public int getCount() {
return data_text.length;
}
public String getItem(int position) {
return null;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.listview, parent, false);
TextView textview = (TextView) row.findViewById(R.id.TextView01);
ImageView imageview = (ImageView) row
.findViewById(R.id.ImageView01);
textview.setText(data_text[position]);
imageview.setImageResource(data_image[position]);
return (row);
}
}
#Override
public boolean onHandleActionBarItemClick(ActionBarItem item, int position) {
switch (item.getItemId()) {
case R.id.action_bar_back:
startActivity(new Intent(this, mbymanufacturers.class));
break;
}
return true;
}
Hope I made myself clear,
Any suggestions?
Thank You
Tiberio Bozotti

You shouldn't query the String array directy, but get the item from the Adapter.
Try this...
Create a custom private class to hold the String and integer value pair, such as:
private class DataPair { String text; int value; }
Then, instead of using your MyCustomAdapter, create an ArrayList of DataPair objects and assign it to an ArrayAdapter, such as:
new ArrayAdapter<DataPair>(this, R.layout.ListView01, new ArrayList<DataPair>);
Now, for getting your data, you only have to do:
getItem(position).text
or
getItem(position).value
In case of the setOnItemClickListener use:
((DataPair)listview.getAdapter().getItem(position)).text

Related

Bug in subtracting the total amount when there are no items left in the "Cart"

I'm asking for help to analyze my algorithm and try to fix what am I missing or doing something wrong. At first when I just add items on the "Cart", its price adds up to the total amount. When I delete some of the items, it does do its subtraction without any problem. But when there is one item left in the "Cart", for example, the price costs $20, so the total amount is $20. When I delete the remaining item, my "Cart" is empty but the total amount is still $20.
Here is my Cart.class where I set the text in my total amount
package com.example.claude.afinal;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class Cart extends MainActivity {
TextView amount_total;
ListView cartList;
CartCustomAdapter cartCustomAdapter;
String name, price;
static ArrayList<Order> cartArray = new ArrayList<Order>();
static Double total_amount = 0.00d;
static Double temp = 0.00d;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
amount_total = (TextView) findViewById(R.id.total_tv);
Bundle bundle = getIntent().getExtras();
Button checkout = (Button) findViewById(R.id.check_out);
Button add_item = (Button) findViewById(R.id.add_item);
name = bundle.getString("i_name");
price = bundle.getString("i_price");
temp = Double.parseDouble(price);
total_amount = (total_amount + temp);
add_item.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Cart.this,MainActivity.class);
startActivity(intent);
}
});
cartList = (ListView) findViewById(R.id.cart_list);
cartCustomAdapter = new CartCustomAdapter(Cart.this,R.layout.list_cart,cartArray);
cartList.setItemsCanFocus(false);
cartList.setAdapter(cartCustomAdapter);
cartArray.add(new Order(name,price,"1"));
cartCustomAdapter.notifyDataSetChanged();
}
public void change_total(int y, Double result) {
if (y == 0) {
amount_total.setText(total_amount.toString());
} else {
total_amount = total_amount - result;
amount_total.setText(total_amount.toString());
}
}
And here is my CarCustomAdapter with listview for the items and with delete button
package com.example.claude.afinal;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class CartCustomAdapter extends ArrayAdapter<Order> {
Context context;
int layoutResourceId;
ArrayList<Order> items = new ArrayList<Order>();
Integer counter = 0;
Double x = 0.00d;
Boolean clicked = false;
Double y = 0.00d;
public CartCustomAdapter(Context context, int layoutResourceId,
ArrayList<Order> items) {
super(context, layoutResourceId, items);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.items = items;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
UserHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new UserHolder();
holder.cart_name = (TextView) row.findViewById(R.id.tv_cart_name);
holder.cart_qty = (TextView) row.findViewById(R.id.tv_cart_qty);
holder.cart_price = (TextView) row.findViewById(R.id.tv_cart_price);
holder.del = (Button) row.findViewById(R.id.del_item_button);
row.setTag(holder);
} else {
holder = (UserHolder) row.getTag();
}
final Order data = items.get(position);
holder.cart_name.setText(data.getName());
holder.cart_price.setText(data.getPrice());
holder.cart_qty.setText(data.getQty());
x = x + Double.parseDouble(data.getPrice());
holder.del.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
remove(getItem(position));
y = Double.parseDouble(data.getPrice());
clicked = true;
}
});
if(clicked){
((Cart)getContext()).change_total(1,y);
}
else
{
((Cart)getContext()).change_total(0,x);
}
return row;
}
static class UserHolder {
TextView cart_name;
TextView cart_qty;
TextView cart_price;
Button del;
}
}
I can see this code is executed at the very end of the getView method
if(clicked){
((Cart)getContext()).change_total(1,y);
}
else
{
((Cart)getContext()).change_total(0,x);
}
When the listview has no items, the getView method may not be getting called, so you should try simply moving the ((Cart)getContext()).change_total(1,y); line to your delete button click listener, like this:
remove(getItem(position));
y = Double.parseDouble(data.getPrice());
clicked = true;
((Cart)getContext()).change_total(1,y);
That's my guess.
In your function change_total, why don't you simply do this:
if (y == 0) {
total_amount = 0.0;
amount_total.setText("0");
}
So, if there are no items, simply set the cart total to 0

How can I assign a SoundPool Object to an element created by Gridview in Java?

I'm working on an Android App that displays 151 pictures via Gridview. I have 151 sounds and I want to assign every sound to a single image, so I can play that sound tapping on an Image.
I'm using SoundPool. Ideas about how can I do this thing?
package com.example.thefe.newsmartkedex;
import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import static com.example.thefe.newsmartkedex.R.raw.pkmn1;
public class MainActivity extends AppCompatActivity {
int pkmn1, pkmn2, pkmn3;
SoundPool mySoundPool = new SoundPool (1, AudioManager.STREAM_MUSIC, 0);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pkmn1 = mySoundPool.load(this, R.raw.pkmn1, 1);
pkmn2 = mySoundPool.load(this, R.raw.pkmn2, 1);
pkmn3 = mySoundPool.load(this, R.raw.pkmn3, 1);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
position += 1; //forget about this
Toast.makeText(MainActivity.this, "" + position,
Toast.LENGTH_SHORT).show();
mySoundPool.play(/*???*/,1,1,1,0,1);
}
});
};
}
My ImageAdapter Class works like this:
package com.example.thefe.newsmartkedex;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import java.util.ArrayList;
/**
* Created by TheFe on 27/09/2016.
*/
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c, ArrayList list) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.pkmn1, R.drawable.pkmn2,
R.drawable.pkmn3, R.drawable.pkmn4,
R.drawable.pkmn5, R.drawable.pkmn6,
R.drawable.pkmn7, R.drawable.pkmn8,
R.drawable.pkmn9, R.drawable.pkmn10,
R.drawable.pkmn11, R.drawable.pkmn12,
R.drawable.pkmn13, R.drawable.pkmn14,
R.drawable.pkmn15, R.drawable.pkmn16,
R.drawable.pkmn17, R.drawable.pkmn18,
R.drawable.pkmn19, R.drawable.pkmn20,
R.drawable.pkmn21, R.drawable.pkmn22,
R.drawable.pkmn23, R.drawable.pkmn24,
R.drawable.pkmn25, R.drawable.pkmn26,
R.drawable.pkmn27, R.drawable.pkmn28,
R.drawable.pkmn29, R.drawable.pkmn30,
R.drawable.pkmn31, R.drawable.pkmn32,
R.drawable.pkmn33, R.drawable.pkmn34,
R.drawable.pkmn35, R.drawable.pkmn36,
R.drawable.pkmn37, R.drawable.pkmn38,
R.drawable.pkmn39, R.drawable.pkmn40,
R.drawable.pkmn41, R.drawable.pkmn42,
R.drawable.pkmn43, R.drawable.pkmn44,
R.drawable.pkmn45, R.drawable.pkmn46,
R.drawable.pkmn47, R.drawable.pkmn48,
R.drawable.pkmn49, R.drawable.pkmn50,
R.drawable.pkmn51, R.drawable.pkmn52,
R.drawable.pkmn53, R.drawable.pkmn54,
R.drawable.pkmn55, R.drawable.pkmn56,
R.drawable.pkmn57, R.drawable.pkmn58,
R.drawable.pkmn59, R.drawable.pkmn60,
R.drawable.pkmn61, R.drawable.pkmn62,
R.drawable.pkmn63, R.drawable.pkmn64,
R.drawable.pkmn65, R.drawable.pkmn66,
R.drawable.pkmn67, R.drawable.pkmn68,
R.drawable.pkmn69, R.drawable.pkmn70,
R.drawable.pkmn71, R.drawable.pkmn72,
R.drawable.pkmn73, R.drawable.pkmn74,
R.drawable.pkmn75, R.drawable.pkmn76,
R.drawable.pkmn77, R.drawable.pkmn78,
R.drawable.pkmn79, R.drawable.pkmn80,
R.drawable.pkmn81, R.drawable.pkmn82,
R.drawable.pkmn83, R.drawable.pkmn84,
R.drawable.pkmn85, R.drawable.pkmn86,
R.drawable.pkmn87, R.drawable.pkmn88,
R.drawable.pkmn89, R.drawable.pkmn90,
R.drawable.pkmn91, R.drawable.pkmn92,
R.drawable.pkmn93, R.drawable.pkmn94,
R.drawable.pkmn95, R.drawable.pkmn96,
R.drawable.pkmn97, R.drawable.pkmn98,
R.drawable.pkmn99, R.drawable.pkmn100,
R.drawable.pkmn101, R.drawable.pkmn102,
R.drawable.pkmn103, R.drawable.pkmn104,
R.drawable.pkmn105, R.drawable.pkmn106,
R.drawable.pkmn107, R.drawable.pkmn108,
R.drawable.pkmn109, R.drawable.pkmn110,
R.drawable.pkmn111, R.drawable.pkmn112,
R.drawable.pkmn113, R.drawable.pkmn114,
R.drawable.pkmn115, R.drawable.pkmn116,
R.drawable.pkmn117, R.drawable.pkmn118,
R.drawable.pkmn119, R.drawable.pkmn120,
R.drawable.pkmn121, R.drawable.pkmn122,
R.drawable.pkmn123, R.drawable.pkmn124,
R.drawable.pkmn125, R.drawable.pkmn126,
R.drawable.pkmn127, R.drawable.pkmn128,
R.drawable.pkmn129, R.drawable.pkmn130,
R.drawable.pkmn131, R.drawable.pkmn132,
R.drawable.pkmn133, R.drawable.pkmn134,
R.drawable.pkmn135, R.drawable.pkmn136,
R.drawable.pkmn137, R.drawable.pkmn138,
R.drawable.pkmn139, R.drawable.pkmn140,
R.drawable.pkmn141, R.drawable.pkmn142,
R.drawable.pkmn143, R.drawable.pkmn144,
R.drawable.pkmn145, R.drawable.pkmn146,
R.drawable.pkmn147, R.drawable.pkmn148,
R.drawable.pkmn149, R.drawable.pkmn150,
R.drawable.pkmn151
};
}
Create a model class that holds a reference to your image and the sound associated with it. When an item is clicked, get the object corresponding to the object, load the SoundPool object using the reference and play it.
public class Item {
int imageId;
int soundID;
public Item(int imageId, int soundID){
this.imageId = imageId;
this.soundID = soundID;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public int getSoundID() {
return soundID;
}
public void setSoundID(int soundID) {
this.soundID = soundID;
}
}
Create a List of Items,
List<Item> items = new ArrayList<Items>();
for(int i = 0; i < 10; i++) {
items.add(new Item(R.drawable.your_image_id, R.raw.your_sound_id));
}
Pass this list to your adapter. You will to modify the constructor of ImageAdapter. Initialize your adapter like this,
GridView gridview = (GridView) findViewById(R.id.gridview);
ImageAdapter adapter = new ImageAdapter(this, items);
gridview.setAdapter(adapter);
Inside you onItemClick,
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// your code
Item selectedItem = items.get(position);
int soundID = selectedItem.getSoundID();
pkmn1 = mySoundPool.load(MainActivity.this, soundID, 1);
mySoundPool.play(pkmn1, 1, 1, 1, 0, 1);
}
});

When I search listview item and then click desire item, it does open first position of List View

I have Searchable country ListView. When I click Country List Item without search, it's works properly. but when I type Brazil in search box and click Brazil, then open Country_Details.Java Activity and pass Afganistan details.
when I type Brazil in Search box and click Brazil, I want ot pass Brazil details in WebView.
Full Source Code : https://drive.google.com/open?id=0B46bPR7LKLjpZFRhcV9DdmIweTQ
Here My Code.
package com.nasir.search;
import java.util.ArrayList;
import java.util.Arrays;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class Search_Country extends ListActivity {
private EditText SearchText;
private ListView ListText;
private String[] Number_List = { "Afghanistan", "Albania", "Algeria", "Brazil"};
private ArrayList<String> array_sort;
int textlength = 0;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.search_country);
SearchText = (EditText) findViewById(R.id.listview_search);
ListText = (ListView) findViewById(android.R.id.list);
array_sort = new ArrayList<String>(Arrays.asList(Number_List));
setListAdapter(new bsAdapter(this));
SearchText.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
}
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
textlength = SearchText.getText().length();
array_sort.clear();
for (int i = 0; i < Number_List.length; i++)
{
if (textlength <= Number_List[i].length())
{
if(Number_List[i].toUpperCase().contains(SearchText.getText().toString().toUpperCase().trim()))
{
array_sort.add(Number_List[i]);
}
}
}
AppendList(array_sort);
}
});
ListText.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch( position )
{
case 0: Intent intent = new Intent(Search_Country.this, Country_Details.class);
intent.putExtra("header", getString(R.string.html_afganistan));
startActivity(intent);
break;
case 1: Intent intent1 = new Intent(Search_Country.this, Country_Details.class);
intent1.putExtra("header", getString(R.string.html_albenia));
startActivity(intent1);
break;
case 2: Intent intent2 = new Intent(Search_Country.this, Country_Details.class);
intent2.putExtra("header", getString(R.string.html_algeria));
startActivity(intent2);
break;
case 3: Intent intent3 = new Intent(Search_Country.this, Country_Details.class);
intent3.putExtra("header", getString(R.string.html_brazil));
startActivity(intent3);
break;
}
}
});
}
public void AppendList(ArrayList<String> str)
{
setListAdapter(new bsAdapter(this));
}
public class bsAdapter extends BaseAdapter
{
Activity cntx;
public bsAdapter(Activity context)
{
this.cntx = context;
}
public int getCount()
{
return array_sort.size();
}
public Object getItem(int position)
{
return array_sort.get(position);
}
public long getItemId(int position)
{
return array_sort.size();
}
public View getView(final int position, View convertView, ViewGroup parent)
{
View row = null;
LayoutInflater inflater = cntx.getLayoutInflater();
row = inflater.inflate(R.layout.search_country_listview, null);
TextView tv = (TextView) row.findViewById(R.id.listview_seacrh_text);
tv.setText(array_sort.get(position));
return row;
}
}
}
Problem is here, you are call webview on static position, instead you call accordingly your array list
Like below.
if( array_list.get(position).equals("Afghanistan")){
Intent intent = new Intent(Search_Country.this, Country_Details.class); intent.putExtra("header", getString(R.string.html_afganistan)); startActivity(intent);
}

How to add a listview item to a whole other listview

So in my android app, I have two scrollable tabs which each contain Listview using a Fragment. One a list of apps and the other is blank. What I am aiming to do is add a plus button in place of where my checkbox is and duplicate that item in the other listview which is blank.
I have done research on this, but I have not found any successful examples on how to implement this.
Android - Add an item from one ListView to another ListView?
Here is my fragment that returns the apps
package com.spicycurryman.getdisciplined10.app;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageItemInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import com.ibc.android.demo.appslist.app.ApkAdapter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class InstalledAppActivity extends Fragment
implements OnItemClickListener {
PackageManager packageManager;
ListView apkList;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
View rootView = inflater.inflate(R.layout.user_installed, container, false);
packageManager = getActivity().getPackageManager();
/*To filter out System apps*/
apkList = (ListView) rootView.findViewById(R.id.applist);
new LoadApplications(getActivity().getApplicationContext()).execute();
return rootView;
}
/**
* Return whether the given PackageInfo represents a system package or not.
* User-installed packages (Market or otherwise) should not be denoted as
* system packages.
*
* #param pkgInfo
* #return boolean
*/
private boolean isSystemPackage(PackageInfo pkgInfo) {
return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) ? true
: false;
}
private boolean isSystemPackage1(PackageInfo pkgInfo) {
return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) ? false
: true;
}
// Don't need in Fragment
/*#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.block, menu);
// super.onCreateOptionsMenu(menu,inflater);
}*/
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
private class LoadApplications extends AsyncTask<Void, Void, Void> {
Context mContext;
private ProgressDialog pDialog;
List<PackageInfo> packageList1 = new ArrayList<PackageInfo>();
public LoadApplications(Context context){
Context mContext = context;
}
#Override
protected Void doInBackground(Void... params) {
List<PackageInfo> packageList = packageManager
.getInstalledPackages(PackageManager.GET_PERMISSIONS);
/* List<ApplicationInfo> list = mContext.getPackageManager().getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
for(int n = 0;n<list.size();n++){
if ((list.get(n).flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP))
}*/
for(PackageInfo pi : packageList) {
boolean b = isSystemPackage(pi);
boolean c = isSystemPackage1(pi);
if(!b || !c ) {
packageList1.add(pi);
}
}
//sort by application name
final PackageItemInfo.DisplayNameComparator comparator = new PackageItemInfo.DisplayNameComparator(packageManager);
Collections.sort(packageList1, new Comparator<PackageInfo>() {
#Override
public int compare(PackageInfo lhs, PackageInfo rhs) {
return comparator.compare(lhs.applicationInfo, rhs.applicationInfo);
}
});
return null;
}
#Override
protected void onCancelled() {
super.onCancelled();
}
#Override
protected void onPreExecute() {
pDialog = new ProgressDialog(InstalledAppActivity.this.getActivity());
pDialog.setMessage("Loading your apps...");
pDialog.show();
}
#Override
protected void onPostExecute(Void result) {
apkList.setAdapter(new ApkAdapter(getActivity(), packageList1, packageManager));
if (pDialog.isShowing()){
pDialog.dismiss();
}
super.onPostExecute(result);
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
}
And here is my adapter class:
package com.ibc.android.demo.appslist.app;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import com.spicycurryman.getdisciplined10.app.R;
import java.util.List;
public class ApkAdapter extends BaseAdapter {
List<PackageInfo> packageList;
Activity context;
PackageManager packageManager;
boolean[] itemChecked;
public ApkAdapter(Activity context, List<PackageInfo> packageList,
PackageManager packageManager) {
super();
this.context = context;
this.packageList = packageList;
this.packageManager = packageManager;
itemChecked = new boolean[packageList.size()];
}
private class ViewHolder {
TextView apkName;
CheckBox ck1;
}
public int getCount() {
return packageList.size();
}
public Object getItem(int position) {
return packageList.get(position);
}
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.installed_apps, null);
holder = new ViewHolder();
holder.apkName = (TextView) convertView
.findViewById(R.id.appname);
holder.ck1 = (CheckBox) convertView
.findViewById(R.id.checkBox1);
convertView.setTag(holder);
//holder.ck1.setTag(packageList.get(position));
} else {
holder = (ViewHolder) convertView.getTag();
}
// ViewHolder holder = (ViewHolder) convertView.getTag();
PackageInfo packageInfo = (PackageInfo) getItem(position);
Drawable appIcon = packageManager
.getApplicationIcon(packageInfo.applicationInfo);
String appName = packageManager.getApplicationLabel(
packageInfo.applicationInfo).toString();
appIcon.setBounds(0, 0, 75, 75);
holder.apkName.setCompoundDrawables(appIcon, null, null, null);
holder.apkName.setCompoundDrawablePadding(15);
holder.apkName.setText(appName);
holder.ck1.setChecked(false);
if (itemChecked[position])
holder.ck1.setChecked(true);
else
holder.ck1.setChecked(false);
holder.ck1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (holder.ck1.isChecked())
itemChecked[position] = true;
else
itemChecked[position] = false;
}
});
return convertView;
}
}
How would I go about achieving this?
So what you are doing is, you are populating a listview of installed apps using package manager. Add a plus button in place of the checkbox. Now get installed apps similar to this -
List<PackageInfo> packageList1 = packageManager.getInstalledPackages(0);
final PackageItemInfo.DisplayNameComparator comparator = new PackageItemInfo.DisplayNameComparator(packageManager);
Collections.sort(packageList1, new Comparator<PackageInfo>() {
#Override
public int compare(PackageInfo lhs, PackageInfo rhs) {
return comparator.compare(lhs.applicationInfo, rhs.applicationInfo);
}
});
for (int i = 0; i < packageList1.size(); i++) {
PackageInfo PackInfo = packageList1.get(i);
if (((PackInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) != true) {
//Add to adapter
}
}
}
Now, create a public string array in the activity which is holding the tabs. When you click on the plus button add the packagename to the array.
Now on the other tab use the same adapter, but here before adding it to the adapter check if it is found in the string array using the index. Something like -
List<PackageInfo> packageList1 = packageManager.getInstalledPackages(0);
final PackageItemInfo.DisplayNameComparator comparator = new PackageItemInfo.DisplayNameComparator(packageManager);
Collections.sort(packageList1, new Comparator<PackageInfo>() {
#Override
public int compare(PackageInfo lhs, PackageInfo rhs) {
return comparator.compare(lhs.applicationInfo, rhs.applicationInfo);
}
});
for (int i = 0; i < packageList1.size(); i++) {
PackageInfo PackInfo = packageList1.get(i);
if (((PackInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) != true) {
if (Mainactivity.array contains String PackageName= PackInfo.packageName)
{
//Add to adapter
}
}
}
}
To persist the selected apps, add their package name to shared_preferences.
edit :
In your fragment's LoadApplications class, you retrieve a list of installed apps. On the second fragment use the same code, but just add one more condition
for(PackageInfo pi : packageList) {
boolean b = isSystemPackage(pi);
boolean c = isSystemPackage1(pi);
if(!b || !c ) {
if (array contains packagename){
packageList1.add(pi);
}
}
List<UserApps> packageListBlocked = new ArrayList<UserApps>();
holder.ck1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (holder.ck1.isChecked())
itemChecked[position] = true;
packageListBlocked.add(packageList.get(position));
else
itemChecked[position] = false;
}
});
ArrayList can hold duplicate data, so be cautious while adding.
Now you have all checkedItems in packageListBlocked pass it on to next tab and set data, make sure you call adapter.notifyDataSetChanged();

SetOnItem Click Listener event is not getting effected?

this is my code to display listview in customized format .but my requirement is when ever i clicked on any of the item of listview its further details need to be displayed but when am clicking on it ....its not at all getting effected i mean no setonitemclicklictener event is performed ....
i request please have a look on my code which is given above ....thanks to all
package com.hands;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Passengers extends Activity implements OnClickListener{
ListView lview3;
Button btn;
int textlength = 0;
ArrayList<String> text_sort = new ArrayList<String>();
ArrayList<Integer> image_sort = new ArrayList<Integer>();
ArrayList<String> text_sort2 = new ArrayList<String>();
EditText edittext;
private static String month[] = {"kareem","Saleem","Imran","Anwar","Shahid",
"Raheem","Afzal","Nazeer","Ahmed"};
private static String desc[] = {"ssagi123,Indian","ssagi1234,Indian","ssagi1235,Indian",
"ssagi1236,Indian","ssagi1237,Indian","ssagi1238,Indian","ssagi1239,Indian",
"ssagi12310,Indian","ssagi123411,Indian","Month - 10"};
int[] image = { R.drawable.user2, R.drawable.user2, R.drawable.user2,
R.drawable.user2, R.drawable.user2, R.drawable.user2, R.drawable.user2,
R.drawable.user2, R.drawable.user2, R.drawable.user2 };
/* private static String bstatus[] = {"no status","no status","no status","no status","no status",
"no status","no status","no status","no status"};
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.passengers);
System.out.println("****1");
btn=(Button)findViewById(R.id.psnbutt1);
btn.setOnClickListener(this);
edittext = (EditText) findViewById(R.id.search_mycontact);
lview3 = (ListView) findViewById(R.id.listView3);
System.out.println("****2");
lview3.setAdapter(new ListViewCustomAdapter(image, month, desc));
edittext.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after)
{
}
public void onTextChanged(CharSequence s, int start,
int before, int count)
{
textlength = edittext.getText().length();
text_sort.clear();
image_sort.clear();
text_sort2.clear();
for (int i = 0; i < month.length; i++)
{
if (textlength <= month[i].length())
{
if (edittext.getText().toString().
equalsIgnoreCase((String) month[i].subSequence(0, textlength)))
{
text_sort.add(month[i]);
image_sort.add(image[i]);
text_sort2.add(desc[i]);
}
}
}
System.out.println("****3");
lview3.setAdapter(new ListViewCustomAdapter(image_sort, text_sort, text_sort2));
}
});
System.out.println("****4");
lview3.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println("****5");
// Object listItem = lview3.getItemAtPosition(position);
//String keyword=listItem.toString();
// Toast.makeText(getApplicationContext(), "you are selected"+keyword, Toast.LENGTH_LONG).show();
}
});
}
class ListViewCustomAdapter extends BaseAdapter
{
String[] title;
String[] description;
int[] number;
Activity context;
LayoutInflater inflater;
ListViewCustomAdapter()
{
}
ListViewCustomAdapter(int[] image, String[] month, String[] desc)
{
title = month;
description=desc;
number=image;
}
ListViewCustomAdapter(ArrayList<Integer> image,ArrayList<String> month, ArrayList<String> desc)
{
title = new String[month.size()];
description=new String[desc.size()];
number = new int[image.size()];
for(int i=0;i<month.size();i++)
{
title[i] = month.get(i);
description[i]=desc.get(i);
number[i] = image.get(i);
}
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return title.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row=convertView;
row = inflater.inflate(R.layout.listitem_row2, parent, false);
TextView textview = (TextView) row.findViewById(R.id.txtViewTitle);
TextView textview1 = (TextView) row.findViewById(R.id.txtViewDescription);
System.out.println("before list row statement");
//final ListView lview3=(ListView) row.findViewById(R.id.listView3);
// final ListView lview3=(ListView)findViewById(R.id.listView3);
System.out.println("after list row statement");
ImageView imageview = (ImageView) row.findViewById(R.id.imgViewLogo);
textview.setText(title[position]);
textview1.setText(description[position]);
imageview.setImageResource(number[position]);
/*lview3.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
Object listItem = lview3.getItemAtPosition(position);
String keyword=listItem.toString();
Toast.makeText(getApplicationContext(), "you are selected"+keyword, Toast.LENGTH_LONG).show();
}
});
*/
return (row);
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.psnbutt1:
Intent i = new Intent(this, NewhapploginActivity.class);
startActivity(i);
break;
}
}
}
You need to add
android:focusable="false"
android:focusableInTouchMode="false"
to all the View's of your row.xml.Because in Custom ListView as the View over it are having the focus so ListView is not able to get its focus working.

Categories