UI对象& ProgressBar - 不更新AsyncTask

时间:2015-05-04 14:25:48

标签: android android-asynctask

大家好,我在下面添加了关于我的问题的所有代码。请注意,进度条和文本视图都不是通过asynctask更新。 textview可以在getview方法中编辑,但不能在进度条中编辑,因为当我尝试设置为40时,它不会将ui更新为40。

以下代码请注意请阅读评论!我尝试了很多不同的方法,但我仍然被困在这里

textview和ProgressBar目前都没有通过(onprogressupdated)asyncTask更新。

以下是官方android doc作为比较代码的引用: http://developer.android.com/reference/android/os/AsyncTask.html

这是我看到的另一个stackoverflow问题: Android Async Task Progress Bar onProgressUpdate

   public View getView(final int position, View convertView, ViewGroup parent) {
    View view = convertView;
        if (!(data.get(position) instanceof TemporarySongInfomation)) {
            SongViewHolder holder;
            if(view == null) {
                view = inflater.inflate(R.layout.music_list_format, null);
                holder = new SongViewHolder();
                holder.timesplayed = (TextView) view.findViewById(R.id.textView7);
                holder.artist = (TextView) view.findViewById(R.id.textView6);
                holder.title = (TextView) view.findViewById(R.id.textView5);
                holder.imagebutton = (ImageButton) view.findViewById(R.id.playbutton);
                holder.source = (TextView) view.findViewById(R.id.textView8);
            } else holder = (SongViewHolder)view.getTag();
            tempValue = (SongInfomation) data.get(position);
            String songName = tempValue.getName();
            holder.imagebutton.setBackgroundResource(R.drawable.playbutton1);
            holder.source.setText(tempValue.getVideoid());
            holder.title.setText(songName.length() > 45 ? songName.substring(0, 38) + "..." : songName);
            holder.timesplayed.setText("" + tempValue.getTimesplayed());
            holder.artist.setText(tempValue.getArtist());
            swipeDetector = new SwipeDetector();
            view.setOnClickListener(new SongListOnItemClickListener(position));
            view.setOnTouchListener(swipeDetector);
            holder.imagebutton.setOnClickListener(new OnPlayButtonClickListener(position));
        } else {
            TemporarySongViewHolder holder = new TemporarySongViewHolder();

            if(view == null) {
                view = inflater.inflate(R.layout.music_list_process_format, null);
                holder.artist = (TextView) view.findViewById(R.id.artisttemp);
                holder.bar = (ProgressBar) view.findViewById(R.id.progressBar2);
                holder.textpercent = (TextView) view.findViewById(R.id.textView9);
                holder.title = (TextView) view.findViewById(R.id.titletemp);
                holder.source = (TextView) view.findViewById(R.id.sourcetemp);
            } else holder = (TemporarySongViewHolder) view.getTag();
            tempValue1 = (TemporarySongInfomation) data.get(position);
            String songName = tempValue1.getName();
            holder.source.setText(tempValue1.getVideoid());
            holder.title.setText(songName.length() > 45 ? songName.substring(0, 38) + "..." : songName);
            holder.artist.setText(tempValue1.getArtist());
            holder.textpercent.setText("Initializing");
            new UpdateProgressBar(holder.bar, tempValue1, holder.textpercent).execute();

    }

    return view;
}



 class UpdateProgressBar extends AsyncTask<Void, Integer, Void> {
     TemporarySongInfomation songinfo;
     ProgressBar progress;
    TextView textpercent;

    UpdateProgressBar(ProgressBar bar, TemporarySongInfomation tp, TextView percent) {
        progress = bar;
        songinfo = tp;
        textpercent = percent;
    }

    @Override
    protected Void doInBackground(Void... params) {
        while (!songinfo.isCompleted()) {
            publishProgress((int) songinfo.getProgress());
            try {
                Thread.sleep(500);
            } catch (Exception e) {

            }
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        System.out.println("progress: "+values[0]);
        progress.setProgress(values[0]);
        textpercent.setText("Current Progress: "+values[0] + "%");
    }
}

XML根据需要也评论请阅读:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/titletemp"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:textSize="16dp"
    android:textColor="#ffffffff" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Small Text"
    android:id="@+id/artisttemp"
    android:layout_below="@+id/titletemp"
    android:layout_alignParentStart="true"
    android:textColor="#ffffffff"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Small Text"
    android:id="@+id/sourcetemp"
    android:layout_alignTop="@+id/artisttemp"
    android:layout_centerHorizontal="true"
    android:textColor="#ffffffff"/>

<ProgressBar
    android:layout_width="match_parent"
    android:layout_height="10dp"
    android:indeterminate="false"
    android:id="@+id/progressBar2"
    android:layout_below="@+id/sourcetemp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Small Text"
    android:id="@+id/textView9"
    android:textColor="#ffffffff"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/progressBar2" />

如要求:

 class SongViewHolder {
    TextView title, artist, timesplayed, source;
    ImageButton imagebutton;
}

class TemporarySongViewHolder {
    TextView title, artist, source, textpercent;
    ProgressBar bar;
}

1 个答案:

答案 0 :(得分:0)

正在寻找横向进度条......

此外,应使用getTag()来防止双重调用getview