我创建了一个listView,我可以将图像和文本放在listView中,并动态地将行添加到listView中。

但是我不明白如何实现我可以更改listView中特定行项目的布局?

例如,在活动开始时,我将用5行初始化listView,并且当触发发生时(几分钟后),我想更改例如第四listView项(仅更改第四项的布局,所有其他条目保留原始布局)

非常感谢

private  ListView lv;
private List<String> text = new ArrayList<String>();
private List<Bitmap> listImages;
private String freindsId;

ArrayList<String> text_sort = new ArrayList<String>();
ArrayList<Bitmap> image_sort = new ArrayList<Bitmap>();


private MyCustomAdapter adapter;
  adapter =new MyCustomAdapter(text,listImages);
  lv.setAdapter(adapter);

class MyCustomAdapter extends BaseAdapter{
          public   List<String> text_array = new ArrayList<String>();
          public   List<Bitmap> image_array = new ArrayList<Bitmap>();
          public int getCount(){
               return text_array.size();
          }

          MyCustomAdapter(List<String> text, List<Bitmap> image)
          {
           text_array = text;
           image_array = image;
          }
          public long getItemId(int position){
               return position;
          }
          public String getItem(int position){
               return null;
          }
          public View getView(final int position, View convertView, ViewGroup parent) {
            LayoutInflater inflate = getLayoutInflater();
            View v = inflate.inflate(R.layout.listview, parent, false);
            final ImageView image = (ImageView) v.findViewById(R.id.ImageView01);
            TextView txtUnderImage =(TextView) v.findViewById(R.id.TextView01);

             if(listImages.get(position) != null) {
                         Bitmap bitmap = image_array.get(position);
                        image.setImageBitmap(bitmap);
                        ByteArrayOutputStream stream = new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                        final byte[] byteArray = stream.toByteArray();

                         image.setOnClickListener(new View.OnClickListener() {

                            public void onClick(View v) {
                                // Switching to Register screen
                             Intent i = new Intent(getApplicationContext(), itemSaleActivity.class);
                             i.putExtra("picture", byteArray);
                            startActivity(i);


                                }
                        });
                         txtUnderImage.setText(text_array.get(position));
                         image.setVisibility(View.VISIBLE);
              } else {
                         image.setVisibility(View.GONE);
                         image.setImageBitmap(null);
                          image.setVisibility(View.VISIBLE);
          }
         return v;

        }
         public void addObject(String text, Bitmap bitmap) {
                text_array.add(text);
                image_array.add(bitmap);
            notifyDataSetChanged();
         }

         public void addFirst(String text, Bitmap bitmap) {

                image_array.add(0,bitmap);
                text_array.add(0,text);

                notifyDataSetChanged();
             }
         public void removeObject(String text,Bitmap bitmap) {

                int indexToRemove = image_array.indexOf(bitmap);
                image_array.remove(indexToRemove);
                text_array.remove(indexToRemove);
                notifyDataSetChanged();
         }
         public void deleteAll() {
             image_array.clear();
             text_array.clear();
             notifyDataSetChanged();
         }
    }

最佳答案

尝试像这样获取特定位置的视图

getChildAt(position)您将获得自己的视图,

并更改行的布局,然后调用notifyDataSetChanged()。

如果您想突出显示所选行,请使用此
Inflate ListView row from OnClickListener in Android?

当事件触发更改适配器的数据并在Adapater对象上调用notifyDataSetChanged()时。

09-11 18:50
查看更多