0

i´m newbie in android programming and try to set two Image Array from (Values)XML in to one common Array using TypedArray.

Here are my XML Arrays:

<array name="Img1">
<item>  @drawable/pic_1 </item>
<item>  @drawable/pic_2 </item> 
<item>  @drawable/pic_3 </item> 
</array>

<array name="Img2">
<item>  @drawable/pic_4 </item>
<item>  @drawable/pic_5 </item> 
<item>  @drawable/pic_6 </item> 
</array>

imprtant part of java code in onCreate method:

private TypedArray Images;

TypedArray img1 = res.obtainTypedArray(R.array.Img1);
TypedArray img2 = res.obtainTypedArray(R.array.Img2):

ArrayList<TypedArray> img = new ArrayList<TypedArray>();
img.addAll(Arrays.asList(img1));
img.addAll(Arrays.asList(img2));
Images = img.toArray(new TypedArray(img1.length()+img2.length); <-- NOT WORKING!

what ist wron in my code? maybe i can solve the problem by another way?

Thank you very much!!!

public class c_DTArtikel_allforsearch extends Activity implements AdapterView.OnItemClickListener { //Variablen deklarieren! private ListView ListViewLayout; private String[] Number; private int[] ImagesID; private EditText Search; ArrayList name = new ArrayList();

My method:

private ListView ListViewLayout;
private int[] ImagesID;
private EditText Search;
ArrayList<String> name = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_lv_search);


    Resources res = getResources();

    String[] saugname = res.getStringArray(R.array.SauglanzeName);
    String[] pumpmname = res.getStringArray(R.array.PumpMName);


    String[] saugnummer = res.getStringArray(R.array.SauglanzeNumber);
    String[] pumpmnummer = res.getStringArray(R.array.PumpMNummer);

    TypedArray saugimg = res.obtainTypedArray(R.array.SauglanzeImg);
    TypedArray pumpmimg = res.obtainTypedArray(R.array.PumpeMImg);


    name.addAll(Arrays.asList(saugname));
    name.addAll(Arrays.asList(pumpmname));


    ArrayList<String> number = new ArrayList<String>();
    number.addAll(Arrays.asList(saugnummer));
    number.addAll(Arrays.asList(pumpmnummer));
    Number = number.toArray(new String[saugnummer.length+pumpmnummer.length]);

    ArrayList<Integer> imgInts = new ArrayList<Integer>();
    int nr = -1;
    while ( ++nr < saugimg.length() )
        imgInts.add (saugimg.getResourceId(nr, 0));
    nr = -1;
    while ( ++nr < pumpmimg.length() )
        imgInts.add (pumpmimg.getResourceId(nr, 0));
    ImagesID = new int[imgInts.size()];
    for (int i=0; i < imgInts.size(); i++)
    {
        ImagesID[i] = ((Integer) imgInts.get(i)).intValue();
    }

    Search = (EditText) findViewById(R.id.EditTextSearch);
    ListViewLayout = (ListView) findViewById(R.id.listViewLayoutSearch);
    final TIAdapter adapter = new TIAdapter(this, name, ImagesID, Number);
    ListViewLayout.setAdapter(adapter);
    ListViewLayout.setOnItemClickListener(this);

    Search.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            adapter.getFilter().filter(cs);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
        }
    });

class Adapter:

    class TIAdapter extends ArrayAdapter<String> {
    Context context;
    int [] ImagesID;
    ArrayList<String> NameArray;
    String[] NummerArray;

    TIAdapter(Context c, ArrayList<String> Artikel, int[] imgs, String[] Nummer) {
        super(c, R.layout.layout_text_image, R.id.textViewArtikel, Artikel);
        this.context = c;
        this.ImagesID = imgs;
        this.NameArray = Artikel;
        this.NummerArray = Nummer;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ImageView imageView = null;

        if(row == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.layout_text_image, parent, false);
            imageView = (ImageView)row.findViewById(R.id.imageViewArtikel);
        }
        else {
            imageView = (ImageView)row.findViewById(R.id.imageViewArtikel);
            DecodeTask dt1 = (DecodeTask)imageView.getTag(R.id.imageViewArtikel);
            if(dt1 != null)
                dt1.cancel(true);
        }

        imageView.setImageBitmap(null);
        DecodeTask dt2 = new DecodeTask(getContext(), imageView, ImagesID[position]);
        dt2.execute();

        TextView textViewArtikel = (TextView) row.findViewById(R.id.textViewArtikel);
        TextView textViewArtikelNummer = (TextView) row.findViewById(R.id.textViewArtikelNummer);

        imageView.setTag(R.id.imageViewArtikel, dt2);
        textViewArtikel.setText(name.get(position));
        textViewArtikelNummer.setText(Number[position]);
        return row;
    }
}
Sergej
  • 11
  • 4

2 Answers2

0

Maybe something like this:

   ArrayList<Drawable> img = new ArrayList<Drawable>();

   int nr = -1;
   while ( ++nr < img1.length() )
       img.add( img1.getDrawable(nr));

   nr = -1;
   while ( ++nr < img2.length() )
       img.add( img2.getDrawable(nr));

   Drawable [] Images;
   Images = img.toArray(new Drawable[img.size()] );  
