在标签中更新ListViews(Android应用程序)

时间:2011-04-21 22:22:09

标签: java android android-listview android-adapter

我正在开发一款Android应用程序来处理该领域技术人员的工作人员 首先让我解释一下我的应用程序的布局:
该应用程序的主要部分包含3个选项卡

标签1 - 主页标签
- 包含当前打开的工作人员的摘要

选项卡2 - 指定的工作单选项卡
- 包含当前分配给登录用户的工作单列表

选项卡3 - 未分配的工作单选项卡
- 包含分配给用户工作人员的工作人员列表,但不包含给特定人员

我使用一个名为“Database”的类,它是一个Singleton类,它是Application上下文的一部分,因此可以在整个应用程序中访问它。我将在下面加入一些代码

以下是问题:
当用户单击列表中的特定工作订单时,将启动工作订单详细信息活动,显示有关工作订单的各种信息。如果工作订单是“未分配”工作订单,它还会显示“取得所有权”按钮,将“所有者”字段更改为当前用户,将其从未分配的列表中删除,然后将其添加到该用户的指定列表中。但是,一旦用户点击后退按钮关闭工作订单详细信息屏幕(活动),然后返回到选项卡视图,列表就不会更新,直到您以某种方式强制视图刷新(例如在列表上向下滚动,以便刚改变所有权的工作单是在屏幕外。

我试过了:
- 将自定义ListAdapter移动到Database(singleton)类中并通过那里访问它 - 在列表适配器上调用“notifyDataSetChanged()”

有没有人有任何关于如何在每次选择标签时更新它的想法(或通过点击后退按钮解雇工作单细节屏幕)?我一直在寻找一个解决方案,并没有成功找到我找到的任何东西,所以我再次在这里发布,知道这里的大多数人都比我聪明!

提前感谢您的帮助!我在下面列出了相关代码:

数据库类:

public class Database extends Application {
    private static Database instance = null;    
    private ArrayList<Workorder> assignedwolist;
    private ArrayList<Workorder> unassignedwolist;
    private Credentials creds;
    private WOListAdapter assignedadapter;
    private WOListAdapter unassignedadapter;

    private static void checkInstance() {
        if(instance==null) {
            throw new IllegalStateException("Database class not created yet!");
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance=this;
    }

    public Database() {
        this.creds=null;
        this.assignedwolist = getAssignedWorkorders();
        this.unassignedwolist = getUnassignedWorkorders();
        this.assignedadapter=new WOListAdapter(this,getAssignedWorkorders());
        this.unassignedadapter=new WOListAdapter(this,getUnassignedWorkorders());
    }

    public ArrayList<String> getSummary() {
        int numassigned = assignedwolist.size();
        int assignedp1 = 0;
        for(Workorder wo:assignedwolist) {
            if(wo.getPriority().equalsIgnoreCase("1")) {
                assignedp1++;
            }
        }
        int numunassigned = unassignedwolist.size();
        int unassignedp1 = 0;
        for(Workorder wo:unassignedwolist) {
            if(wo.getPriority().equalsIgnoreCase("1")) {
                unassignedp1++;
            }
        }
        ArrayList<String> summary = new ArrayList<String>();
        summary.add(Integer.toString(numassigned));
        summary.add(Integer.toString(assignedp1));
        summary.add(Integer.toString(numunassigned));
        summary.add(Integer.toString(unassignedp1));
        return summary;
    }

    public Workorder getWO(String id) {
        for(Workorder wo:assignedwolist) {
            if(wo.getId().equalsIgnoreCase(id)) {
                return wo;
            }
        }
        for(Workorder wo:unassignedwolist) {
            if(wo.getId().equalsIgnoreCase(id)) {
                return wo;
            }
        }
        return null;
    }

    public ArrayList<Workorder> getAssignedWorkorders() {
        return assignedwolist;
    }

    public ArrayList<Workorder> getUnassignedWorkorders() {
        return unassignedwolist;
    }

    public void setValidated(boolean validated) {
        this.validated = validated;
    }

    public boolean isValidated() {
        return validated;
    }

    public void takeOwnership(String wonum) {
        Workorder wo;
        for(int i=0;i<unassignedwolist.size();i++) {
            if(unassignedwolist.get(i).getId().equalsIgnoreCase(wonum)) {
                wo = unassignedwolist.remove(i);
                wo.setOwner(creds.getUsername());
                assignedwolist.add(wo);
                Collections.sort(assignedwolist);
                return;
            }
        }
        this.assignedadapter.notifyDataSetChanged();
        this.unassignedadapter.notifyDataSetChanged();
    }

    public WOListAdapter getAssignedAdapter() {
        return assignedadapter;
    }

    public WOListAdapter getUnassignedAdapter() {
        return unassignedadapter;
    }
}

主应用程序(包含标签):

public class MainApp extends TabActivity {


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.tab);

        /*Tab host for tabs */
        TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);// The activity TabHost

        TabSpec hometabspec = tabHost.newTabSpec("Home");
        TabSpec assignedtabspec = tabHost.newTabSpec("Assigned");
        TabSpec unassignedtabspec = tabHost.newTabSpec("Unassigned");

        hometabspec.setIndicator("Home",getResources().getDrawable(R.drawable.ic_tab_home)).setContent(new Intent(this,HomeActivity.class));        
        assignedtabspec.setIndicator("Assigned",getResources().getDrawable(R.drawable.ic_tab_assigned)).setContent(new Intent(this,AssignedActivity.class));
        unassignedtabspec.setIndicator("Unassigned",getResources().getDrawable(R.drawable.ic_tab_unassigned)).setContent(new Intent(this,UnassignedActivity.class));

        tabHost.addTab(hometabspec);
        tabHost.addTab(assignedtabspec);
        tabHost.addTab(unassignedtabspec);
        tabHost.getTabWidget().setCurrentTab(0);
    }
}

