0

我开发了一个带有自定义列表视图的应用程序,它工作正常,但在滚动数据更改布局后。

我的列表视图布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/layout_start_lesson"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textlesson"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal"
            android:padding="8dp"
            android:text="Medium Text"
            android:textSize="18dp"
            android:textStyle="bold" />

        <View
            android:id="@+id/view1"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="#E0E0E0" />
    </LinearLayout>

    <!-- Layout Progress -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textViewProgress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical|center_horizontal"
            android:padding="5dp"
            android:text="@string/progress"
            android:textSize="16dp" />

        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical|center_horizontal"
            android:layout_weight="0.5"
            android:max="100"
            android:progress="0"
            android:progressDrawable="@drawable/progressbar_style" />

        <TextView
            android:id="@+id/textProgress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:paddingRight="10dp"
            android:text="Medium Text"
            android:textSize="16dp" />
    </LinearLayout>

    <!-- Layout Test Liv 1-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textViewTest"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical|center_horizontal"
            android:padding="5dp"
            android:text="@string/testLiv1"
            android:textSize="16dp" />

        <ProgressBar
            android:id="@+id/progressBarTest"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical|center_horizontal"
            android:layout_weight="0.5"
            android:max="100"
            android:progress="0"
            android:progressDrawable="@drawable/progressbar_style" />

        <TextView
            android:id="@+id/textPercentTest"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:paddingRight="10dp"
            android:text="Medium Text"
            android:textSize="16dp" />
    </LinearLayout>

    <!-- Layout Test Liv 2-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textViewTestLiv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical|center_horizontal"
            android:padding="5dp"
            android:text="@string/testLiv2"
            android:textSize="16dp" />

        <ProgressBar
            android:id="@+id/progressBarTestLiv2"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical|center_horizontal"
            android:layout_weight="0.5"
            android:max="100"
            android:progress="0"
            android:progressDrawable="@drawable/progressbar_style" />

        <TextView
            android:id="@+id/textPercentTestLiv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:paddingRight="10dp"
            android:text="Medium Text"
            android:textSize="16dp" />
    </LinearLayout>

</LinearLayout>

这是我的声明:

StartLessonAdapter adapter = new StartLessonAdapter(datalist);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() { .... }

我的惊吓适配器类:

public View getView(int position, View view, ViewGroup parent) {
        final StartLessonTestBean entry = (StartLessonTestBean) languageBean
                .get(position);

        int main = R.layout.layout_start_lesson;

        final ViewHolderLanguage holder;

        View convertView = null;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(main, null);
        } else {
            convertView = view;
        }

        holder = new ViewHolder();
        holder.txtLesson = (TextView) convertView.findViewById(R.id.textlesson);
        holder.txtLesson.setText(entry.getLesson().trim());


        // ---- START Progress -------
        holder.progressBarTraining = (ProgressBar) convertView
                .findViewById(R.id.progressBar);
        holder.progressBarTraining.setMax(100);
        holder.progressBarTraining.setProgress(entry.getProgressTraining());

        holder.textProgressTraining = (TextView) convertView
                .findViewById(R.id.textProgress);
        String pr = entry.getTxtProgressTraining().trim(); 
        holder.textProgressTraining.setText(pr);
        // ----- END Progress

        // ---- START Test Liv 1 -------
        holder.progressTestLiv1Bar = (ProgressBar) convertView
                .findViewById(R.id.progressBarTest);
        holder.progressTestLiv1Bar.setMax(100);
        holder.progressTestLiv1Bar.setProgressDrawable(convertView
                .getResources().getDrawable(R.drawable.progressbartest_style));
        holder.progressTestLiv1Bar.setProgress(entry.getProgressTestLiv1Ok());
        holder.progressTestLiv1Bar.setSecondaryProgress(entry
                .getProgressTestLiv1NotOk());

        holder.textTestLiv1Progress = (TextView) convertView
                .findViewById(R.id.textPercentTest);
        pr = entry.getTxtProgressTestLiv1().trim(); 
        holder.textTestLiv1Progress.setText(pr);
        // ----- END Test Liv 1

        // ---- START Test Liv 2 -------
        holder.progressTestLiv2Bar = (ProgressBar) convertView
                .findViewById(R.id.progressBarTestLiv2);
        holder.progressTestLiv2Bar.setMax(100);
        holder.progressTestLiv2Bar.setProgressDrawable(convertView
                .getResources().getDrawable(R.drawable.progressbartest_style)); 
        holder.progressTestLiv2Bar.setProgress(entry.getProgressTestLiv2Ok());
        holder.progressTestLiv2Bar.setSecondaryProgress(entry
                .getProgressTestLiv2NotOk());

        holder.textTestLiv2Progress = (TextView) convertView
                .findViewById(R.id.textPercentTestLiv2);
        pr = entry.getTxtProgressTestLiv2().trim(); 
        holder.textTestLiv2Progress.setText(pr);
        // ----- END Test Liv 2

        holder.position = position;

        return convertView;
    }

