Android : Adding one listview item to another listview (Both are fragments)

on Wednesday, July 30, 2014


I have two fragments one with installed apps and one that is blank. Both are listviews. I have a plus sign next to each item in the installed app listview. I am trying to make it so that when the user clicks the plus button. The listview app item goes to the blank fragment.


enter image description here


enter image description here


What I am trying to do is use create a public string array in the activity which is holding the tabs and when I click on the plus button add the packagename to the array.


And then in the blank fragment I am trying to check if it is found in the string array using the index.


In my BlockActivity class with holds the actionbar tabs I made this:



public static List<String> blacklist = new ArrayList<String>();


And here is my blank fragment condition to retrieve the app:



if(!b || !c ) {

if (BlockActivity.blacklist.contains(pi.packageName))
{
packageList1.add(pi);
}


}


Now I think I have to you will have to reference to the position of the item the button was clicked from listview. Doing this, I will probably have to reference a method from my adapter class, but I am not sure how to implement that and the method.


I am trying to follow something similar to this:


http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=92


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.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
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;
ImageButton 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 = (ImageButton) convertView
.findViewById(R.id.plus1);

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);

// I guess the onClickListener would go here??!?!? Not sure how to do it in this case and reference it to the Fragment??






return convertView;

}




}


And here is my blank fragment:



package com.spicycurryman.getdisciplined10.app;

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 java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CustomList_Activity 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;
}



@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

}


private class LoadApplications extends AsyncTask<Void, Void, Void> {


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);





for(PackageInfo pi : packageList) {
boolean b = isSystemPackage(pi);
boolean c = isSystemPackage1(pi);

if(!b || !c ) {

if (BlockActivity.blacklist.contains(pi.packageName))
{
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 onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}

}

0 comments:

Post a Comment