AssignedActivity:

public class AssignedActivity extends Activity {


    /** Called when the activity is first created. */
    @Override   
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Database db = (Database)getApplicationContext();
        setContentView(R.layout.wolist);
        ListView listview =(ListView)findViewById(R.id.wolistview);
        listview.setTextFilterEnabled(false);

        WOListAdapter wolistadapter = db.getAssignedAdapter();
        listview.setAdapter(wolistadapter);
        listview.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterview, View view, int position,
                    long arg3) {
                Workorder wo = (Workorder)adapterview.getItemAtPosition(position);
                Intent i = new Intent();
                i.setClass(AssignedActivity.this,WorkorderDetailActivity.class);
                i.putExtra("wonum",wo.getId());
                startActivity(i);
            }
        });
    } 
}

UnassignedActivity:

public class UnassignedActivity extends Activity {


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Database db = (Database)getApplicationContext();
        setContentView(R.layout.wolist);
        ListView listview =(ListView)findViewById(R.id.wolistview);
        listview.setTextFilterEnabled(false);

        WOListAdapter wolistadapter = db.getUnassignedAdapter();
        listview.setAdapter(wolistadapter);
        listview.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterview, View view, int position,
                    long arg3) {
                Workorder wo = (Workorder)adapterview.getItemAtPosition(position);
                Intent i = new Intent();
                i.setClass(UnassignedActivity.this,WorkorderDetailActivity.class);
                i.putExtra("wonum",wo.getId());
                startActivity(i);
            }
        });
    }
}

WorkorderDetailActivity:

public class WorkorderDetailActivity extends Activity {
    /** Called when the activity is first created. */
    @Override   
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Database db = (Database)getApplicationContext();
        final Workorder wo;
        Bundle extras = getIntent().getExtras();
        if(extras!=null) {
            //Set Fields
            wo = db.getWO(extras.getString("wonum"));
            setContentView(R.layout.wodetail);
            TextView tv=(TextView)findViewById(R.id.wonumview);
            tv.setTextSize(20);
            tv.setText(wo.getId());
            tv=(TextView)findViewById(R.id.wodescriptionview);
            tv.setTextSize(18);
            tv.setText(wo.getDescription());
            tv=(TextView)findViewById(R.id.wolocationview);
            tv.setTextSize(18);
            tv.setText(wo.getLocation());
            tv=(TextView)findViewById(R.id.wostatusview);
            tv.setTextSize(18);
            tv.setText(wo.getStatus());
            tv=(TextView)findViewById(R.id.woreporteddateview);
            tv.setTextSize(18);
            tv.setText(wo.getReportdate().substring(0, 19));
            tv=(TextView)findViewById(R.id.wotypeview);
            tv.setTextSize(18);
            tv.setText(wo.getType());

            //Assigned or Unassigned adjustments
            tv=(TextView)findViewById(R.id.woownerview);
            Button b = (Button)findViewById(R.id.TakeOwnershipButton);
            if(wo.getOwner().equalsIgnoreCase("none")) { //Display TakeOwnership button
                b.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Database db = (Database)getApplicationContext();
                        db.takeOwnership(wo.getId());
                        Button b = (Button)findViewById(R.id.TakeOwnershipButton);
                        TextView tv=(TextView)findViewById(R.id.woownerview);
                        b.setVisibility(View.INVISIBLE);
                        tv.setVisibility(View.VISIBLE);
                        tv.setTextSize(18);
                        tv.setText(wo.getOwner());
                    }
                });
                b.setVisibility(View.VISIBLE);
                tv.setVisibility(View.INVISIBLE);
            } else { //Display owner
                b.setVisibility(View.INVISIBLE);
                tv.setVisibility(View.VISIBLE);
                tv.setTextSize(18);
                tv.setText(wo.getOwner());
            }

            //Icon color adjustment based on priority
            ImageView icon = (ImageView)findViewById(R.id.woiconview);
            switch(wo.getPriorityNum()) {
                case 1: icon.setColorFilter(Color.RED); break;
                case 2: icon.setColorFilter(Color.YELLOW); break;
                case 3: icon.setColorFilter(Color.GREEN); break;
                case 4: icon.setColorFilter(Color.BLUE); break;
                default: icon.setColorFilter(Color.TRANSPARENT); break;
            }

            //Status change options
            Spinner s = (Spinner)findViewById(R.id.wostatusselector);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, db.getValidStatuses(wo.getStatus()));
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            s.setAdapter(adapter);

        } else { //No wonum was passed in!
            TextView tv = new TextView(this);
            tv.setText("Error Retrieving Workorder");
            setContentView(tv);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

我使用setOnTabChanged()更新Google Analytics并从AdMob请求新添加:

tabHost.setOnTabChangedListener(new OnTabChangeListener(){
            public void onTabChanged(String tabId) {
                tracker.dispatch();
                adView.requestFreshAd();
            }});