Android Studio Java项目解决「代码过大」问题的分步指南
分步解决「方法体积过大」问题
步骤1:创建数据模型类
把每个列表项的核心信息(名称、纬度、经度)封装成独立Java类,彻底替代硬编码的if判断逻辑:
public class Place { private String name; private double latitude; private double longitude; public Place(String name, double latitude, double longitude) { this.name = name; this.latitude = latitude; this.longitude = longitude; } // Getter方法 public String getName() { return name; } public double getLatitude() { return latitude; } public double getLongitude() { return longitude; } }
步骤2:迁移2000+条数据到JSON资源文件
把原来写在if里的位置信息转移到结构化的JSON文件中,避免硬编码在Java代码里:
- 在
app/src/main/res/目录下新建raw文件夹(如果没有的话) - 在
raw里创建places.json文件,按以下格式填充数据:
[ {"name": "地点1", "latitude": 39.9042, "longitude": 116.4074}, {"name": "地点2", "latitude": 31.2304, "longitude": 121.4737}, // 剩余1998条数据按相同格式补充 ]
步骤3:加载JSON数据生成数据源
在Activity中添加方法,把JSON文件解析成List<Place>集合:
private List<Place> loadPlacesFromJson() { List<Place> placeList = new ArrayList<>(); try { InputStream inputStream = getResources().openRawResource(R.raw.places); int size = inputStream.available(); byte[] buffer = new byte[size]; inputStream.read(buffer); inputStream.close(); String jsonString = new String(buffer, StandardCharsets.UTF_8); JSONArray jsonArray = new JSONArray(jsonString); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String name = jsonObject.getString("name"); double lat = jsonObject.getDouble("latitude"); double lng = jsonObject.getDouble("longitude"); placeList.add(new Place(name, lat, lng)); } } catch (IOException | JSONException e) { e.printStackTrace(); } return placeList; }
步骤4:自定义ListView Adapter绑定数据
创建Adapter将数据源渲染到ListView的每个列表项:
public class PlaceAdapter extends ArrayAdapter<Place> { public PlaceAdapter(Context context, List<Place> places) { super(context, 0, places); } @NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_1, parent, false); } Place place = getItem(position); TextView textView = convertView.findViewById(android.R.id.text1); textView.setText(place.getName()); return convertView; } }
步骤5:重构点击事件逻辑
删除原来的2000个if,用数据源直接处理点击跳转:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView listView = findViewById(R.id.list_view); List<Place> placeList = loadPlacesFromJson(); PlaceAdapter adapter = new PlaceAdapter(this, placeList); listView.setAdapter(adapter); listView.setOnItemClickListener((parent, view, position, id) -> { Place selectedPlace = placeList.get(position); // 跳转地图 Uri mapUri = Uri.parse("geo:" + selectedPlace.getLatitude() + "," + selectedPlace.getLongitude() + "?q=" + selectedPlace.getName()); Intent mapIntent = new Intent(Intent.ACTION_VIEW, mapUri); mapIntent.setPackage("com.google.android.apps.maps"); if (mapIntent.resolveActivity(getPackageManager()) != null) { startActivity(mapIntent); } }); }
额外优化建议
- 若JSON文件体积过大,可改用Room框架将数据存入本地SQLite数据库,提升加载效率
- 推荐将ListView替换为RecyclerView,它的视图复用机制更适合大量数据的列表展示
内容的提问来源于stack exchange,提问作者imacro
相关产品推荐
相关产品推荐

