Hi, this is actually 2 questions.
I have a list of items stored in one string array and a list of the collections those items are in stored in a second string array.
I want the user to be able to search for an item and see in which collection it exists.
I have managed to do this in a less-than-elegant way by simply combining the 2 string arrays into one and using a ListView with a EditText with a TextWatcher to filter the results. This all works but the result is not so eye-pleasing. I use this to make the distinction between item and collections:
Code:
<item>ITEM \r\n -> COLLECTION(S)
when defining the string array.
As I said, it works but I would like the COLLECTION(S) to be formatted differently from the ITEM. Is this possible in a ListView?
This is my current code:
Code:
public class Search extends Activity {
/** Called when the activity is first created. */
private ListView lv1;
private EditText ed;
private String[] lv_arr;
private ArrayList<String> arr_sort= new ArrayList<String>();
int textlength=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
lv1=(ListView)findViewById(R.id.listView1);
ed=(EditText)findViewById(R.id.editText1);
lv_arr = getResources().getStringArray(R.array.all_cont);
lv1.setAdapter(new ArrayAdapter<String>(this, R.layout.row , lv_arr));
ed.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=ed.getText().length();
arr_sort.clear();
for(int i=0;i<lv_arr.length;i++)
{
if(textlength<=lv_arr[i].length())
{
if(ed.getText().toString().equalsIgnoreCase((String) lv_arr[i].subSequence(0, textlength)))
{
arr_sort.add(lv_arr[i]);
}}}
lv1.setAdapter(new ArrayAdapter<String>(Search.this, R.layout.row , arr_sort));
}
});
}}
As a second solution that looks more elegant, I used a SimpleAdapter to put the 2 original string arrays in the ListView like this:
Code:
public class Search extends ListActivity {
private String[] l1;
private String[] l2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seach);
ArrayList<Map<String, String>> list = buildData();
String[] from = { "name", "packs" };
int[] to = { android.R.id.text1, android.R.id.text2 };
SimpleAdapter adapter = new SimpleAdapter(this, list, android.R.layout.simple_list_item_2, from, to);
setListAdapter(adapter);}
private ArrayList<Map<String, String>> buildData() {
l1 = getResources().getStringArray(R.array.items);
l2 = getResources().getStringArray(R.array.packs);
Integer i;
int to = l1.length;
ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
for(i=0;i < to;i++){
list.add(putData(l1[i], l2[i]));
}
return list;
}
private HashMap<String, String> putData(String name, String packs) {
HashMap<String, String> item = new HashMap<String, String>();
item.put("name", name);
item.put("packs", packs);
return item;
}
}
This looks a lot better but I can't figure out how to make use of the EditText to filter the results.
Any help is welcomed!
Hey!
I read your post and I think it would be better to make a class like this:
Code:
public class ListItem
{
public String itemName;
public String collectionName;
}
And then in your Search activity you can Make a single ArrayList of the type ListItem. Populate this array in a way similar to how you are populating the ArrayList of the HashMaps.
Now for setting the ListAdapter you'll have to make a custom view for each row of the list(this custom row could contain two text views, one for the item name and the other for the collection and you can give them their own formatting), and then subclass the ArrayAdapter class and override its getView method.
Heres a couple of links that might be helpful:
http://www.ezzylearning.com/tutoria...droid-listview-items-with-custom-arrayadapter
http://stackoverflow.com/questions/2265661/how-to-use-arrayadaptermyclass
Hope this helps
The_R said:
Hey!
I read your post and I think it would be better to make a class like this:
Code:
public class ListItem
{
public String itemName;
public String collectionName;
}
And then in your Search activity you can Make a single ArrayList of the type ListItem. Populate this array in a way similar to how you are populating the ArrayList of the HashMaps.
Now for setting the ListAdapter you'll have to make a custom view for each row of the list(this custom row could contain two text views, one for the item name and the other for the collection and you can give them their own formatting), and then subclass the ArrayAdapter class and override its getView method.
Heres a couple of links that might be helpful:
http://www.ezzylearning.com/tutoria...droid-listview-items-with-custom-arrayadapter
http://stackoverflow.com/questions/2265661/how-to-use-arrayadaptermyclass
Hope this helps
Click to expand...
Click to collapse
Thanks once more. I looked over the links and I think I have an idea of how to adapt it to my app. Will try it tomorrow and let you know
Yeah. Let me know if it worked.
The_R said:
Yeah. Let me know if it worked.
Click to expand...
Click to collapse
I feel like my head is exploding.
I did what you said but I still have some issues. Here's what I did:
1. Created a new class, Icons:
Code:
public class Icons {
public String icon;
public String title;
public Icons(){
super();
}
public Icons(String icon, String title) {
super();
this.icon = icon;
this.title = title;
}
}
Created a new XML for the style of the ListView:
Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/icon_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:textSize="18sp"
android:textColor="#ffffff"
android:textStyle="bold" />
<TextView
android:id="@+id/in_pack"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:textSize="12sp" />
</LinearLayout>
created a new Adapter:
Code:
public class IconsAdapter extends ArrayAdapter<Icons>{
Context context;
int layoutResourceId;
Icons data[] = null;
public IconsAdapter(Context context, int layoutResourceId, Icons[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
IconsHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new IconsHolder();
holder.txtIcon = (TextView)row.findViewById(R.id.icon_name);
holder.txtTitle = (TextView)row.findViewById(R.id.in_pack);
row.setTag(holder);
}
else
{
holder = (IconsHolder)row.getTag();
}
Icons icons = data[position];
holder.txtTitle.setText(icons.title);
holder.txtIcon.setText(icons.icon);
return row;
}
static class IconsHolder
{
TextView txtIcon;
TextView txtTitle;
}
}
Modified my Search class like this:
Code:
public class Search extends Activity {
private ListView listView1;
private String[] l1;
private String[] l2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seach);
l1 = getResources().getStringArray(R.array.items);
l2 = getResources().getStringArray(R.array.packs);
Integer i;
int to = l1.length;
// for(i=0;i < to;i++){}
Icons Icons_data[] = new Icons[]
{
new Icons(l1[0], l2[0]),
new Icons(l1[1], l2[1]),
new Icons(l1[2], l2[2]),
new Icons(l1[3], l2[3]),
};
IconsAdapter adapter = new IconsAdapter(this,
R.layout.row, Icons_data);
listView1 = (ListView)findViewById(R.id.listView1);
View header = (View)getLayoutInflater().inflate(R.layout.row, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
}
}
Up until now, everything works (almost) great. Only thing I can't do is figure out how to assign the items in the Icons_data[] array automatically (my for(...) statement doesn't seem to want to fit anywhere). Format looks good and manually inserting data does what it's supposed to. Still need to figure out the automatic data inserting thing...my arrays have 100-150 elements
What I also can't figure out is how the hell to perform the filtering/search on this new special Array... I tried using the old method with the TextWatcher on an EditText field but can't seem to be able to adapt this part:
Code:
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textlength=ed.getText().length();
[B][U]arr_sort.clear();[/U][/B]
for(int i=0;i<to;i++)
{
if(textlength<=l1[i].length())
{
if(ed.getText().toString().equalsIgnoreCase((String) l1[i].subSequence(0, textlength)))
{
[B][U]arr_sort.add(l1[i]);[/U][/B]
}
}
}
[B][U]lv1.setAdapter(new ArrayAdapter<String>(Search.this, R.layout.row , arr_sort))[/U][/B];
}
});
ed is the EditText item. I guess I would need to make arr_sort of the type Icons[] and then change the Bold, Underlined lines to something...but no idea what... Is it even possible to do it like i'm doing it? Or should I look for another method to sort it?
Hey I modified your search class:
Code:
public class Search extends Activity {
private ListView listView1;
// Note: I've removed the two String[] class members cause we are going
// store this data in a single Icons[] member
private Icons[] iconsData;
private ArrayList<Icons> arr_sort; // Note: Changed the type of arr_sort
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seach);
// We'll create two local String[] variables to assemble the data
String[] l1 = getResources().getStringArray(R.array.items);
String[] l2 = getResources().getStringArray(R.array.packs);
// get the total number of icons
int totalIcons = l1.length;
// Allocate the data for the Icon[] array
iconsData = new Icons[totalIcons];
// Now to populate the Icon array
for (int i = 0; i < totalIcons; i++)
{
iconsData[i] = new Icons(l1[i], l2[i]);
}
// Rest remains the same
IconsAdapter adapter = new IconsAdapter(this,
R.layout.row, iconsData);
listView1 = (ListView)findViewById(R.id.listView1);
View header = (View)getLayoutInflater().inflate(R.layout.row, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
}
}
Now you don't need to change the sorting method. Just slight modifications to fit the data structuring is all that is needed.
Code:
public void onTextChanged(CharSequence s, int start, int before, int count)
{
textlength = ed.getText().length();
arr_sort.clear();
for(int i = 0 ; i < to; i++)
{
if(textlength <= iconsData[i].icon.length()) //Note: l1 becomes iconsData[i].icon
{
if(ed.getText().toString().equalsIgnoreCase((String) iconsData[i].icon.subSequence(0, textlength)))
{
arr_sort.add(iconsData[i]); // Note: we'll store iconsData[i] if a match is found
}
}
}
lv1.setAdapter(new IconsAdapter(Search.this, R.layout.row , arr_sort.toArray()));
}
Haven't tested it. So watch out for some possible errors.
I can't thank you enough but I still need your help.
The first part works (modifications to the Search class).
Now, In the same class, after that part, I add the filtering part:
Code:
ed=(EditText)findViewById(R.id.editText1);
ed.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) {
int textlength = ed.getText().length();
arr_sort.clear();
for(int i=0;i<[B][I]totalIcons[/I][/B];i++)
{
if(textlength <= iconsData[i].icon.length()) //Note: l1 becomes iconsData[i].icon
{
if(ed.getText().toString().equalsIgnoreCase((String) iconsData[i].icon.subSequence(0, textlength)))
{
arr_sort.add(iconsData[i]); // Note: we'll store iconsData[i] if a match is found
}
}
}
listView1.setAdapter(new IconsAdapter(Search.this, R.layout.row , [U][B](Icons[])[/B][/U] arr_sort.toArray()));
}
});
}
}
I had to make 2 changes in order for it not to give any errors. First, I changed the "to" in the for statement to "totalIcons" since that's actually the number we need and "to" was not defined. When I did this I also had to change "totalIcons" to final int since I had this error:"Cannot refer to a non-final variable totalIcons inside an inner class defined in a different method"
Also, I had to add the (Icons[]) at the end because of this error: "The constructor IconsAdapter(Search, int, Object[]) is undefined". The suggested fixes was changing the constructor for IconsAdapter, adding a new constructor or adding the (Icons[]) thing.
Now I have no errors in Eclipse but when I run the app and try to type something in the EditText box the app crashes...I get these errors:
04-12 19:32:27.032: E/AndroidRuntime(998): FATAL EXCEPTION: main
04-12 19:32:27.032: E/AndroidRuntime(998): java.lang.NullPointerException
04-12 19:32:27.032: E/AndroidRuntime(998): at mmarin.iconpack.manager.Search$1.onTextChanged(Search.java:70)
04-12 19:32:27.032: E/AndroidRuntime(998): at android.widget.TextView.sendOnTextChanged(TextView.java:6295)
04-12 19:32:27.032: E/AndroidRuntime(998): at android.widget.TextView.handleTextChanged(TextView.java:6336)
04-12 19:32:27.032: E/AndroidRuntime(998): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:6485)
04-12 19:32:27.032: E/AndroidRuntime(998): at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:889)
Sorry about the previous untested code. I was in a rush to go somewhere but I saw you online and thought that it'd be better if I replied.
Anyways, I think the problem is that "totalIcons" is a local variable. So remove the final keyword. And in the for loop in the TextWatcher's onTextChanged method instead of using totalIcons use the length property of iconsData:
Code:
for (int i = 0; i < iconsData.length; i++)
Should fix the runtime error
First off, please do all the things you have to do and don't waste your time with me. I really really appreciate you trying to help me so if you don't have time for this, it's absolutely no problem.
Now, the runtime errors are still there after the change
04-12 19:59:24.332: E/AndroidRuntime(1034): FATAL EXCEPTION: main
04-12 19:59:24.332: E/AndroidRuntime(1034): java.lang.NullPointerException
04-12 19:59:24.332: E/AndroidRuntime(1034): at mmarin.iconpack.manager.Search$1.onTextChanged(Search.java:70)
04-12 19:59:24.332: E/AndroidRuntime(1034): at android.widget.TextView.sendOnTextChanged(TextView.java:6295)
04-12 19:59:24.332: E/AndroidRuntime(1034): at android.widget.TextView.handleTextChanged(TextView.java:6336)
04-12 19:59:24.332: E/AndroidRuntime(1034): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:6485)
04-12 19:59:24.332: E/AndroidRuntime(1034): at mmarin.iconpack.manager.Search$1.onTextChanged(Sea rch.java:70)
Can you paste line 70 of Search.java?
Wait are you doing this in onCreate?
Code:
arr_sort = new ArrayList<Icons>();
That's what I was looking for (actually how to enable line numbers in Eclipse )
here it is:
Code:
arr_sort.clear();
Yep. We aren't creating arr_sort. So its a null pointer.
Do this somewhere in onCreate
Code:
arr_sort = new ArrayList<Icons>();
ok, that solved that issue. now the problem is with this:
Code:
listView1.setAdapter(new IconsAdapter(Search.this, R.layout.row, (Icons[]) arr_sort.toArray()));
What error/exception do you get?
04-12 20:17:26.663: E/AndroidRuntime(1205): FATAL EXCEPTION: main
04-12 20:17:26.663: E/AndroidRuntime(1205): java.lang.ClassCastException: [Ljava.lang.Object;
04-12 20:17:26.663: E/AndroidRuntime(1205): at mmarin.iconpack.manager.Search$1.onTextChanged(Search.java:84)
I guess that the arr_sort.toArray() creates an Object[] but we need a Icons[] resource for the IconsAdapter.
Am I close?
Yea you are right
One quick and ugly solution I can think of is maybe creating an Icons array right after the search and then filling it with all the items in the arraylist. This happens right before you are setting the adapter
Code:
Icons[] data = new Icons[arr_data.size()];
for (int i = 0; i < arr_data.size(); i++)
{
data[i] = arr_data.get(i);
}
listView1.setAdapter(new IconsAdapter(Search.this, R.layout.row, data));
This should work but it isn't really a good solution =/
Quick and ugly works for me! You're 3 for 3!
You get a big special thanks in my App!!!
Once more, thank you and probably I will ask for your help again in a short while, with another issue I can't figure out.
It will probably be about getting a market link for an app through the Share menu in the Play Store and using that information to send an e-mail - but I will try to figure it out for myself . I already found how to get my app in the "Share" menu in the Play Store and also (possibly) how to save that information to a string. Now i have to find out how to actually get my app to start a certain Activity when it is started by the Play Store app. Will do that over the weekend
Sure! I'll be happy to help out with whatever little bit know!
Related
Hi everyone,
I am currently working on my first app which grabs a ZIP from the internet and the extracts it to a certain location. Everything works great but I can not figure out how to show a Toast message when the extraction operation is done.
The code I am using for unzipping is:
Code:
package mmarin.test.download;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
*
* @author jon
*/
public class Decompress{
private String _zipFile;
private String _location;
byte[] buffer = new byte[1024];
int length;
public Decompress(String zipFile, String location) {
_zipFile = zipFile;
_location = location;
_dirChecker("");
}
public void unzip() {
try {
FileInputStream fin = new FileInputStream(_zipFile);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
Log.v("Decompress", "Unzipping " + ze.getName());
if(ze.isDirectory()) {
_dirChecker(ze.getName());
} else {
FileOutputStream fout = new FileOutputStream(_location + ze.getName());
while ((length = zin.read(buffer))>0) {
fout.write(buffer, 0, length);
}
zin.closeEntry();
fout.close();
}
}
zin.close();
} catch(Exception e) {
Log.e("Decompress", "unzip", e);
}
}
private void _dirChecker(String dir) {
File f = new File(_location + dir);
if(!f.isDirectory()) {
f.mkdirs();
}
}
}
I am calling the Decompress activity through a button:
Code:
Button decompress = (Button)findViewById(R.id.button1);
decompress.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
String zipFile = Environment.getExternalStorageDirectory() + "/IPM/Splash.zip";
String unzipLocation = Environment.getExternalStorageDirectory() + "/IPM/Splash/";
Decompress d = new Decompress(zipFile, unzipLocation);
d.unzip();
}
});
I found this here: http://www.jondev.net/articles/Unzipping_Files_with_Android_(Programmatically) and it works great.
As I said above, only issue is displaying a message that everything is done.
Can someone please help me out?
Thank you!
Please use the Q&A Forum for questions &
Read the Forum Rules Ref Posting
Moving to Q&A
Put the toast after zin.close()
www.stackoverflow.com
Here you can find what you want
Xperian using xda app
http://stackoverflow.com/questions/9824772/toast-after-email-intent-message
Check this
Xperian using xda app
RoberGalarga said:
Put the toast after zin.close()
Click to expand...
Click to collapse
Hey,
I tried this but it doesn't work. I used this statement:
Code:
Toast.makeText(this, "Extraction complete", "LENGTH_SHORT").show();
and I got this error message: The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (Decompress, String, String).
Help?
The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (Decompress, String, String)
What the above line means is that you need to pass a Context object, a CharSequence object and an int. You are passing the wrong object types (Decompress, String, String).
The example you saw used the Toast in the activity class itself, that is why the first value passed was a this. The "LENGTH_SHORT" is actually a constant Toast.LENGTH_SHORT.
I am guessing you are making the button object in your main activity class. So i'd suggest making an additional method for the activity class that looks like this
Code:
public void displayToast(CharSequence cs)
{
Toast.makeText(this, cs, Toast.LENGTH_SHORT).show();
}
and then make the following change to your code
Code:
Button decompress = (Button)findViewById(R.id.button1);
decompress.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
String zipFile = Environment.getExternalStorageDirectory() + "/IPM/Splash.zip";
String unzipLocation = Environment.getExternalStorageDirectory() + "/IPM/Splash/";
Decompress d = new Decompress(zipFile, unzipLocation);
d.unzip();
// Add the following line
displayToast("Unzip complete");
}
});
Let me know if it worked for you.
The_R said:
The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (Decompress, String, String)
What the above line means is that you need to pass a Context object, a CharSequence object and an int. You are passing the wrong object types (Decompress, String, String).
The example you saw used the Toast in the activity class itself, that is why the first value passed was a this. The "LENGTH_SHORT" is actually a constant Toast.LENGTH_SHORT.
I am guessing you are making the button object in your main activity class. So i'd suggest making an additional method for the activity class that looks like this
Code:
public void displayToast(CharSequence cs)
{
Toast.makeText(this, cs, Toast.LENGTH_SHORT).show();
}
and then make the following change to your code
Code:
Button decompress = (Button)findViewById(R.id.button1);
decompress.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
String zipFile = Environment.getExternalStorageDirectory() + "/IPM/Splash.zip";
String unzipLocation = Environment.getExternalStorageDirectory() + "/IPM/Splash/";
Decompress d = new Decompress(zipFile, unzipLocation);
d.unzip();
// Add the following line
displayToast("Unzip complete");
}
});
Let me know if it worked for you.
Click to expand...
Click to collapse
PERFECT! You're amazing!
Hello guys first of all I'm new here so i hope i make everything correct and I'm in the right section.
I just searched for the Stock S3 Gallery (the same like Note 2 with Gallery Flow Effect) but I couldn't find somehting that runs on AOSP Roms, so I just googled a little bit and found these Code (Source was stackoverflow.com, sorry but I can't post the links because Forum Rules with 10 Posts).
Code:
gal_caterories.setAdapter(new CategoryPreferenceAdapter (context,
response.customOfferPojo.offerAvailableCategories,1));
public class CategoryPreferenceAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
public static List<String> thumbnailsselection;
private ViewHolder holder = new ViewHolder();
private List<CategoriesClass> categories = new ArrayList<CategoriesClass>();
public static final int ACTIVITY_CREATE = 10;
public static List<Integer> prefferedCategories = new ArrayList<Integer>();
private int i = 0;
// Keep all Images in array
// public Integer[] mThumbIds = { R.drawable.pic_1, R.drawable.pic_2,
// R.drawable.pic_3, R.drawable.pic_4, R.drawable.pic_5,
// R.drawable.pic_6, R.drawable.pic_7, R.drawable.pic_8,
// R.drawable.pic_9, R.drawable.pic_10, R.drawable.pic_11,
// R.drawable.pic_12, R.drawable.pic_13, R.drawable.pic_14,
// R.drawable.pic_15 };
public CategoryPreferenceAdapter(Context c,
List<CategoriesClass> categories, List<Integer> prefferedCategories) {
this.mContext = c;
this.categories = categories;
this.prefferedCategories = prefferedCategories;
mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
thumbnailsselection = new ArrayList<String>();
}
class ViewHolder {
ImageView imageview;
TextView tv_categoryname;
// LinearLayout ll_category_selection;
CheckBox cb_check_category;
}
[user=439709]@override[/user]
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = mInflater
.inflate(R.layout.template_categories_grid, null);
holder.imageview = (ImageView) convertView
.findViewById(R.id.iv_image_category);
holder.tv_categoryname = (TextView) convertView
.findViewById(R.id.tv_categoryname);
// holder.ll_category_selection = (LinearLayout) convertView
// .findViewById(R.id.ll_category_selection);
holder.cb_check_category = (CheckBox) convertView
.findViewById(R.id.cb_check_category);
if (prefferedCategories.contains(categories.get(position).categoryid)) {
holder.cb_check_category.setChecked(true);
} else {
holder.cb_check_category.setChecked(false);
holder.cb_check_category.setBackgroundColor(Color.TRANSPARENT);
}
holder.tv_categoryname.setText(categories.get(position).category
.toUpperCase());
holder.cb_check_category
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
prefferedCategories.add(categories.get(position).categoryid);
} else {
holder.cb_check_category
.setBackgroundColor(Color.TRANSPARENT);
prefferedCategories.remove(categories.get(position).categoryid);
}
}
});
ImageLoader imageload = new ImageLoader(mContext);
imageload.DisplayImage(
categories.get(position).defaultCategoryImage.toString(),
holder.imageview);
holder.imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
return convertView;
}
[user=439709]@override[/user]
public int getCount() {
// TODO Auto-generated method stub
return categories.size();
}
[user=439709]@override[/user]
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
[user=439709]@override[/user]
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}
Can someone maybe build an App with this Code? I ask because I have no clue about that, but I think the sourcecode is good and many guys would be happy. Maybe some Guy has the time to do it? (It is maybe a cool App for the Playstore too).
Thank you for the attention.
Lostion said:
Hello guys first of all I'm new here so i hope i make everything correct and I'm in the right section.
I just searched for the Stock S3 Gallery (the same like Note 2 with Gallery Flow Effect) but I couldn't find somehting that runs on AOSP Roms, so I just googled a little bit and found these Code (Source was stackoverflow.com, sorry but I can't post the links because Forum Rules with 10 Posts).
Code:
gal_caterories.setAdapter(new CategoryPreferenceAdapter (context,
response.customOfferPojo.offerAvailableCategories,1));
public class CategoryPreferenceAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
public static List<String> thumbnailsselection;
private ViewHolder holder = new ViewHolder();
private List<CategoriesClass> categories = new ArrayList<CategoriesClass>();
public static final int ACTIVITY_CREATE = 10;
public static List<Integer> prefferedCategories = new ArrayList<Integer>();
private int i = 0;
// Keep all Images in array
// public Integer[] mThumbIds = { R.drawable.pic_1, R.drawable.pic_2,
// R.drawable.pic_3, R.drawable.pic_4, R.drawable.pic_5,
// R.drawable.pic_6, R.drawable.pic_7, R.drawable.pic_8,
// R.drawable.pic_9, R.drawable.pic_10, R.drawable.pic_11,
// R.drawable.pic_12, R.drawable.pic_13, R.drawable.pic_14,
// R.drawable.pic_15 };
public CategoryPreferenceAdapter(Context c,
List<CategoriesClass> categories, List<Integer> prefferedCategories) {
this.mContext = c;
this.categories = categories;
this.prefferedCategories = prefferedCategories;
mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
thumbnailsselection = new ArrayList<String>();
}
class ViewHolder {
ImageView imageview;
TextView tv_categoryname;
// LinearLayout ll_category_selection;
CheckBox cb_check_category;
}
[user=439709]@override[/user]
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = mInflater
.inflate(R.layout.template_categories_grid, null);
holder.imageview = (ImageView) convertView
.findViewById(R.id.iv_image_category);
holder.tv_categoryname = (TextView) convertView
.findViewById(R.id.tv_categoryname);
// holder.ll_category_selection = (LinearLayout) convertView
// .findViewById(R.id.ll_category_selection);
holder.cb_check_category = (CheckBox) convertView
.findViewById(R.id.cb_check_category);
if (prefferedCategories.contains(categories.get(position).categoryid)) {
holder.cb_check_category.setChecked(true);
} else {
holder.cb_check_category.setChecked(false);
holder.cb_check_category.setBackgroundColor(Color.TRANSPARENT);
}
holder.tv_categoryname.setText(categories.get(position).category
.toUpperCase());
holder.cb_check_category
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
prefferedCategories.add(categories.get(position).categoryid);
} else {
holder.cb_check_category
.setBackgroundColor(Color.TRANSPARENT);
prefferedCategories.remove(categories.get(position).categoryid);
}
}
});
ImageLoader imageload = new ImageLoader(mContext);
imageload.DisplayImage(
categories.get(position).defaultCategoryImage.toString(),
holder.imageview);
holder.imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
return convertView;
}
[user=439709]@override[/user]
public int getCount() {
// TODO Auto-generated method stub
return categories.size();
}
[user=439709]@override[/user]
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
[user=439709]@override[/user]
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}
Can someone maybe build an App with this Code? I ask because I have no clue about that, but I think the sourcecode is good and many guys would be happy. Maybe some Guy has the time to do it? (It is maybe a cool App for the Playstore too).
Thank you for the attention.
Click to expand...
Click to collapse
Me too .Looking for the same thing for a long time.
But could not find any.:crying:
Just wishing some guy to build an apk that would run on all phones like some other games.
Keep searching and kindly share if you find any.:good:
Thanks
Thank you man for posting this i think a lot of us wish to get this app on cyanogenmod for example i hope there's a hero dev. Can do this for us
Regards
تم الإرسال من جهازي GT-I9300 بواسطة تاباتلك 4 now Free
Hi there!
I'm trying to know how to get the position of an item when it is already been dropped. I have used a gridview to list images.
my xml code is:
Code:
<RelativeLayout xmlns:android="..."
xmlns:tools="..."
android:id="@+id/parent_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridView
android:id="@+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:horizontalSpacing="10dip"
android:numColumns="4"
android:verticalSpacing="10dip" />
</RelativeLayout>
here is my mainactivity class:
Code:
public class MainActivity extends Activity implements OnDragListener,
OnItemLongClickListener {
ArrayList drawables;
GridView gridView;
private BaseAdapter adapter;
private int draggedIndex = -1;
private int droppedIndex = -1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawables = new ArrayList();
drawables.add(R.drawable.ic_launcher);
drawables.add(R.drawable.ic_launcher);
drawables.add(R.drawable.ic_launcher);
drawables.add(R.drawable.ic_launcher);
drawables.add(R.drawable.ic_launcher);
drawables.add(R.drawable.ic_launcher);
drawables.add(R.drawable.ic_launcher);
drawables.add(R.drawable.ic_launcher);
gridView = (GridView) findViewById(R.id.grid_view);
gridView.setOnItemLongClickListener(MainActivity.this);
gridView.setAdapter(adapter = new BaseAdapter() {
@Override
// Get a View that displays the data at the specified position in
// the data set.
public View getView(int position, View convertView,
ViewGroup gridView) {
// try to reuse the views.
ImageView view = (ImageView) convertView;
// if convert view is null then create a new instance else reuse
// it
if (view == null) {
view = new ImageView(MainActivity.this);
}
view.setImageResource((Integer) drawables.get(position));
view.setTag(String.valueOf(position));
return view;
}
@Override
// Get the row id associated with the specified position in the
// list.
public long getItemId(int position) {
return position;
}
@Override
// Get the data item associated with the specified position in the
// data set.
public Object getItem(int position) {
return drawables.get(position);
}
@Override
// How many items are in the data set represented by this Adapter.
public int getCount() {
return drawables.size();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onDrag(View view, DragEvent dragEvent) {
switch (dragEvent.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
// Ignore this event
return true;
case DragEvent.ACTION_DRAG_ENTERED:
// Ignore this event
return true;
case DragEvent.ACTION_DRAG_EXITED:
// Ignore this event
return true;
case DragEvent.ACTION_DRAG_LOCATION:
// Ignore this event
return true;
case DragEvent.ACTION_DROP:
// Dropped inside a new view
// get the position where the item is been dropped
adapter.notifyDataSetChanged();
case DragEvent.ACTION_DRAG_ENDED:
view.setOnDragListener(null);
return true;
}
return false;
}
@Override
public boolean onItemLongClick(AdapterView gridView, View view,
int position, long row) {
ClipData.Item item = new ClipData.Item((String) view.getTag());
ClipData clipData = new ClipData((CharSequence) view.getTag(),
new String[] { ClipDescription.MIMETYPE_TEXT_PLAIN }, item);
view.startDrag(clipData, new View.DragShadowBuilder(view), null, 0);
view.setVisibility(View.INVISIBLE);
draggedIndex = position;
return true;
}
}
I guessed that i can get the position when the action is "DragEvent.ACTION_DROP" but i cannot come up with the way to get the position!
Any ideas?
thanks in advance
I couldn't think of a better way to word the title, but the issue I'm having is this: I'm using HoloColorPicker by Lars Werkman. Now I have correctly imported the library, I can see the ColorPicker and other views from the library in my app, the only symbol that is failing to be found is OnColorChangedListener. I looked through the code of the library and it is an interface that is defined in ColorPicker.java, which I am importing, and the ColorPicker view itself, as I already stated, is appearing in my GUI fine.
Here is my associated code:
Code:
import com.larswerkman.holocolorpicker.SaturationBar;
import com.larswerkman.holocolorpicker.ColorPicker;
import com.larswerkman.holocolorpicker.ValueBar;
public class ColorChooserDialog extends DialogFragment {
private String title;
private CustomColor cur_color;
private boolean edit = false;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.choose_color_dialog_title);
// Inflate layout color chooser view and set to builder
LayoutInflater layout_inflater = getActivity().getLayoutInflater();
View view = layout_inflater.inflate(R.layout.color_chooser_view, null);
builder.setView(view);
// Get views by IDs
EditText hex_input = (EditText)view.findViewById(R.id.hexInput);
EditText red_input = (EditText)view.findViewById(R.id.redInput);
EditText green_input = (EditText)view.findViewById(R.id.greenInput);
EditText blue_input = (EditText)view.findViewById(R.id.blueInput);
ColorPicker color_picker = (ColorPicker)view.findViewById(R.id.colorPicker);
SaturationBar sat_bar = (SaturationBar)view.findViewById(R.id.saturationBar);
ValueBar val_bar = (ValueBar)view.findViewById(R.id.valueBar);
// Connect bars to color picker
color_picker.addSaturationBar(sat_bar);
color_picker.addValueBar(val_bar);
// When color is changed via picker, set text fields
color_picker.setOnColorChangedListener(new OnColorChangedListener() {
@Override
public void onColorChanged(int color) {
//TODO
}
});
// Add listener for when HEX value is changed
hex_input.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable editable) {}
@Override
public void beforeTextChanged(CharSequence chars, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence chars, int start, int before, int count) {
//TODO
}
});
builder.setPositiveButton(
R.string.ok_button,
new DialogInterface.OnClickListener() {
// User clicked OK, add color to palette
public void onClick(DialogInterface dialog, int id) {
//TODO
}
}
);
builder.setNegativeButton(
R.string.cancel_button,
new DialogInterface.OnClickListener() {
// User cancelled the dialog, don't create palette
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}
);
// Create the AlertDialog object and return it
return builder.create();
}
}
OK, so I figured it out, I didn't notice this at first because Java is not my first language, but the OnColorChangedListener is within the ColorPicker class, so I instead of using this:
Code:
color_picker.setOnColorChangedListener(new OnColorChangedListener() {
@Override
public void onColorChanged(int color) {
//TODO
}
});
I needed to use this:
Code:
color_picker.setOnColorChangedListener(new [B]ColorPicker.[/B]OnColorChangedListener() {
@Override
public void onColorChanged(int color) {
//TODO
}
});
I hope this is the right place to ask this. The other sections seem to be more about discussion. I am running into one error that I have not encountered before and have tried quite a bit to find out what is going on.
AndroidStudio said:
Error: (109, 51) error: non-static method getAllContacts() cannot be referenced from a static context
Click to expand...
Click to collapse
Code:
// Class for dealing with the database
package com.testapplication.app;
import ...
public class DataBaseHandler extends SQLiteOpenHelper {
public List<Contact> getAllContacts() {
List<Contact> contListAdapt = new ArrayList<Contact>();
// Becomes non static in a static context if the method becomes static
SQLiteDatabase sql = getWritableDatabase();
Cursor cursor = sql.rawQuery("SELECT * FROM " + TABLE_CONTACTS, null);
if (cursor.moveToFirst()) {
do {
new Contact(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), Uri.parse(cursor.getString(5)));
} while (cursor.moveToNext());
}
cursor.close();
sql.close();
return contListAdapt;
}
}
Code:
// The main activity
package com.testapplication.app;
import ...
public class MainActivity extends ActionBarActivity {
List<Contact> contactListAdapter = new ArrayList<Contact>();
@Override
protected void onCreate (Bundle savedInstanceState) {
// There is a class called Contact
// FIXME : non static method in static context
Collection<Contact> coll = DataBaseHandler.getAllContacts();
if(!coll.isEmpty()) {
contactListAdapter.addAll(coll);
}
}
}
As far as I can tell this is all that is involved with this error. If I make getAllContacts() static then I will get the same old error but in a different spot: getWritableDatabase();