简体   繁体   中英

splitting a too large method in android

I am a beginner and I am creating an android app in java with android studio. it is a ListView with more than 2000 items. When the user clicks on an item, he will be taken to a certain map location. when I run the app, it says code is too large because of the large method,.. I have read almost every post about this subject and they all suggest that I should split the method into smaller methods. or add another class or (.file) to my recourses. So I know my problem is writing too many (if statement) lines inside my method because I am a beginner and I did them manually. Could you Please show me a detailed step by step guide to solve "code is too large". Thanks a lot

 
    package com.example.myapplicationsecond;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;

import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.example.myapplicationsecond.R;

public class MainActivity9 extends AppCompatActivity {

    ListView listView;

    String[] name = {

            "first location ",
            "second location",
            "third location",
            "fourth location",
            "fifth location",
            "sixth location",
until 2000th location

ArrayAdapter<String> arrayAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main9);
    View view = getLayoutInflater().inflate(R.layout.abs_layout, null);
    ActionBar.LayoutParams params = new ActionBar.LayoutParams(
            ActionBar.LayoutParams.WRAP_CONTENT,
            ActionBar.LayoutParams.MATCH_PARENT,
            Gravity.CENTER);

    TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
    Title.setText("search here");

    getSupportActionBar().setCustomView(view,params);
    getSupportActionBar().setDisplayShowCustomEnabled(true); //show custom title
    getSupportActionBar().setDisplayShowTitleEnabled(false); //hide the default title


    getSupportActionBar().setTitle("search here");


    listView = findViewById(R.id.listview);

    arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,name);
    listView.setAdapter(arrayAdapter);


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


            String clickedText = arrayAdapter.getItem(position);

            int positionInArray = java.util.Arrays.asList(name).indexOf(clickedText);


            if(positionInArray==0){

                Intent intent = new Intent(Intent.ACTION_VIEW , Uri.parse("http://maps.google.com/maps?saddr&daddr= 21.422503, 39.826179"));
                startActivity(intent);


            }
            if(positionInArray==1){

                Intent intent = new Intent(Intent.ACTION_VIEW , Uri.parse("http://maps.google.com/maps?saddr&daddr= 24.468333, 39.610833"));
                startActivity(intent);

            }
            if(positionInArray==2){

                Intent intent = new Intent(Intent.ACTION_VIEW , Uri.parse("http://maps.google.com/maps?saddr&daddr= N 26 23.026 E 44 59.378"));
                startActivity(intent);


            }
            if(positionInArray==3){

                Intent intent = new Intent(Intent.ACTION_VIEW , Uri.parse("http://maps.google.com/maps?saddr&daddr= 26.403737, 44.912791"));
                startActivity(intent);


            }
            if(positionInArray==4){

                Intent intent = new Intent(Intent.ACTION_VIEW , Uri.parse("http://maps.google.com/maps?saddr&daddr= N 26 36.698 E 47 34.159"));
                startActivity(intent);


            }
            if(positionInArray==5){

                Intent intent = new Intent(Intent.ACTION_VIEW , Uri.parse("http://maps.google.com/maps?saddr&daddr= N 26 05.760 E 46 29.135"));
                startActivity(intent);

until 2000th line

As you're a beginner it's understandable that this is all very overwhelming so here's a few pointers to help you along. This is not a complete solution nor is it what would be widely considered to be best practice but it will help move you forwards from where you are. I hope you take something from it.

Firstly create a class to represent objects that hold information about your locations. ie their names and latLon.

class Location {
    private String name;
    private String latLon;

    public Location(String name, String latLon) {
        this.name = name;
        this.latLon = latLon;
    }

    public String getName() {
        return name;
    }

    public String getLatLon() {
        return latLon;
    }
}

You can then replace your name array with an array of these items like so

Location[] locations = {
        new Location("first location", "24.468333, 39.610833"),
        new Location("second location", "24.468333, 39.610833"),
        new Location("third location", "24.468333, 39.610833"),
        new Location("fourth location", "24.468333, 39.610833")
        // and so on
};

Now you need a way of extracting a list of the location names to display in your ListView . For that you can use the below function

private List<String> getNamesFromLocations(Location[] locations) {
    // creates a new ArrayList to hold the names of locations
    ArrayList<String> locationNames = new ArrayList<>();
    // this iterates through the array one item at a time
    for (Location location : locations) {
        // get the location name and add it to the new list defined in this function
        locationNames.add(location.getName());
    }
    // return the location names
    return locationNames;
}

You can now use this function to create your list of location names and use it to initialise the ArrayAdapter

List<String> names = getNamesFromLocations(locations);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, names);

In the listView onItemClick function you can take the position of the item that was clicked and use that to grab the location at the same position in the locations array

Location location = locations[position];
String latLon = location.getLatLon();

Then you can make the code that creates your Intent cleaner by combining this String (which is the same in each of your if statements) "http://maps.google.com/maps?saddr&daddr = " with the latLon specific to you location

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?saddr&daddr = " + latLon));
startActivity(intent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM