如何通过具有不同参数的两个不同类访问一个类?

时间:2015-12-03 08:55:11

标签: android listview android-listview parameters

我想知道如何才能实现这样的目标。

在我的MainActivity中,我想调用 MyCustomBaseAdapter getCount函数。

MainActivity

public class MainActivity extends AppCompatActivity {

    InfoAPI sqlcon;
    private SimpleCursorAdapter dataAdapter;
    private SQLiteDatabase database;
    private MyDatabaseHelper dbHelper;
    private ListView listView;
    TextView txtNoResult;
    MyCustomBaseAdapter obj;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView=(ListView)findViewById(R.id.listView2);
        txtNoResult=(TextView)findViewById(R.id.textView);
        dbHelper = new MyDatabaseHelper(this);
        sqlcon = new InfoAPI(this);
       obj=new MyCustomBaseAdapter(this);  // here the error
        listView.setAdapter(obj);
        int totalItem=obj.getCount(); // call getCount in MyCustomBaseAdapter
        {
            if (totalItem== 0) {
                txtNoResult.setVisibility(View.GONE);
                BuildList();
            } else {
                txtNoResult.setVisibility(View.VISIBLE);
                listView.setVisibility(View.GONE);

            }

        }

在活动B中,我还需要访问 MyCustomBaseAdapter 。如您所见,我在活动B中添加了页脚页脚布局

活动B

 FrameLayout footerLayout;
 AbsoluteLayout footer;

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 listview = (ListView) findViewById(R.id.listView);

 footer = (AbsoluteLayout) getLayoutInflater().inflate(R.layout.total_hours, null);
        totalHours = (TextView) footer.findViewById(R.id.textView10);
        footerLayout = (FrameLayout) getLayoutInflater().inflate(R.layout.under_list_view_button, null);
        btnSubmit = (Button) footerLayout.findViewById(R.id.btnSave);
        btnAddClaims = (Button) footerLayout.findViewById(R.id.addClaims);
        objMyCustomBaseAdapter = new MyCustomBaseAdapter(getApplicationContext(), results, listview, footerLayout, footer);
        listview.setAdapter(objMyCustomBaseAdapter);

   btnSubmit.setOnClickListener(new View.OnClickListener() { // sumbit button is clicked
            public void onClick(View arg0) {
                Toast.makeText(getApplicationContext(), "Click", Toast.LENGTH_LONG).show();
                first=objMyCustomBaseAdapter.getFistTime();
                String[] first1 = first.split(":", 2);
                last=objMyCustomBaseAdapter.getLastTime();
                String[] last1=last.split(":",2);;
               long a=ts.insertTimeSheet(name, weather, date, status,first1[1] , last1[1]);
                WD.insertWorkDetails(results,a);
                Intent intent=new Intent(getApplicationContext(),MainActivity.class);
                startActivity(intent);


            }
        });
    }

最后这是 MyCustomBaseAdapter 类。

public class MyCustomBaseAdapter extends BaseAdapter{   // for ListView


        private static ArrayList<SearchResults> searchArrayList;

        FrameLayout footerLayout;
        private LayoutInflater mInflater;
        ListView listview;
       AbsoluteLayout footer;

        public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results,ListView listview,FrameLayout footerLayout,AbsoluteLayout footer) {
            searchArrayList = results;
            this.listview=listview;
            this.footerLayout=footerLayout;
            mInflater = LayoutInflater.from(context);
             this.footer=footer;
            addOrRemoveFooter();
        }

    public void addOrRemoveFooter(){
        if(searchArrayList.size() == 0 && listview.getFooterViewsCount() > 0){
           listview.removeFooterView(footer);
            listview.removeFooterView(footerLayout);
        }else if(listview.getFooterViewsCount() == 0 && searchArrayList.size()>0){
            listview.addFooterView(footer);
            listview.addFooterView(footerLayout);
        }
    }

    public int getCount() {
        return searchArrayList.size();
    }

       public String getFistTime() {
        SearchResults firstTime = this.searchArrayList.get(0);
        return firstTime.getTimeIn();
       }

    public String getLastTime() {
        SearchResults lastTime = this.searchArrayList.get(searchArrayList.size() - 1);
        return lastTime.getTimeOut();
    }

如何允许两个活动访问 MyCustomBaseAdapter ,因为参数不一样?请有人给我一些提示。谢谢

1 个答案:

答案 0 :(得分:0)

我建议将数组的大小保存在数据库中,并在每次从列表中添加或删除项目时进行更新。这样,您只需从活动中的数据库中获取值

即可