动态改变的ListView的大小和载入图像 [英] dynamically change ListView size and load images
问题描述
我想创建与动态改变他的大小每一行中的图像和文本的ListView控件(例如,在启动列表视图会显示什么,然后,我就可以添加条目到ListView),我也想了ListView控件将可以绘制加载列表位图图像,而不是图像。
我创造了这个code,然而code只加载从绘制图像,创造了一次(意思是我不能更改列表动态 - 添加或删除的ListView项)
的String [] =文字{一,二,三,四有,五,六,七宗罪,
八,九,十大}; INT [] =图像{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}; 公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
lv.setAdapter(新MyCustomAdapter(文字,listImages));
的EditText =(EditText上)findViewById(R.id.EditText01); edittext.addTextChangedListener(新TextWatcher()
{ 公共无效afterTextChanged(编辑S)
{ } 公共无效beforeTextChanged(CharSequence中,诠释开始,
诠释计数,诠释后)
{ } 公共无效onTextChanged(CharSequence中,诠释开始,
INT之前,诠释计数)
{ 。=长度限制:Textlength edittext.getText()长();
text_sort.clear();
image_sort.clear(); 的for(int i = 0; I< text.length;我++)
{
如果(与长度限制:Textlength LT =文[I]。长度())
{
如果(edittext.getText()。的toString()。
equalsIgnoreCase((字符串)文本[I] .subSequence(0,长度限制:Textlength)))
{
text_sort.add(文[I]);
// image_sort.add(图片由[i]);
}
}
} lv.setAdapter(新MyCustomAdapter
(text_sort,image_sort)); }
});
}
勾选此实施的ListView的http://www.java2s.com/$c$c/Android/UI/Demonstratestheusingalistviewintranscriptmode.htm
检查本教程中使用自定义列表视图项<一个href=\"http://www.framentos.com/en/android-tutorial/2012/07/16/listview-in-android-using-custom-listadapter-and-viewcache/\" rel=\"nofollow\">http://www.framentos.com/en/android-tutorial/2012/07/16/listview-in-android-using-custom-listadapter-and-viewcache/
Eidt:
使用这个适配器类:
类MyCustomAdapter延伸BaseAdapter {
公共静态的ArrayList text_array =新的ArrayList();
公共静态的ArrayList image_array =新的ArrayList();
公众诠释的getCount(){
返回text_array..size();
}
众长getItemId(INT位置){
返回的位置;
}
公共字符串的getItem(INT位置){
返回null;
}
公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
LayoutInflater膨胀= getLayoutInflater();
视图V = inflate.inflate(R.layout.listview,父母,假);
最终ImageView的图像=(ImageView的)v.findViewById(R.id.ImageView01); 如果(listImages.get(位置)!= NULL){
image.setImageBitmap(image_array.get(位置));
image.setVisibility(View.VISIBLE);
}其他{
image.setVisibility(View.GONE);
image.setImageBitmap(NULL);
image.setVisibility(View.VISIBLE);
}
返回伏; }
公共无效ADDOBJECT(字符串文本,位图位图){
text_array.add(文本);
image_array.add(位图);
notifyDataSetChanged();
}
}
调用函数ADDOBJECT从您的活动类的列表视图中添加新项目
i want to create a ListView with images and text in every row that change his size dynamically (for example, at start the listView will show nothing and then, i will can add entries to the listView), also i want that the listView will can load List of Bitmap images and not images from drawable.
i created this code, however the code only load images from drawable and created once (mean that i cant change the list dynamically - adding or remove listView entries)
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));
}
});
}
Check this implementation of listview http://www.java2s.com/Code/Android/UI/Demonstratestheusingalistviewintranscriptmode.htm
Check this tutorial for using customizing list view item http://www.framentos.com/en/android-tutorial/2012/07/16/listview-in-android-using-custom-listadapter-and-viewcache/
Eidt : Use this adapter class:
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(); } }
call addObject function from your activity class to add new item in list view
这篇关于动态改变的ListView的大小和载入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!