2
\$\begingroup\$

I am doing an android side project, and I was wondering if I could have my code reviewed to get feedback on improving its efficiency and overall elegance. Particular classes to focus on would be Dashboard, the two fragment classes.

Here is Dashboard.java, and the rest of my code on GitHub.

 public  class Dashboard extends AppCompatActivity
            implements NavigationView.OnNavigationItemSelectedListener, AddTaskPgOne.OnFragmentInteractionListener , AddTaskPgTwo.FragTwoInterface{
    private Context context;
    private FloatingActionButton addTask;
    private UserDB userDB;
    private TaskDB taskDB;
    private static ArrayList<Task> tasks;
    private  static  RecyclerView mRecyclerView;
    private  static RecyclerView.Adapter mAdapter;
    private ArrayList<String> inputData;
    private  static LinearLayoutManager mLayoutManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dash_board);
        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        mRecyclerView.setHasFixedSize(true);
         inputData=new ArrayList<>();
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        context=this;
        addTask=(FloatingActionButton)findViewById(R.id.addfab);

        userDB=new UserDB();
        taskDB= new TaskDB() ;
        tasks=new ArrayList<>();
        addTask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startFragment("first");
            }
        });


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        taskDB.getData(this);
  //      feed = (ListView) findViewById(R.id.feedlist);
        mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mAdapter = new CardAdapter(tasks);
        mRecyclerView.setAdapter(mAdapter);



        TextView textView = new TextView(context);
        textView.setText("Recent Tasks");
        textView.setTextSize(25);

//        feed.addHeaderView(textView);
//
//        feed.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position,
//                                    long id) {
//
//
//            }
//        });


    }


    public ArrayList<Task> getTasks() {
        return tasks;
    }


    public static void setTask(ArrayList<Task> t){
        tasks=t;
    }


    @Override
    public void onBackPressed() {
        if(tasks.size()>0) {
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
            } else {
                super.onBackPressed();
            }
        }
    }

    public static void  populatefeed(ArrayList<Task> t){

        Collections.reverse(t);
        mAdapter=new CardAdapter(t);
        mRecyclerView.setAdapter(mAdapter);

//        adapter.result= t;
//        feed.invalidateViews();
//        feed.setAdapter(adapter);


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.dash_board, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 1) {
            if(resultCode == Activity.RESULT_OK){
                Bundle b= data.getExtras();


                String taskString=b.getString("task");
                Uri uri=Uri.parse(b.getString("uri"));
                List<String> items = Arrays.asList(taskString.split("\\s*,\\s*"));
                //Task task=new Task(items.get(0),items.get(1),items.get(2),items.get(3),items.get(4),items.get(5),items.get(6));

                if(URLUtil.isValidUrl(uri.toString())){
                    Log.i("here", uri.getEncodedPath());
                }
               // taskDB.addToDB(task);
                taskDB.getData(getApplicationContext());
            }

        }
    }//onActivityResult


    void startFragment(String label){
        switch (label){
            case "first":
                FragmentManager createTaskOne=getSupportFragmentManager();
                AddTaskPgOne addOne=new AddTaskPgOne();
                addOne.show(createTaskOne,label);
                break;
            case "second":
                FragmentManager createTaskTwo=getSupportFragmentManager();
                AddTaskPgTwo addTwo=new AddTaskPgTwo();
                //Bundle b=new Bundle();
                //b.putStringArrayList("first",inputData);
                addTwo.show(createTaskTwo,label);
                break;
        }

    }

    @Override
    public void dataTransfer(String[] data) {

        startFragment("second");
        inputData.addAll(Arrays.asList(data));

    }

    @Override
    public void onFragTwoInteraction(String[] data) {
        inputData.addAll(Arrays.asList(data));
        Log.i("done",inputData.toString());

    }
}
\$\endgroup\$
  • \$\begingroup\$ anyone willing to take a look?? \$\endgroup\$ – Jacob Abraham Mar 7 '17 at 19:38
  • \$\begingroup\$ Overall elegance of what? Code formatting? Cleverness to display 2 different fragments? Elegance of current solution? \$\endgroup\$ – TerNovi Jul 21 '17 at 4:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.