问题描述
||
我正在尝试将LinearLayout中的两个textViews居中。这个LinearLayout嵌套在另一个带有ListView元素的对象中。
我认为我的XML非常正确。我在Adapterclass中动态填充textViews。
XML:
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"
android:layout_width=\"fill_parent\"
android:layout_height=\"fill_parent\" android:orientation=\"vertical\">
<LinearLayout
android:layout_width=\"fill_parent\"
android:layout_height=\"wrap_content\"
android:orientation=\"horizontal\"
android:gravity=\"center\">
<TextView android:layout_height=\"wrap_content\"
android:id=\"@+id/atlVacaturesnummer\"
android:layout_width=\"wrap_content\"
android:textColor=\"@color/Accent\"
android:text=\"x\"
/>
<TextView android:layout_height=\"wrap_content\"
android:id=\"@+id/atlVacatures\"
android:layout_width=\"wrap_content\"
android:text=\"y\"
/>
</LinearLayout>
<ListView android:id=\"@+id/android:list\"
android:layout_width=\"fill_parent\"
android:layout_height=\"fill_parent\"
android:layout_weight=\"1\"
android:drawSelectorOnTop=\"false\" />
<TextView android:layout_height=\"fill_parent\"
android:id=\"@+id/android:empty\"
android:layout_width=\"fill_parent\"
android:text=\"Er zijn geen jobs die voldoen aan uw criteria...\"
android:gravity=\"center\"/>
</LinearLayout>
适配器类:
/*
* Klasse VacatureAdapter
*/
private class VacatureAdapter extends ArrayAdapter<Vacature>{
private ArrayList<Vacature> vacatures;
public VacatureAdapter(Context context,int textViewResourceId,ArrayList<Vacature> vacatures){
super(context,textViewResourceId,vacatures);
this.vacatures = getArray();
//System.out.println(\"Array vacatureadapter: \" + v);
}
@Override
public View getView(int position,View convertview,ViewGroup parent){
View view = convertview;
if(view==null){
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.vacature_list_item,null);
//view.setBackgroundColor((position % 2) == 1? Color.LTGRAY: Color.WHITE);
}
TextView atlVacatures = (TextView)findViewById(R.id.atlVacatures);
TextView atlVacaturesnr = (TextView)findViewById(R.id.atlVacaturesnummer);
atlVacaturesnr.setText(\"\" + arrVacatures.size());
atlVacatures.setText(\" jobs op maat gevonden!\");
Vacature vaca = vacatures.get(position);
if(vaca != null){
TextView tvNaam = (TextView) view.findViewById(R.id.vacatureNaam);
TextView tvWerkveld = (TextView) view.findViewById(R.id.vacatureWerkveld);
TextView tvRegio = (TextView) view.findViewById(R.id.vacatureRegio);
if(tvNaam != null){
tvNaam.setText(vaca.getTitel());
if(tvWerkveld != null){
tvWerkveld.setText(\"Werkveld: \" + vaca.getWerkveld());
if(tvRegio!=null){
tvRegio.setText(\"Regio: \"+vaca.getRegio());
}
}
}
}
return view;
}
}
奇怪的是,如果我的微调器运行,他会正确显示在XML中设置的文本,如果微调器停止并填充我的ListView,它只会显示我的数字的一位,而当我滚动时,他会完全显示我的数字以及第二个TextView 。我不太明白这是怎么回事,也许有些代码需要放在其他地方?
解决方法
在关闭对话框后,我通过将setText代码放入Runnable方法解决了我的问题。现在看起来像这样:
private Runnable returnRes = new Runnable(){
public void run(){
if (arrVacatures.size() == 0){
dialog.dismiss();
}
if(arrVacatures!=null && arrVacatures.size() > 0){
adapter.notifyDataSetChanged();
for(int i= 0; i< arrVacatures.size();i++){
adapter.add(arrVacatures.get(i));
}
dialog.dismiss();
TextView atlVacatures = (TextView)findViewById(R.id.atlVacatures);
TextView atlVacaturesnr = (TextView)findViewById(R.id.atlVacaturesnummer);
atlVacaturesnr.setText(\"\" + arrVacatures.size());
atlVacatures.setText(\" jobs op maat gevonden!\");
adapter.notifyDataSetChanged();
}
}
};