问题描述
我有一个很讨厌的问题。我的任务是添加多个LinearLayouts使用自定义XML模板(我不知道该如何布局的许多可能有)与2 TextViews内(每个)到1 ListView项。这样的:
是否有可能做到这一点在ArrayAdapter?任何帮助将是非常美联社preciated!
好吧,我已经成功地做了一件与乔纳斯锆石的帮助。而这就是我的本钱。
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
如果(convertView == NULL)
{
convertView = inflater.inflate(R.layout.parsed_csv_list_view_main_item,父母,假);
}
RealmResults< ParsedCSV>标题= parentFragment.getParsedCSVtitles();
。的String [] parsedTitles = titles.get(0).getValues()分裂();
的String [] parsedValues = items.get(位置).getValues()分裂(;)。
的for(int i = 0; I< parsedValues.length;我++){
查看支架= inflater.inflate(R.layout.parsed_csv_list_view_subitem,父母,假);
TextView的textViewTitles =(TextView中)holder.findViewById(R.id.parsed_csv_list_view_subitem_text_title);
TextView的textViewValues =(TextView中)holder.findViewById(R.id.parsed_csv_list_view_subitem_text_value);
textViewTitles.setText(parsedTitles [I]);
textViewValues.setText(parsedValues [I]);
((的LinearLayout)convertView).addView(保持器);
}
返程((查看)convertView);
}
我不得不投我convertView到的LinearLayout因为刚刚converView没有过addView方法。好吧,这是一个狗屎,但它的工作原理......一些问题。现在,滚动时,好像在列表中增加因某种原因项目金额。有人可以解释我为什么会出现这种情况,如何解决?
完全正常的解决方案:从 JonasCz:
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
如果(convertView == NULL)
{
convertView = inflater.inflate(R.layout.parsed_csv_list_view_main_item,父母,假);
} 其他 {
((的LinearLayout)convertView).removeAllViews();
}
RealmResults< ParsedCSV>标题= parentFragment.getParsedCSVtitles();
。的String [] parsedTitles = titles.get(0).getValues()分裂();
的String [] parsedValues = items.get(位置).getValues()分裂(;)。
的for(int i = 0; I< parsedValues.length;我++){
查看支架= inflater.inflate(R.layout.parsed_csv_list_view_subitem,父母,假);
TextView的textViewTitles =(TextView中)holder.findViewById(R.id.parsed_csv_list_view_subitem_text_title);
TextView的textViewValues =(TextView中)holder.findViewById(R.id.parsed_csv_list_view_subitem_text_value);
textViewTitles.setText(parsedTitles [I]);
textViewValues.setText(parsedValues [I]);
((的LinearLayout)convertView).addView(保持器);
}
返程((查看)convertView);
}
在您的适配器的 getView()
方法,你夸大列表项的主要XML布局。然后,你应该通过你的数据(数组,指针,等等),并为您希望每个额外的LinearLayout,创建它,并添加TextViews,设置它们的文字,并将其添加到主体布局,从XML膨胀的循环。请参见这个答案。
如果您要重新使用从convertView参数(你应该做的事)的观点,你还必须确保你的主要布局是使用它,你可以用 myMainListItemLayout做之前空。 removeAllViews()
有一个不同的方法可能是把一个ListView您的列表视图中。 (可能不是一个好主意。)
编辑:试着改变你的code如下:
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
如果(convertView == NULL)
{
convertView = inflater.inflate(R.layout.parsed_csv_list_view_main_item,父母,假);
} 其他 {
((的LinearLayout)convertView).removeAllViews();
}
RealmResults< ParsedCSV>标题= parentFragment.getParsedCSVtitles();
。的String [] parsedTitles = titles.get(0).getValues()分裂();
的String [] parsedValues = items.get(位置).getValues()分裂(;)。
的for(int i = 0; I< parsedValues.length;我++){
查看支架= inflater.inflate(R.layout.parsed_csv_list_view_subitem,父母,假);
TextView的textViewTitles =(TextView中)holder.findViewById(R.id.parsed_csv_list_view_subitem_text_title);
TextView的textViewValues =(TextView中)holder.findViewById(R.id.parsed_csv_list_view_subitem_text_value);
textViewTitles.setText(parsedTitles [I]);
textViewValues.setText(parsedValues [I]);
((的LinearLayout)convertView).addView(保持器);
}
返程((查看)convertView);
}
这是丑陋的,而且很可能略慢,但它的工作原理。
I've got a quite irritating problem. My task is to add multiple LinearLayouts with custom XML template (And I don't know how many of Layouts there could be) with 2 TextViews inside (in each) into 1 ListView item.Like this:
Is it possible to do this in ArrayAdapter? Any help would be much appreciated!
Ok, i've managed to did something with help from Jonas Cz.And this is what I've got.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null)
{
convertView = inflater.inflate(R.layout.parsed_csv_list_view_main_item, parent, false);
}
RealmResults<ParsedCSV> titles = parentFragment.getParsedCSVtitles();
String[] parsedTitles = titles.get(0).getValues().split(";");
String[] parsedValues = items.get(position).getValues().split(";");
for (int i = 0; i < parsedValues.length; i++) {
View holder = inflater.inflate(R.layout.parsed_csv_list_view_subitem, parent, false);
TextView textViewTitles = (TextView) holder.findViewById(R.id.parsed_csv_list_view_subitem_text_title);
TextView textViewValues = (TextView) holder.findViewById(R.id.parsed_csv_list_view_subitem_text_value);
textViewTitles.setText(parsedTitles[i]);
textViewValues.setText(parsedValues[i]);
((LinearLayout) convertView).addView(holder);
}
return ((View)convertView);
}
I had to cast my convertView to LinearLayout because just converView didn't had addView method. Ok, it's a shit but it works...with some problem.Now, while scrolling, it seems like the amount of items in list increases for some reason. Can someone explain me why is this happening and how to fix it?
FULLY WORKING SOLUTION FROM JonasCz:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null)
{
convertView = inflater.inflate(R.layout.parsed_csv_list_view_main_item, parent, false);
} else {
((LinearLayout) convertView).removeAllViews();
}
RealmResults<ParsedCSV> titles = parentFragment.getParsedCSVtitles();
String[] parsedTitles = titles.get(0).getValues().split(";");
String[] parsedValues = items.get(position).getValues().split(";");
for (int i = 0; i < parsedValues.length; i++) {
View holder = inflater.inflate(R.layout.parsed_csv_list_view_subitem, parent, false);
TextView textViewTitles = (TextView) holder.findViewById(R.id.parsed_csv_list_view_subitem_text_title);
TextView textViewValues = (TextView) holder.findViewById(R.id.parsed_csv_list_view_subitem_text_value);
textViewTitles.setText(parsedTitles[i]);
textViewValues.setText(parsedValues[i]);
((LinearLayout) convertView).addView(holder);
}
return ((View)convertView);
}
In the getView()
method of your adapter, you inflate the main XML layout for the list item. Then you should loop through your data (Array, Cursor, whatever) and for each extra LinearLayout you want, create it, and add the TextViews, set their text, and add it to the main layout you inflated from XML. See this answer.
If you are re-using the view from the convertView parameter (which you should be doing), you must also ensure that your main layout is empty before using it, which you can do withmyMainListItemLayout.removeAllViews()
A different way might be to put a listView inside your listview. (probably not such a good idea.)
Edit: Try changing your code as follows:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null)
{
convertView = inflater.inflate(R.layout.parsed_csv_list_view_main_item, parent, false);
} else {
((LinearLayout) convertView).removeAllViews();
}
RealmResults<ParsedCSV> titles = parentFragment.getParsedCSVtitles();
String[] parsedTitles = titles.get(0).getValues().split(";");
String[] parsedValues = items.get(position).getValues().split(";");
for (int i = 0; i < parsedValues.length; i++) {
View holder = inflater.inflate(R.layout.parsed_csv_list_view_subitem, parent, false);
TextView textViewTitles = (TextView) holder.findViewById(R.id.parsed_csv_list_view_subitem_text_title);
TextView textViewValues = (TextView) holder.findViewById(R.id.parsed_csv_list_view_subitem_text_value);
textViewTitles.setText(parsedTitles[i]);
textViewValues.setText(parsedValues[i]);
((LinearLayout) convertView).addView(holder);
}
return ((View)convertView);
}
It's ugly, and probably slightly slow, but it works.
这篇关于如何将多个自定义linearLayouts添加到编程ListView项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!