greenapps
  • 11,154
  • 2
  • 16
  • 19
  • doesn't work, because my Adapter required "String[], TypedArray, String[]" and get "String[], Drawable[], String[]". If i change TypedArray to Drawable[] in my adapter, compilation runs but the application crashes. Any another idea? – Sergej Apr 24 '14 at 12:10
  • i can't, bacause new user:( – Sergej Apr 24 '14 at 12:18
  • You get an OutOfMemory error for the statement on line 127. What's there? Are your pictures that big? – greenapps Apr 24 '14 at 12:44
  • there is: img.add( img2.getDrawable(nr)); (see your answer). No the pictures are small. – Sergej Apr 24 '14 at 13:01
  • My intention to combine the arrays failed. My plan was to combine the identifiers for the pictures but apparently the code really grabs the pictures and puts them in an array. But you have big pictures why would it fail otherwise? How big are they? Compare to the size mentioned at the memoryerror. – greenapps Apr 24 '14 at 13:07
  • pictures are 10-20kb each, i have about 250 pictures. the size of drawable directory is 2,54mb. Maybe there is another way to get the pictures from xml without TypedAray? – Sergej Apr 24 '14 at 13:15
  • Well you do not need to put the pictures at that moment in an (array)list but only the resouceIds. See my mext try. – greenapps Apr 24 '14 at 13:32
0

Maybe better to give the adapter an array of resource identifiers (which are Integers).

   ArrayList<Integer> imgInts = new ArrayList<Integer>();

   int nr = -1;
   while ( ++nr < img1.length() )
       imgInts.add (img1.getResourceId(nr, 0)); 

   nr = -1;
   while ( ++nr < img2.length() )          
       imgInts.add (img1.getResourceId(nr, 0));  

   int[] ResourceIds = new int[imgInts.size()];
   for (int i=0; i < imgInts.size(); i++)
   {
    ResourceIds[i] = ((Integer) imgInts.get(i)).intValue();
    }

Use ResourceIds as parameter to construct the adapter.

greenapps
  • 11,154
  • 2
  • 16
  • 19
  • PERFECT! It's function - yes! THANK YOU – Sergej Apr 24 '14 at 13:50
  • now you know my adapter and my listview, would you recommend me how to set filter for the names? the easiest way... thx – Sergej Apr 24 '14 at 14:51
  • Your question is way to vague. Wich names? Moreover you did not update your adapter code. – greenapps Apr 24 '14 at 15:03
  • i've ListView with Names(String), Numbers(String), pictures(int ResourceID). now i wont to make edittext on the top of ListView Layout and filter my rows by text. see example here:https://www.youtube.com/watch?v=Y9LreGe8IBY – Sergej Apr 24 '14 at 15:25
  • http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/ – greenapps Apr 24 '14 at 15:40
  • Thanks, i've tryed to implement the code, but doesnt work. See the code and errormessage(edited post). – Sergej Apr 25 '14 at 12:00
  • The original has EditText inputSearch; Where and how did you declare Search? – greenapps Apr 26 '14 at 10:29
  • i use just Search as EditText, i've declare Search on the top of the Activity as "private EditText Search;" and the text watcher is inside of the on Create method. – Sergej Apr 26 '14 at 11:13
  • Then what is different in declaring the listview? Or the adapter? – greenapps Apr 26 '14 at 11:29
  • i took wrong one, right one is adapter instead TIAdapter. now the app is running but i get wrong results in the list... – Sergej Apr 26 '14 at 11:59
  • i don't know a the moment what is wrong exactly - i'll take a look at first and post later. thanks – Sergej Apr 26 '14 at 12:05
  • can't find the problem, i get wrong results if i put in one letter to editText. for example i put in "m" and get results with word withot "m"? i've update my code and adapter. thanks – Sergej Apr 26 '14 at 13:30
  • Try with changing adapter parameter ArrayList Artikel to String [] Artikel. (As has androidhive). – greenapps Apr 26 '14 at 17:30
  • I've tryed but the same result. – Sergej Apr 26 '14 at 18:36
  • I think you have to override the getFilter method of the adapter as this one is acting on the array you gave it with super(....., Article); – greenapps Apr 27 '14 at 08:22
  • No but the ArrayAdapter has. You are calling/using it you know. And now you have to @Override it. As you have overridden getView(). You have to implement one yourself. Googling gives a lot of answers for instance: http://stackoverflow.com/questions/19122848/custom-getfilter-in-custom-arrayadapter-in-android – greenapps Apr 27 '14 at 09:32
  • Maybe easier: http://stackoverflow.com/questions/10379261/extending-arrayadapter-in-android Please start a new thread if you need help implementing getFilter(). – greenapps Apr 27 '14 at 09:43
  • Thanks, the all are using ArrayList I have ArrayList. How to declarate it as "Item" or something else? – Sergej Apr 27 '14 at 12:11
  • You have to rewrite it to an item to. As it makes no sense to only filter the article names. You have to filter the article numbers also and the image resourceids to keep all in sync. All that can be done by declaring an Item structure with name, number, id. As in the examples. – greenapps Apr 27 '14 at 12:40
  • I don't understand how to declarate. Could you help me? – Sergej Apr 27 '14 at 14:49
  • In http://stackoverflow.com/questions/19122848/custom-getfilter-in-custom-arrayadapter-in-android you see: ArrayAdapter and further class ListTo with fileName and fileUrl; In example of mokasocial.com you see ArrayAdapter. Contact is not defined there but it will also be a structure with name, phonenumber and so on. So you do something like that: a new class witch contains name, number and id. Replace the three parameters of your adapter (name, number, ids) by one for that item. – greenapps Apr 27 '14 at 16:15
  • it's a little complicated for me to implement the filter, i've opened new thread: http://stackoverflow.com/questions/23341241/implementing-getfilter-into-custom-arrayadapter-with-asynctask – Sergej Apr 28 '14 at 13:54