在Android中刷新TabHost的活动

时间:2014-05-30 09:43:33

标签: android android-intent refresh android-tabhost

显然我有问题! 删除元素后,我需要刷新TabHost调用的内容(这是一个最喜欢的页面,我给了用户消除他想要的喜欢的可能性),我该怎么做? 以下是“收藏夹”页面的代码(行在可扩展列表视图中)。

    package it.sii.android.jobaroundu;


public class Preferiti  extends ExpandableListActivity{
    //Initialize variables
    private static final String STR_CHECKED = " has Checked!";
    private static final String STR_UNCHECKED = " has unChecked!";
    private int ParentClickStatus=-1;
    private int ChildClickStatus=-1;
    //aggiustare creando la classe annunci_parent e annunci_child
    private ArrayList<Parent> pref=new ArrayList<Parent>();



    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        MyDBHelper dbHelper = new MyDBHelper(this,"JobAroundU_DB", null, 1);
        SQLiteDatabase db=dbHelper.getWritableDatabase();

        Resources res = this.getResources();
        Drawable devider = res.getDrawable(R.drawable.line);
        // Set ExpandableListView values 
        getExpandableListView().setGroupIndicator(null);
        getExpandableListView().setDivider(devider);
        getExpandableListView().setChildDivider(devider);
        getExpandableListView().setDividerHeight(1);
        registerForContextMenu(getExpandableListView());

        //RECUPERO RICERCHE RECENTI 
        String sqlSavedPreferences="SELECT * FROM MyPreferences";

        Cursor cursor = db.rawQuery(sqlSavedPreferences, null);
        //creo padre e aggiungo ad esso le info
        while (cursor.moveToNext()){
            Parent p= new Parent();
            p.setPosizione(cursor.getString(1));
            p.setAzienza(cursor.getString(2));
            p.setId(cursor.getString(4));
            Log.i("Parent ID", p.getId());
            Child cp = new Child();
            cp.setDescrizione(cursor.getString(3));
            p.setChildren(new ArrayList<Child>());
            p.getChildren().add(cp);

            pref.add(p);         

        }
        db.close();
        cursor.close();

        //se parents e' vuoto --> messaggio errore --> non esistono preferiti salvati
        if (pref.isEmpty()) {
             LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));
                ImageView image = (ImageView) layout.findViewById(R.id.immagine);
                image.setImageDrawable(getResources().getDrawable(R.drawable.icon_25761));
                TextView text = (TextView) layout.findViewById(R.id.ToastTV);
                text.setText("Non ci sono preferiti da visualizzare!!!");

                Toast toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                toast.setDuration(Toast.LENGTH_SHORT);
                toast.setView(layout);
                toast.show();

                String n = getIntent().getStringExtra(getPackageName()+".Username");
                Intent i = new Intent(this, PersonalPage.class);
                i.putExtra(getPackageName()+".Username", n);
                startActivity(i);

        }

        if (this.getExpandableListAdapter() == null)        {
            //Create ExpandableListAdapter Object
            final MyExpandableListAdapter mAdapter = new MyExpandableListAdapter();
            // Set Adapter to ExpandableList Adapter
            this.setListAdapter(mAdapter);
        }
        else
        {
             // Refresh ExpandableListView data 
            ((MyExpandableListAdapter)getExpandableListAdapter()).notifyDataSetChanged();

        }

    }

    private class MyExpandableListAdapter extends BaseExpandableListAdapter{
            private LayoutInflater inflater;
            //protected SQLiteOpenHelper dbHelper2 =new MyDBHelper(PaginaRisultati.this,"JobAroundU_DB", null, 1);

            public MyExpandableListAdapter(){
                // Create Layout Inflator
                inflater = LayoutInflater.from(Preferiti.this);
            }


            // This Function used to inflate parent rows view

            @Override
            public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parentView){

                final Parent parent = pref.get(groupPosition);

                // Inflate grouprow.xml file for parent rows
                convertView = inflater.inflate(R.layout.grouprow_pref, parentView, false); 

                // Get grouprow.xml file elements and set values
                ((TextView) convertView.findViewById(R.id.text1)).setText(parent.getPosizione());
                ((TextView) convertView.findViewById(R.id.TVAzienda)).setText(parent.getAzienda());
                ImageView image=(ImageView)convertView.findViewById(R.id.image_tag);
                image.setImageResource(R.drawable.yellow_star);
                ImageButton bin = (ImageButton)convertView.findViewById(R.id.imageButton_bin);
                bin.setFocusable(false);


                bin.setOnClickListener(new OnClickListener(){

                    @Override
                    public void onClick(final View v) {

                        AlertDialog.Builder alt_bld = new AlertDialog.Builder(Preferiti.this);
                        alt_bld.setMessage("Sei sicuro di voler eliminare l'utente dai preferiti?")
                        .setCancelable(false)
                        .setPositiveButton("Si", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id)
                        {
                            MyDBHelper dbHelper2 = new MyDBHelper(Preferiti.this,"JobAroundU_DB", null, 1);
                            SQLiteDatabase db2=dbHelper2.getWritableDatabase();
                            String sqlDelete = "DELETE FROM MyPreferences WHERE idDBJobs="+parent.getId()+";";
                            db2.execSQL(sqlDelete);
                            db2.close();


                        }
                        })

                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                        //  Action for 'NO' Button
                            dialog.cancel();
                        }
                        });

                        AlertDialog alert = alt_bld.create();
                        // Title for AlertDialog
                        alert.setTitle("CANCELLARE QUESTO PREFERITO?");
                        // Icon for AlertDialog
                        alert.setIcon(R.drawable.trash_empty);
                        alert.show();


                        //elimino il preferito dal db
                        Log.i("DELETING", "I'm deleting   "+parent.getId()+"   Position:  "+parent.getPosizione());

                    }

                });
                return convertView;
            }


            // This Function used to inflate child rows view
            @Override
            public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parentView){
                final Parent parent = pref.get(groupPosition);
                final Child child = parent.getChildren().get(childPosition);

                // Inflate childrow.xml file for child rows
                convertView = inflater.inflate(R.layout.childrow_pref, parentView, false);

                // Get childrow.xml file elements and set values
                /***DESCRIZIONE***/
                ((TextView) convertView.findViewById(R.id.TVDescrizione)).setText(child.getDescrizione());
                ImageView image=(ImageView)convertView.findViewById(R.id.image_tag);
                image.setImageResource(R.drawable.icon_16301);


                return convertView;
}