public int getCount() {
        return languageBean.size();
    }

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        return position;
    }

为什么滚动后数据发生了变化?在布局中,我有 3 个文本视图和 3 个进度条,

滚动后我只有 3 个文本视图和 1 个进度条

4

2 回答 2

0
 public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        //return null;  Return Here Object Or Something like
        retrun arraylist[arg0]
    }

不确定可能会解决您的问题,但请尝试

于 2013-05-27T13:08:46.097 回答
0

仅当视图为空时才应使用 findviewbyid。您可以用这个替换您的 getView 方法。它现在应该可以工作了。

public View getView(int position, View view, ViewGroup parent) 
      {

StartLessonTestBean entry = (StartLessonTestBean) languageBean.get(position);
        int main = R.layout.layout_start_lesson;
        ViewHolderLanguage holder = null;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (view == null) {

            holder = new ViewHolderLanguage();
            view = inflater.inflate(main, null);
            holder.txtLesson = (TextView) convertView.findViewById(R.id.textlesson);
            holder.progressBarTraining = (ProgressBar) view.findViewById(R.id.progressBar);
            holder.textProgressTraining = (TextView) view.findViewById(R.id.textProgress);
            holder.progressTestLiv1Bar = (ProgressBar) view.findViewById(R.id.progressBarTest);
            holder.textTestLiv1Progress = (TextView) view.findViewById(R.id.textPercentTest);
            holder.progressTestLiv2Bar = (ProgressBar) view.findViewById(R.id.progressBarTestLiv2);
            holder.textTestLiv2Progress = (TextView) view.findViewById(R.id.textPercentTestLiv2);
            view.setTag(holder);
        } else {
            holder = (ViewHolderLanguage)view.getTag();
        }

        holder.txtLesson.setText(entry.getLesson().trim());


        holder.progressBarTraining.setMax(100);
        holder.progressBarTraining.setProgress(entry.getProgressTraining());

        String pr = entry.getTxtProgressTraining().trim(); 
        holder.textProgressTraining.setText(pr);


        holder.progressTestLiv1Bar.setMax(100);
        holder.progressTestLiv1Bar.setProgressDrawable(view.getResources().getDrawable(R.drawable.progressbartest_style));
        holder.progressTestLiv1Bar.setProgress(entry.getProgressTestLiv1Ok());
        holder.progressTestLiv1Bar.setSecondaryProgress(entry.getProgressTestLiv1NotOk());

        pr = entry.getTxtProgressTestLiv1().trim(); 
        holder.textTestLiv1Progress.setText(pr);


        holder.progressTestLiv2Bar.setMax(100);
        holder.progressTestLiv2Bar.setProgressDrawable(view.getResources().getDrawable(R.drawable.progressbartest_style)); 
        holder.progressTestLiv2Bar.setProgress(entry.getProgressTestLiv2Ok());
        holder.progressTestLiv2Bar.setSecondaryProgress(entry.getProgressTestLiv2NotOk());

        pr = entry.getTxtProgressTestLiv2().trim(); 
        holder.textTestLiv2Progress.setText(pr);

        return view;
    }
于 2013-05-27T13:50:11.860 回答