我想在每一行中创建一个ListView,其中包含动态改变其大小的每一行中的图像和文本(例如,在开始时listView将不显示任何内容,然后,我将可以向listView添加条目),我也想要listView可以加载位图图像列表而不是可绘制图像。
我创建了这段代码,但代码只从drawable加载图片并创建一次(意味着我无法动态更改列表 - 添加或删除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));
}
});
}
答案 0 :(得分:0)
检查listview http://www.java2s.com/Code/Android/UI/Demonstratestheusingalistviewintranscriptmode.htm
的此实现查看本教程,了解如何使用自定义列表视图项http://www.framentos.com/en/android-tutorial/2012/07/16/listview-in-android-using-custom-listadapter-and-viewcache/
Eidt: 使用此适配器类:
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(); } }
从您的活动类调用addObject函数以在列表视图中添加新项