0

I have custom Array List, which is connected to adapter and custom XML for each row of my array list. When clicking on item, I have made and string builder alert and when I confirm it, I would like to change image displayed in custom Array List. Up to now, I haven't been able to somehow update list with new image at selected item. Do you know, how it could be done? Is there any way how to track, which posion in the list was clicked? Is the on resume method and some sort of "redraw" the best solution?

Thanks

Edit: Added code for Adapter I am using:

public class datamanagerAdapter extends ArrayAdapter<mapObject>{
private ArrayList<mapObject> items;
 private Context ctx;
 String rememberName="";

public datamanagerAdapter(Context context, int textViewResourceId, ArrayList<mapObject> items) {
    super(context, textViewResourceId, items); 
    this.items = items;
    this.ctx = context;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
        View v = convertView;


        if (v == null) {
            LayoutInflater vi = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
        }
        final mapObject mo = items.get(position);
        if (mo != null) {
                ImageView imv = (ImageView) v.findViewById(R.id.selected);
                TextView one = (TextView) v.findViewById(R.id.mapname);
                TextView two = (TextView) v.findViewById(R.id.map_contains);
                TextView three = (TextView) v.findViewById(R.id.map_size);

                if (one != null) {
                      one.setText(""+mo.getMapName());                            
                }
                if(two != null){
                      two.setText(""+ mo.getMapContains());
                }
                if(three != null){
                    three.setText("Size: "+ mo.getMapSize()+"MB     POI: "+mo.getMapContainsPoi()+"");
                }
                if (imv != null) {
                    imv.setImageDrawable(mo.getImage());  
                }else{
                    imv.setImageDrawable(v.getResources().getDrawable(R.drawable.cross)); 
                }

                v.setOnClickListener(new OnClickListener() {
                    public void onClick(View arg0) {

                            showDetails(mo, ctx);
                    }

                    private void showDetails(final mapObject mo, final Context ctx) {
                        final Context mContext = ctx.getApplicationContext();
                        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
                        final View layout = inflater.inflate(R.layout.custom_dialog,null);
                        AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
                        builder.setView(layout);

                        final AlertDialog alertDialog  = builder.create();

                        TextView l1=(TextView) layout.findViewById(R.id.lineone);
                        TextView l2=(TextView) layout.findViewById(R.id.linetwo);
                        TextView l3=(TextView) layout.findViewById(R.id.linethree);
                        TextView l4=(TextView) layout.findViewById(R.id.linefour);
                        TextView l5=(TextView) layout.findViewById(R.id.linefive);
                        TextView l6=(TextView) layout.findViewById(R.id.linesix);
                        TextView l7=(TextView) layout.findViewById(R.id.lineseven);

                        Button select=(Button) layout.findViewById(R.id.selectActiveMap);
                        Button cancel=(Button) layout.findViewById(R.id.closeWindowButton);

                        rememberName=mo.getMapName();

                        l1.setText("...");
                        l2.setText("Map: "+mo.getMapName());
                        l3.setText("Covered Area: "+mo.getMapContains());
                        l4.setText("Size: "+mo.getMapSize()+" MB");
                        l5.setText("POI: "+mo.getMapContainsPoi());
                        l6.setText("Source: "+mo.getMapSource());
                        l7.setText("Version: "+mo.getMapVersion());

                        select.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                            //here I want to change image which is displayed in the list. If I select this map, I ant to have displayed "tick" better than a "cross" which is used by default.  


                                }
                                alertDialog.dismiss();



                            }
                        });

                        cancel.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                alertDialog.dismiss();

                            }
                        });





                        alertDialog.show();
                    }
                });
        }
        return v;       
}

}

1
  • Post the code of your adapter. Commented Mar 10, 2012 at 17:50

1 Answer 1

1

The code of your getView() method is a mess, it method should be simple like this:

@Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            View v = convertView;

            if (v == null) {
                LayoutInflater vi = (LayoutInflater) ctx
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.stacktest3_row, null);
            }
            final mapObject mo = items.get(position);
            if (mo != null) {
                ImageView imv = (ImageView) v.findViewById(R.id.selected);
                TextView one = (TextView) v.findViewById(R.id.mapname);
                TextView two = (TextView) v.findViewById(R.id.map_contains);
                TextView three = (TextView) v.findViewById(R.id.map_size);

                if (one != null) {
                    one.setText("" + mo.getMapName());
                }
                if (two != null) {
                    two.setText("" + mo.getMapContains());
                }
                if (three != null) {
                    three.setText("Size: " + mo.getMapSize() + "MB     POI: "
                            + mo.getMapContainsPoi() + "");
                }
                if (imv != null) {
                    imv.setImageDrawable(mo.getImage());
                } else {
                    imv.setImageDrawable(v.getResources().getDrawable(
                            R.drawable.cross));
                }

            }
            return v;
        }

If you want a list row click listener then you'll have to implement either the method onListItemClick(ListView l, View v, int position, long id) if you extend ListActivity or set a setOnItemClickListener() on your ListView element if you extend Activity. Then in the onItemClick() method:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    mapObject item = l.getItemAtPosition(position);
            showDetails(item, context); //see what context you can use           
}

then in the select button:

select.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        //If you want to change the image then put your new image(or drawable or what ever) or id in the ITEM mapObject that you pass to the method showDetails() then call notifyDataSetChanged() on your adapter object
         alertDialog.dismiss(); 
        }
    });
Sign up to request clarification or add additional context in comments.

1 Comment

@Waypoint When you press the select button in the dialog change the image in the item mapObject with the change method you have and then call: adapterObject.notifyDataSetChanged(). This will notify your adapter that the data has changed and to refresh the list to reflect the new changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.