在LinearLayout中居中2个文本视图,滚动时仅显示文本

时间:2011-04-13 07:59:27

标签: android listview scroll textview

我正在尝试将两个位于LinearLayout中的textView集中在一起。此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>    

Adapterclass:

/*
 * 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。我不太明白什么是错的,也许有些代码需要放在其他地方?

1 个答案:

答案 0 :(得分:1)

我解除了我的问题后,将我的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();
        }
    }
};