@Override
public Object getChild(int groupPosition, int childPosition)
{
    //Log.i("Childs", groupPosition+"=  getChild =="+childPosition);
    return pref.get(groupPosition).getChildren().get(childPosition);
}

//Call when child row clicked
@Override
public long getChildId(int groupPosition, int childPosition)
{
    /****** When Child row clicked then this function call *******/

    if( ChildClickStatus!=childPosition)
    {
       ChildClickStatus = childPosition;

    }  

    return childPosition;
}

@Override
public int getChildrenCount(int groupPosition)
{
    int size=0;
    if(pref.get(groupPosition).getChildren()!=null)
        size = pref.get(groupPosition).getChildren().size();
    return size;
}


@Override
public Object getGroup(int groupPosition)
{
    Log.i("Parent", groupPosition+"=  getGroup ");

    return pref.get(groupPosition);
}

@Override
public int getGroupCount()
{
    return pref.size();
}

//Call when parent row clicked
@Override
public long getGroupId(int groupPosition)
{
    ParentClickStatus=groupPosition;
    if(ParentClickStatus==0)
        ParentClickStatus=-1;
    return groupPosition;
}

@Override
public void notifyDataSetChanged()
{
    // Refresh List rows
    super.notifyDataSetChanged();
}

@Override
public boolean isEmpty()
{
    return ((pref == null) || pref.isEmpty());
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
    return true;
}


/******************* Checkbox Checked Change Listener ********************/

private final class CheckUpdateListener implements OnCheckedChangeListener
{
    private final Parent parent;

    private CheckUpdateListener(Parent parent)
    {
        this.parent = parent;
    }
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        Log.i("onCheckedChanged", "isChecked: "+isChecked);
        parent.setChecked(isChecked);

        ((MyExpandableListAdapter)getExpandableListAdapter()).notifyDataSetChanged();

        final Boolean checked = parent.isChecked();
    }
}
/***********************************************************************/


@Override
public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return false;
}

}


}

这里是TabHost的代码。

package it.sii.android.jobaroundu;


public class Preferiti  extends ExpandableListActivity{
    //Initialize variables
    private static final String STR_CHECKED = " has Checked!";
    private static final String STR_UNCHECKED = " has unChecked!";
    private int ParentClickStatus=-1;
    private int ChildClickStatus=-1;
    //aggiustare creando la classe annunci_parent e annunci_child
    private ArrayList<Parent> pref=new ArrayList<Parent>();



    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        MyDBHelper dbHelper = new MyDBHelper(this,"JobAroundU_DB", null, 1);
        SQLiteDatabase db=dbHelper.getWritableDatabase();

