【问题标题】:Android custom Array List image updateAndroid 自定义 Array List 图片更新
【发布时间】:2012-03-10 17:47:29
【问题描述】:

我有自定义数组列表,它连接到适配器和自定义 XML,用于我的数组列表的每一行。单击项目时,我已发出和字符串生成器警报,当我确认时,我想更改自定义数组列表中显示的图像。到目前为止,我还无法以某种方式在所选项目中使用新图像更新列表。你知道,它是怎么做到的吗?有什么方法可以跟踪,点击了列表中的哪个位置? on resume 方法和某种“重绘”是最好的解决方案吗?

谢谢

编辑:为我正在使用的适配器添加代码:

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

}

【问题讨论】:

  • 贴出你的适配器的代码。

标签: android arraylist android-listview


【解决方案1】:

你的getView()方法的代码很乱,它的方法应该是这样的简单:

@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;
        }

如果你想要一个列表行点击监听器,那么如果你扩展ListActivity,你必须实现方法onListItemClick(ListView l, View v, int position, long id),或者如果你扩展Activity,则在ListView元素上设置setOnItemClickListener()。然后在onItemClick()方法中:

@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           
}

然后在选择按钮中:

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

【讨论】:

  • @Waypoint 当您在对话框中按下选择按钮时,使用您拥有的更改方法更改项目 mapObject 中的图像,然后调用:adapterObject.notifyDataSetChanged()。这将通知您的适配器数据已更改并刷新列表以反映新更改。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多