gpt4 book ai didi

java - 动态改变ListView大小并加载图像

转载 作者:行者123 更新时间:2023-12-01 04:48:53 25 4
gpt4 key购买 nike

我想创建一个ListView,每行都包含图像和文本,动态更改其大小(例如,在开始时listView将不显示任何内容,然后,我可以向listView添加条目),我也希望listView 将可以加载位图图像列表,而不是可绘制图像。

我创建了这段代码,但是该代码仅从可绘制对象加载图像并创建一次(意味着我无法动态更改列表 - 添加或删除 listView 条目)

String[] text = { "One", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten" };

int[] image = { R.drawable.logo, R.drawable.logo, R.drawable.logo,
R.drawable.logo, R.drawable.logo, R.drawable.logo, R.drawable.logo,
R.drawable.logo, R.drawable.logo, R.drawable.logo };

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lv.setAdapter(new MyCustomAdapter(text, listImages));
edittext= (EditText) findViewById(R.id.EditText01);

edittext.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 = edittext.getText().length();
text_sort.clear();
image_sort.clear();

for (int i = 0; i < text.length; i++)
{
if (textlength <= text[i].length())
{
if (edittext.getText().toString().
equalsIgnoreCase((String) text[i].subSequence(0, textlength)))
{
text_sort.add(text[i]);
// image_sort.add(image[i]);
}
}
}

lv.setAdapter(new MyCustomAdapter
(text_sort, image_sort));

}
});
}

最佳答案

检查 listview 的实现 http://www.java2s.com/Code/Android/UI/Demonstratestheusingalistviewintranscriptmode.htm

查看本教程以了解如何使用自定义 ListView 项 http://www.framentos.com/en/android-tutorial/2012/07/16/listview-in-android-using-custom-listadapter-and-viewcache/

编辑:使用此适配器类:

  class MyCustomAdapter extends BaseAdapter{      public static  ArrayList text_array = new ArrayList();      public static  ArrayList image_array = new ArrayList();      public int getCount(){           return text_array..size();      }      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);         if(listImages.get(position) != null) {                     image.setImageBitmap(image_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();     } }

从 Activity 类中调用 addObject 函数以在 ListView 中添加新项目

关于java - 动态改变ListView大小并加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15356099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com