        Resources res = this.getResources();
        Drawable devider = res.getDrawable(R.drawable.line);
        // Set ExpandableListView values 
        getExpandableListView().setGroupIndicator(null);
        getExpandableListView().setDivider(devider);
        getExpandableListView().setChildDivider(devider);
        getExpandableListView().setDividerHeight(1);
        registerForContextMenu(getExpandableListView());

        //RECUPERO RICERCHE RECENTI 
        String sqlSavedPreferences="SELECT * FROM MyPreferences";

        Cursor cursor = db.rawQuery(sqlSavedPreferences, null);
        //creo padre e aggiungo ad esso le info
        while (cursor.moveToNext()){
            Parent p= new Parent();
            p.setPosizione(cursor.getString(1));
            p.setAzienza(cursor.getString(2));
            p.setId(cursor.getString(4));
            Log.i("Parent ID", p.getId());
            Child cp = new Child();
            cp.setDescrizione(cursor.getString(3));
            p.setChildren(new ArrayList<Child>());
            p.getChildren().add(cp);

            pref.add(p);         

        }
        db.close();
        cursor.close();

        //se parents e' vuoto --> messaggio errore --> non esistono preferiti salvati
        if (pref.isEmpty()) {
             LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));
                ImageView image = (ImageView) layout.findViewById(R.id.immagine);
                image.setImageDrawable(getResources().getDrawable(R.drawable.icon_25761));
                TextView text = (TextView) layout.findViewById(R.id.ToastTV);
                text.setText("Non ci sono preferiti da visualizzare!!!");

                Toast toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                toast.setDuration(Toast.LENGTH_SHORT);
                toast.setView(layout);
                toast.show();

                String n = getIntent().getStringExtra(getPackageName()+".Username");
                Intent i = new Intent(this, PersonalPage.class);
                i.putExtra(getPackageName()+".Username", n);
                startActivity(i);

        }

        if (this.getExpandableListAdapter() == null)        {
            //Create ExpandableListAdapter Object
            final MyExpandableListAdapter mAdapter = new MyExpandableListAdapter();
            // Set Adapter to ExpandableList Adapter
            this.setListAdapter(mAdapter);
        }
        else
        {
             // Refresh ExpandableListView data 
            ((MyExpandableListAdapter)getExpandableListAdapter()).notifyDataSetChanged();

        }

    }

    private class MyExpandableListAdapter extends BaseExpandableListAdapter{
            private LayoutInflater inflater;
            //protected SQLiteOpenHelper dbHelper2 =new MyDBHelper(PaginaRisultati.this,"JobAroundU_DB", null, 1);

            public MyExpandableListAdapter(){
                // Create Layout Inflator
                inflater = LayoutInflater.from(Preferiti.this);
            }


            // This Function used to inflate parent rows view

            @Override
            public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parentView){

                final Parent parent = pref.get(groupPosition);

                // Inflate grouprow.xml file for parent rows
                convertView = inflater.inflate(R.layout.grouprow_pref, parentView, false); 

                // Get grouprow.xml file elements and set values
                ((TextView) convertView.findViewById(R.id.text1)).setText(parent.getPosizione());
                ((TextView) convertView.findViewById(R.id.TVAzienda)).setText(parent.getAzienda());
                ImageView image=(ImageView)convertView.findViewById(R.id.image_tag);
                image.setImageResource(R.drawable.yellow_star);
                ImageButton bin = (ImageButton)convertView.findViewById(R.id.imageButton_bin);
                bin.setFocusable(false);


                bin.setOnClickListener(new OnClickListener(){

                    @Override
                    public void onClick(final View v) {

                        AlertDialog.Builder alt_bld = new AlertDialog.Builder(Preferiti.this);
                        alt_bld.setMessage("Sei sicuro di voler eliminare l'utente dai preferiti?")
                        .setCancelable(false)
                        .setPositiveButton("Si", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id)
                        {
                            MyDBHelper dbHelper2 = new MyDBHelper(Preferiti.this,"JobAroundU_DB", null, 1);
                            SQLiteDatabase db2=dbHelper2.getWritableDatabase();
                            String sqlDelete = "DELETE FROM MyPreferences WHERE idDBJobs="+parent.getId()+";";
                            db2.execSQL(sqlDelete);
                            db2.close();


                        }
                        })

                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                        //  Action for 'NO' Button
                            dialog.cancel();
                        }
                        });

                        AlertDialog alert = alt_bld.create();
                        // Title for AlertDialog
                        alert.setTitle("CANCELLARE QUESTO PREFERITO?");
                        // Icon for AlertDialog
                        alert.setIcon(R.drawable.trash_empty);
                        alert.show();


                        //elimino il preferito dal db
                        Log.i("DELETING", "I'm deleting   "+parent.getId()+"   Position:  "+parent.getPosizione());

                    }

                });
                return convertView;
            }


            // This Function used to inflate child rows view
            @Override
            public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parentView){
                final Parent parent = pref.get(groupPosition);
                final Child child = parent.getChildren().get(childPosition);

                // Inflate childrow.xml file for child rows
                convertView = inflater.inflate(R.layout.childrow_pref, parentView, false);

                // Get childrow.xml file elements and set values
                /***DESCRIZIONE***/
                ((TextView) convertView.findViewById(R.id.TVDescrizione)).setText(child.getDescrizione());
                ImageView image=(ImageView)convertView.findViewById(R.id.image_tag);
                image.setImageResource(R.drawable.icon_16301);


                return convertView;
}


