How to access one class by two different classes with different parameters ?

旧街凉风 提交于 2019-12-13 08:15:17

问题


I wonder how can I achieve something like this.

In my MainActivity, I want to call MyCustomBaseAdapter, getCount function.

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);

            }

        }

In Activity B, I need to access MyCustomBaseAdapter as well. As you can see, I have added footer and footer layout in Activity B.

Activity 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);


            }
        });
    }

And finally this is MyCustomBaseAdapter class.

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();
    }

How can I allow two activities access MyCustomBaseAdapter since the parameter are not the same ? Someone give me a hints please. Thanks


回答1:


I would suggest saving the size of your array in a database and update everytime items are added or removed from the list. This way, you can simply get the value from the database in your activities



来源:https://stackoverflow.com/questions/34061544/how-to-access-one-class-by-two-different-classes-with-different-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!