Android RemoteViews setViewVisibility无法正常工作

时间:2013-09-18 15:48:02

标签: android visibility android-appwidget remoteview homescreen

我正在处理主屏幕小部件中的问题,并尝试根据网络可用性和状态显示或隐藏按钮,因此如果连接,将显示按钮并加载内容,如果未连接,则将隐藏所有内容和显示消息。问题是remoteViews.setViewVisibility在所有情况下都没有按预期工作。这是我的代码:

public class WidgetProvider extends AppWidgetProvider {
    ArrayList<WidgetOffer> widgetOffers;

public static Cursor cOffer;
public static RemoteViews remoteViews;
public static String TAG  = "WidgetProvider";
public static String NEXT_OFFER_ACTION = "NEXT_OFFER_ACTION";
public static String PREV_OFFER_ACTION = "PREV_OFFER_ACTION";
public static String BOOK_OFFER_ACTION = "BOOK_OFFER_ACTION";

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
    if(isOnline(context)){
        Log.v(TAG, "On Update Connected");
        remoteViews.setViewVisibility(R.id.tvConnError, View.INVISIBLE);
        loadWidgetOffers lwo = new loadWidgetOffers(context);
        lwo.execute();
    }else{
        Log.v(TAG, "On Update Disconnected");

        remoteViews.setViewVisibility(R.id.right_arrow, View.INVISIBLE);
        remoteViews.setViewVisibility(R.id.left_arrow, View.INVISIBLE);
        remoteViews.setViewVisibility(R.id.btnBook, View.INVISIBLE);
        remoteViews.setViewVisibility(R.id.pbLoading, View.INVISIBLE);
        remoteViews.setViewVisibility(R.id.tvConnError, View.VISIBLE);

    }
    pushWidgetUpdate(context, remoteViews);
}

@Override
public void onDeleted(Context context, int[] appWidgetIds){
    Toast.makeText(context, "Widget Deleted", Toast.LENGTH_SHORT).show();
    super.onDeleted(context, appWidgetIds);
}

@Override
public void onDisabled(Context context){
    Toast.makeText(context, "Widget Disabled", Toast.LENGTH_SHORT).show();
    super.onDisabled(context);
}

@Override
public void onEnabled(Context context){
    Toast.makeText(context, "Widget Enabled", Toast.LENGTH_SHORT).show();
    Log.v(TAG, "On Enabled");

    super.onEnabled(context);
}

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);


}

public static PendingIntent NextOfferPI(Context context) {
    Intent intent = new Intent(context, TraveltodoWidgetIntentReceiver.class);
    intent.setAction(NEXT_OFFER_ACTION);
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

public static PendingIntent PrevOfferPI(Context context) {
    Intent intent = new Intent(context, TraveltodoWidgetIntentReceiver.class);
    intent.setAction(PREV_OFFER_ACTION);
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

public static PendingIntent BookOfferPI(Context context) {
    Intent intent = new Intent(context, TraveltodoWidgetIntentReceiver.class);
    intent.setAction(BOOK_OFFER_ACTION);
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

public static void pushWidgetUpdate(Context context, RemoteViews remoteviews) {
    ComponentName myWidget = new ComponentName(context, TraveltodoWidgetProvider.class);
    AppWidgetManager manager = AppWidgetManager.getInstance(context);
    manager.updateAppWidget(myWidget, remoteviews); 
    Log.v(TAG, "pushWidgetUpdate");
}

private class loadWidgetOffers extends AsyncTask<String, String, Boolean>{
    private Context c;

    public loadWidgetOffers(Context context){
        c = context;
    }

    @Override
    protected void onPreExecute(){
        Log.i("Parse Widget Offers", "Pre");
        DatabaseHandler db = new DatabaseHandler(c);
        db.resetTables();
        db.close();
    }

    @Override
    protected Boolean doInBackground(String... params) {
        Log.i("Parse Widget Offers", "Processing");
        try {
            widgetOffers= new ArrayList<WidgetOffer>();
            WidgetOfferParser wop = new WidgetOfferParser("http://server_name/newsClient2.xml");
            widgetOffers = wop.Parse();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    @Override
    protected void onPostExecute(final Boolean success) {

        if(success == true){
            Log.i(TAG, "Post Execute Success");
            SharedPreferences prefs = c.getSharedPreferences("com.package", Context.MODE_PRIVATE);
            DatabaseHandler db = new DatabaseHandler(c);
            Boolean offersExist = false;

            if(widgetOffers.size() > 0){
                prefs.edit().putInt("TotalOffers", widgetOffers.size()).commit();
                offersExist = true;
                for(int i = 0; i < widgetOffers.size(); i++){
                    WidgetOffer wo = widgetOffers.get(i);
                    db.addOffer(wo.getTitle(), wo.getDescription(), wo.getDetail(), wo.getOldPrice(), wo.getNewPrice(), wo.getIdOffer());
                }
            }

            if (offersExist){
                Log.i(TAG, "Post Execute Success With Offers");
                cOffer = db.getOffer("1");

                //RemoteViews remoteViews = new RemoteViews(c.getPackageName(), R.layout.widget_layout);
                remoteViews.setViewVisibility(R.id.tvConnError, View.INVISIBLE);
                remoteViews.setViewVisibility(R.id.pbLoading, View.INVISIBLE);

                remoteViews.setViewVisibility(R.id.right_arrow, View.VISIBLE);
                remoteViews.setViewVisibility(R.id.left_arrow, View.VISIBLE);
                remoteViews.setViewVisibility(R.id.btnBook, View.VISIBLE);

                remoteViews.setOnClickPendingIntent(R.id.right_arrow, TraveltodoWidgetProvider.NextOfferPI(c));
                remoteViews.setOnClickPendingIntent(R.id.left_arrow, TraveltodoWidgetProvider.PrevOfferPI(c));

                remoteViews.setTextViewText(R.id.tvWidTitle, cOffer.getString(1));
                remoteViews.setTextViewText(R.id.tvWidDescription, cOffer.getString(2));
                remoteViews.setTextViewText(R.id.tvPrice1, cOffer.getString(5));

                String cl = "some url";
                Intent myIntent = new Intent(Intent.ACTION_VIEW);
                myIntent.setData(Uri.parse(cl));
                PendingIntent pendIntent = PendingIntent.getActivity(c, 0, myIntent,0);
                remoteViews.setOnClickPendingIntent(R.id.btnBook, pendIntent);

                prefs.edit().putInt("CurrentOffer", 1).commit();
                prefs.edit().putString("CurrentLink", cl).commit();

                pushWidgetUpdate(c,remoteViews);
            }
        }else{
            Log.i(TAG, "Post Execute Fail");
            remoteViews.setViewVisibility(R.id.pbLoading, View.INVISIBLE);
            Toast.makeText(c, "Pas de connexion", Toast.LENGTH_SHORT).show();
            pushWidgetUpdate(c,remoteViews);
        }


    }
}

private boolean isOnline(Context c) {
    ConnectivityManager cm = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }else{
        return false;
    }
}

任何帮助将不胜感激 感谢

2 个答案:

答案 0 :(得分:0)

试试这个:

remoteViews.setInt(R.id.close, "setVisibility", View.GONE);

答案 1 :(得分:0)

如果“在所有情况下”没有按预期工作“意味着”元素变得不可见但仍然使用空间“,请尝试View.Gone而不是View.Invisible

remoteViews.setViewVisibility(R.id.right_arrow, View.GONE);