@Override
public Object getChild(int groupPosition, int childPosition)
{
    //Log.i("Childs", groupPosition+"=  getChild =="+childPosition);
    return pref.get(groupPosition).getChildren().get(childPosition);
}

//Call when child row clicked
@Override
public long getChildId(int groupPosition, int childPosition)
{
    /****** When Child row clicked then this function call *******/

    if( ChildClickStatus!=childPosition)
    {
       ChildClickStatus = childPosition;

    }  

    return childPosition;
}

@Override
public int getChildrenCount(int groupPosition)
{
    int size=0;
    if(pref.get(groupPosition).getChildren()!=null)
        size = pref.get(groupPosition).getChildren().size();
    return size;
}


@Override
public Object getGroup(int groupPosition)
{
    Log.i("Parent", groupPosition+"=  getGroup ");

    return pref.get(groupPosition);
}

@Override
public int getGroupCount()
{
    return pref.size();
}

//Call when parent row clicked
@Override
public long getGroupId(int groupPosition)
{
    ParentClickStatus=groupPosition;
    if(ParentClickStatus==0)
        ParentClickStatus=-1;
    return groupPosition;
}

@Override
public void notifyDataSetChanged()
{
    // Refresh List rows
    super.notifyDataSetChanged();
}

@Override
public boolean isEmpty()
{
    return ((pref == null) || pref.isEmpty());
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
    return true;
}


/******************* Checkbox Checked Change Listener ********************/

private final class CheckUpdateListener implements OnCheckedChangeListener
{
    private final Parent parent;

    private CheckUpdateListener(Parent parent)
    {
        this.parent = parent;
    }
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        Log.i("onCheckedChanged", "isChecked: "+isChecked);
        parent.setChecked(isChecked);

        ((MyExpandableListAdapter)getExpandableListAdapter()).notifyDataSetChanged();

        final Boolean checked = parent.isChecked();
    }
}
/***********************************************************************/


@Override
public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return false;
}

}


}

1 个答案:

答案 0 :(得分:0)

试试这个:

         pref.clear();

         MyDBHelper dbHelper = new MyDBHelper(this,"JobAroundU_DB", null, 1);
         SQLiteDatabase db=dbHelper.getWritableDatabase();

         String sqlSavedPreferences="SELECT * FROM MyPreferences";

         Cursor cursor = db.rawQuery(sqlSavedPreferences, null);

         while (cursor.moveToNext()){

             Parent p= new Parent();
             p.setPosizione(cursor.getString(1));
             p.setAzienza(cursor.getString(2));
             p.setId(cursor.getString(4));
             Log.i("Parent ID", p.getId());
             Child cp = new Child();
             cp.setDescrizione(cursor.getString(3));
             p.setChildren(new ArrayList<Child>());
             p.getChildren().add(cp);

             pref.add(p);         

        }
        db.close();
        cursor.close();

         // Refresh ExpandableListView data 
        ((MyExpandableListAdapter)getExpandableListAdapter()).notifyDataSetChanged();

将上述代码添加到您的删除方法中,它应该可以正常工作。