I have a ListView which contains progressBars, I want to show and hide the progressBar inside a Thread, here is my code :
public void onClick(View v) {
if (v.getId() == R.id.ib_add_file) {
Intent intent = new Intent(this, FilePickerActivity.class);
startActivityForResult(intent, REQUEST_PICK_FILE);
}
}
When I pick a file it will be added to the ListView and start uploading :
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUEST_PICK_FILE:
if (data.hasExtra(FilePickerActivity.EXTRA_FILE_PATH)) {
File file = new File(data.getStringExtra(FilePickerActivity.EXTRA_FILE_PATH));
Fichier fichier = new Fichier();
fichier.setFile(file);
ArrayAdapter<Message> adapter = (ArrayAdapter<Message>) chat_list.getAdapter();
adapter.add(fichier);
int pos = adapter.getCount() - 1;
MessageHolder holder = (MessageHolder) getViewByPosition(pos, chat_list).getTag();
Log.i("Fichier", holder.fichier.getText().toString());
//the log is showing the right file name
holder.progress.setVisibility(View.VISIBLE);
//the progressBar isn't showing !
Log.i("ProgressBar", (holder.progress.getVisibility() == View.VISIBLE)? "Visible":"invisible");
//the log is showing Visible !!!
}
}
}
}
I'm using this function which i got from this answer to get the added row from the ListView to handle the ProgressBar:
public View getViewByPosition(int pos, ListView listView) {
final int firstListItemPosition = listView.getFirstVisiblePosition();
final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;
if (pos < firstListItemPosition || pos > lastListItemPosition) {
return listView.getAdapter().getView(pos, null, listView);
} else {
final int childIndex = pos - firstListItemPosition;
return listView.getChildAt(childIndex);
}
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
MessageHolder msgHolder = null;
Message msg = data.get(position);
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
msgHolder = new MessageHolder();
msgHolder.fichier = (TextView) row.findViewById(R.id.tv_fichier);
msgHolder.iconFichier = (ImageView) row.findViewById(R.id.iv_icon_fichier);
msgHolder.progress = (ProgressBar) row.findViewById(R.id.pb_fichier);
row.setTag(msgHolder);
//msgHolder.handler = new ProgressBarHandler(msgHolder.progress);
} else {
msgHolder = (MessageHolder) row.getTag();
}
Fichier fich = (Fichier) msg;
msgHolder.fichier.setText(fich.getNomFichier());
msgHolder.fichier.setPaintFlags(msgHolder.fichier.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
msgHolder.iconFichier.setImageResource(fichier.getIconFichier());
return row;
}
private static class MessageHolder {
ImageView iconFichier;
TextView fichier;
ProgressBar progress;
ProgressBarHandler handler;
}
And this is my xml for the the rows of the listview :
<ImageView
android:id="@+id/iv_icon_fichier"
style="@style/AvatarImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:contentDescription="@string/descThumbnail"
android:scaleType="centerCrop" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/iv_icon_fichier"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_fichier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="22sp"
android:text="Fichier" />
<ProgressBar
android:id="@+id/pb_fichier"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="100"
android:minWidth="200dp"
android:progress="0"
android:visibility="gone"/>
</LinearLayout>
Why my ProgressBar is not showing ?
Code Edited : I realized that my problem wasn't relater to the Handler, I tried showing the ProgressBar directly from my onActivityResult methode but it's not showing !