2015-05-04 76 views
0

嗨,大家好,我在下面添加了關於我的問題的所有代碼。請注意,進度欄和文本視圖均未通過asynctask進行更新。 textview可以在getview方法中編輯,但不能在進度條中編輯,因爲當我嘗試將其設置爲40時,它不會將ui更新爲40.UI Objects&ProgressBar - 不更新AsyncTask

代碼下面是請注意閱讀評論!我嘗試了很多不同的方法,但我仍然堅持在這裏

textview和ProgressBar都沒有通過(onprogressupdated)asyncTask目前更新。

下面是引用官方Android文檔作爲比較代碼: http://developer.android.com/reference/android/os/AsyncTask.html

這裏是我看着另一計算器問題: 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; 
} 
+0

的兩次調用是進入while循環? –

+0

是的,但根據android文檔publishProgress可以發生在一個循環內,例如,如果你看他們使用forloop –

+0

不,我問你,當你運行它進入裏面? –

回答

0

一直在尋找一個水平進度條...

此外,應使用getTag()來防止getview