Изменение видимости ProgressBar внутри списка по нажатию кнопки
Мой listview
правильно заполняется с cursor adapter
, После нажатия кнопки сохранения в каждом ряду я начинаю intentservice
а по основной активности у меня BroadcastReceiver
который сообщает мне, что служба завершена, и теперь мы можем снова установить progressbar
visibility
чтобы пойти на этот конкретный ряд. Ниже приведен код, который я использую прямо сейчас. Работает но при прокрутке вижу еще одну progressbar
в каком-то другом ряду. то же самое касается setText
на кнопке. при нажатии кнопки "Сохранить" (кнопка) его текст изменился с сохранением, это нормально, но когда я прокручиваю вниз, я вижу, что то же самое произошло с другой строкой. Я думаю, это из-за позиции курсора.
* Пробовал с getview
в cursoradapter
(думал, что это может дать мне правильную позицию, но проблема остается той же. Это listview_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#b0b0b0"
android:dividerHeight="1dp"
android:drawSelectorOnTop="false"
android:padding="1dp"
android:paddingTop="1dp"
android:paddingBottom="1dp" />
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
Я использую этот макет строки для listview simple_quiz_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:visibility="visible"
android:theme="@style/AppTheme.WithActionBar">
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="@+id/quiz_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight=".70"
android:text="Quiz title will be here. If not please update app"
android:textColor="#000"
android:textSize="20dp"
android:layout_gravity="center_vertical" />
<Button
android:id="@+id/dld_quiz"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".30"
android:layout_centerInParent="true"
android:text="Save"
android:gravity="center_vertical|center_horizontal"
android:background="@android:drawable/dialog_holo_dark_frame"
android:textColor="#ffffff"
android:padding="0dp"
android:layout_margin="0dp" />
</LinearLayout>
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:visibility="gone"
android:layout_below="@+id/LinearLayout1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Это мой текущий курс
public class QuizListAdapter extends CursorAdapter implements View.OnClickListener {
private DBManager dbManager;
String baseuri="http://192.168.176.1";
String url;
Context context;
Cursor c;
ListView postList;
public boolean isNetworkAvailable(Activity c) {
boolean state;
ConnectivityManager cmg = (ConnectivityManager) c
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = cmg.getActiveNetworkInfo();
state = activeNetworkInfo != null && activeNetworkInfo.isConnected();
if (state) {
return true;
} else {
return false;
}
}
protected static class RowViewHolder {
public TextView mTitle;
public Button mButton;
public int position;
public ProgressBar ppBar;
}
public QuizListAdapter(Context context, Cursor c,ListView postList) {
super(context, c);
this.context = context;
this.c = c;
c.moveToFirst();
this.postList = postList;
}
@Override
public void bindView(final View view, final Context context, Cursor cursor) {
final RowViewHolder rowView = (RowViewHolder) view.getTag();
final String postID = cursor.getString(cursor.getColumnIndex("_id"));
rowView.mButton.setTag(postID);
final int pos = c.getPosition();
rowView.mButton.setText("Save "+pos);
final int cat = cursor.getInt(cursor.getColumnIndex("category"));
rowView.mTitle.setTag(postID);
rowView.mButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
dbManager = new DBManager(v.getContext());
dbManager.open();
if(!isNetworkAvailable((Activity)context)){
makeText(context,"No internet connection",Toast.LENGTH_SHORT).show();
return;
}
rowView.ppBar.setIndeterminate(true);
rowView.ppBar.setVisibility(View.VISIBLE);
rowView.mButton.setText("Saving "+pos);
rowView.mButton.setEnabled(false);
final String url2 = baseuri+"/address";
Intent intent= new Intent(v.getContext(),ItQuizService.class);
intent.putExtra("url", url2);
intent.putExtra("id", postID);
intent.putExtra("pos",pos);
intent.putExtra("cat",cat);
v.getContext().startService(intent);
Log.d("QuizListAdapter", "Service Fired !");
}
});
dbManager = new DBManager(view.getContext());
dbManager.open();
String lang;
if(!dbManager.getLang()) {
lang = "HI";
} else {
lang = "EN";
}
if(!dbManager.isEmpty("content",postID,lang)) {
rowView.mButton.setText("Update "+pos);
}
rowView.mTitle.setText(cursor.getString(cursor.getColumnIndex("title")));
}
@Override
public View newView(final Context context, Cursor cursor, final ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View calendarListView = inflater.inflate(R.layout.simple_quiz_list, null);
final RowViewHolder rowView = new RowViewHolder();
Typeface Eczar = Typeface.createFromAsset(context.getAssets(), "fonts/Eczar-Regular.ttf");
rowView.mTitle = (TextView)calendarListView.findViewById(R.id.quiz_title);
rowView.mTitle.setTypeface(Eczar);
rowView.mButton = (Button) calendarListView.findViewById(R.id.dld_quiz);
rowView.ppBar = (ProgressBar) calendarListView.findViewById(R.id.progressBar);
rowView.mTitle.setOnClickListener(titleOnClickListener);
calendarListView.setTag(rowView);
return calendarListView;
}
public void onClick(View view) {
// TODO Auto-generated method stub
makeText(view.getContext(),"hii",Toast.LENGTH_SHORT).show();
}
private View.OnClickListener titleOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String id = (String) v.getTag();
dbManager = new DBManager(v.getContext());
dbManager.open();
if(dbManager.isNull(id,"content",dbManager.lang())){
makeText(v.getContext(),"Download the quiz first",Toast.LENGTH_SHORT).show();
return;
}
Log.d("I Have PostID", " - " +id );
Intent intent = new Intent(v.getContext(), WebViewAdapter.class);
intent.putExtra("id", "" + id);
v.getContext().startActivity(intent);
}
};
}
ItQuizService.java
@Override
protected void onHandleIntent(Intent intent) {
dbManager = new DBManager(this);
dbManager.open();
final String id = intent.getStringExtra("id");
String Content1;
String url = intent.getStringExtra("url");
final int pos2 = intent.getIntExtra("pos",100);
final int cat = intent.getIntExtra("cat",27);
String Content2 = "Seems like something went wrong";
String lang = dbManager.lang();
String error = "";
//Will be doing some url connections here.
/*
* Creates a new Intent containing a Uri object
* BROADCAST_ACTION is a custom Intent action
*/
Intent broadcastIntent = new Intent();
broadcastIntent.setAction(MainActivity.ResponseReceiver.ACTION_RESP);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
broadcastIntent.putExtra("pos",pos2);
broadcastIntent.putExtra("cat",cat);
broadcastIntent.putExtra("id",id);
sendBroadcast(broadcastIntent);
this.stopSelf();
}
Mainactivity.java
public class ResponseReceiver extends BroadcastReceiver {
public static final String ACTION_RESP = "json.com.jsonparser.intent.action.MESSAGE_PROCESSED";
@Override
public void onReceive(Context context, Intent intent) {
final int cat = intent.getIntExtra("cat",1);
//ListView lv = (ListView) findViewById(R.id.postList);
//final int position = lv.getPositionForView((LinearLayout)v.getParent());
//final QuizListAdapter.RowViewHolder rowView = (QuizListAdapter.RowViewHolder) view.getTag();
final int pos = intent.getIntExtra("pos",1);
String ids = intent.getStringExtra("id");
Log.i("mymonoo recieved id ",ids);
//View vi = postList.getChildAt(pos);
View vi = postList.findViewWithTag(ids);
makeText(context,"Quiz Downloaded",Toast.LENGTH_SHORT).show();
//View v = getViewByPosition(pos,postList);
//ProgressBar quizprogress = (ProgressBar) vi.findViewById(R.id.progressBar);
//Button Qbutton = (Button) vi.findViewById(R.id.dld_quiz);
//if(quizprogress.isShown()){
// quizprogress.setVisibility(View.GONE);
//}
//Qbutton.setText("Saved");
//Qbutton.setEnabled(true);
}
}