Java 如何在Android Studio中使用多个值对名称进行分组?

Java 如何在Android Studio中使用多个值对名称进行分组?,java,android,json,Java,Android,Json,我只是想问一下,如何在Java中操作代码,在Java中显示具有多个latlong值的CommonName?请帮忙,我是编程新手 MainActivity.java public class MainActivity extends AppCompatActivity { public AutoCompleteTextView animalname; public String JSONString = "{\n" + " \"result\": [\n" +

我只是想问一下,如何在Java中操作代码,在Java中显示具有多个latlong值的CommonName?请帮忙,我是编程新手

MainActivity.java

public class MainActivity extends AppCompatActivity {

public AutoCompleteTextView animalname;

public String JSONString = "{\n" +
        "   \"result\": [\n" +
        "      {\n" +
        "         \"0\": \"14.44133541176629\",\n" +
        "         \"latitude\": \"14.44133541176629\",\n" +
        "         \"1\": \"120.45936525802608\",\n" +
        "         \"longitude\": \"120.45936525802608\",\n" +
        "         \"2\": \"Bataan\",\n" +
        "         \"province\": \"Bataan\",\n" +
        "         \"3\": \"Tamaraw\",\n" +
        "         \"commonname\": \"Tamaraw\"\n" +
        "      },\n" +
        "      {\n" +
        "         \"0\": \"14.656674396646768\",\n" +
        "         \"latitude\": \"14.656674396646768\",\n" +
        "         \"1\": \"121.05812014083858\",\n" +
        "         \"longitude\": \"121.05812014083858\",\n" +
        "         \"2\": \"Quezon\",\n" +
        "         \"province\": \"Quezon\",\n" +
        "         \"3\": \"Walden's Hornbill\",\n" +
        "         \"commonname\": \"Walden's Hornbill\"\n" +
        "      },\n" +
        "      {\n" +
        "         \"0\": \"14.898346071682198\",\n" +
        "         \"latitude\": \"14.898346071682198\",\n" +
        "         \"1\": \"121.42616213302608\",\n" +
        "         \"longitude\": \"121.42616213302608\",\n" +
        "         \"2\": \"General nakar\",\n" +
        "         \"province\": \"General nakar\",\n" +
        "         \"3\": \"Tamaraw\",\n" +
        "         \"commonname\": \"Tamaraw\"\n" +
        "      },\n" +
        "      {\n" +
        "         \"0\": \"15.378556159943873\",\n" +
        "         \"latitude\": \"15.378556159943873\",\n" +
        "         \"1\": \"121.39594973068233\",\n" +
        "         \"longitude\": \"121.39594973068233\",\n" +
        "         \"2\": \"Dingalan\",\n" +
        "         \"province\": \"Dingalan\",\n" +
        "         \"3\": \"Philippine Cockatoo\",\n" +
        "         \"commonname\": \"Philippine Cockatoo\"\n" +
        "      },\n" +
        "      {\n" +
        "         \"0\": \"15.491670747921509\",\n" +
        "         \"latitude\": \"15.491670747921509\",\n" +
        "         \"1\": \"120.94052188562534\",\n" +
        "         \"longitude\": \"120.94052188562534\",\n" +
        "         \"2\": \"Cabanatuan City\",\n" +
        "         \"province\": \"Cabanatuan City\",\n" +
        "         \"3\": \"Philippine Cockatoo\",\n" +
        "         \"commonname\": \"Philippine Cockatoo\"\n" +
        "      },\n" +
        "      {\n" +
        "         \"0\": \"16.229997551983594\",\n" +
        "         \"latitude\": \"16.229997551983594\",\n" +
        "         \"1\": \"120.52623997214562\",\n" +
        "         \"longitude\": \"120.52623997214562\",\n" +
        "         \"2\": \"Baguio\",\n" +
        "         \"province\": \"Baguio\",\n" +
        "         \"3\": \"Philippine Spotted Deer\",\n" +
        "         \"commonname\": \"Philippine Spotted Deer\"\n" +
        "      },\n" +
        "      {\n" +
        "         \"0\": \"6.984796116278719\",\n" +
        "         \"latitude\": \"6.984796116278719\",\n" +
        "         \"1\": \"122.02642351890961\",\n" +
        "         \"longitude\": \"122.02642351890961\",\n" +
        "         \"2\": \"Zamboanga\",\n" +
        "         \"province\": \"Zamboanga\",\n" +
        "         \"3\": \"Hawksbill Sea Turtle\",\n" +
        "         \"commonname\": \"Hawksbill Sea Turtle\"\n" +
        "      },\n" +
        "      {\n" +
        "         \"0\": \"10.100726050965035\",\n" +
        "         \"latitude\": \"10.100726050965035\",\n" +
        "         \"1\": \"125.59963979398412\",\n" +
        "         \"longitude\": \"125.59963979398412\",\n" +
        "         \"2\": \"Dinagat Island\",\n" +
        "         \"province\": \"Dinagat Island\",\n" +
        "         \"3\": \"Dinagat bushy-tailed Cloud Rat\",\n" +
        "         \"commonname\": \"Dinagat bushy-tailed Cloud Rat\"\n" +
        "      },\n" +
        "      {\n" +
        "         \"0\": \"9.35368808473656\",\n" +
        "         \"latitude\": \"9.35368808473656\",\n" +
        "         \"1\": \"118.36544272849846\",\n" +
        "         \"longitude\": \"118.36544272849846\",\n" +
        "         \"2\": \"Palawan\",\n" +
        "         \"province\": \"Palawan\",\n" +
        "         \"3\": \"Philippine Forest Turtle\",\n" +
        "         \"commonname\": \"Philippine Forest Turtle\"\n" +
        "      },\n" +
        "      {\n" +
        "         \"0\": \"9.646007526813666\",\n" +
        "         \"latitude\": \"9.646007526813666\",\n" +
        "         \"1\": \"122.85255472090171\",\n" +
        "         \"longitude\": \"122.85255472090171\",\n" +
        "         \"2\": \"Negros\",\n" +
        "         \"province\": \"Negros\",\n" +
        "         \"3\": \"Philippine naked-backed fruit bat\",\n" +
        "         \"commonname\": \"Philippine naked-backed fruit bat\"\n" +
        "      },\n" +
        "      {\n" +
        "         \"0\": \"9.551081019434731\",\n" +
        "         \"latitude\": \"9.551081019434731\",\n" +
        "         \"1\": \"123.09185859510103\",\n" +
        "         \"longitude\": \"123.09185859510103\",\n" +
        "         \"2\": \"Negros and Panay\",\n" +
        "         \"province\": \"Negros and Panay\",\n" +
        "         \"3\": \"Negros Bleeding-heart\",\n" +
        "         \"commonname\": \"Negros Bleeding-heart\"\n" +
        "      },\n" +
        "      {\n" +
        "         \"0\": \"9.380944735295179\",\n" +
        "         \"latitude\": \"9.380944735295179\",\n" +
        "         \"1\": \"118.38456063371927\",\n" +
        "         \"longitude\": \"118.38456063371927\",\n" +
        "         \"2\": \"Palawan\",\n" +
        "         \"province\": \"Palawan\",\n" +
        "         \"3\": \"Philippine Cockatoo\",\n" +
        "         \"commonname\": \"Philippine Cockatoo\"\n" +
        "      },\n" +
        "      {\n" +
        "         \"0\": \"10.071930427284427\",\n" +
        "         \"latitude\": \"10.071930427284427\",\n" +
        "         \"1\": \"125.59779391691245\",\n" +
        "         \"longitude\": \"125.59779391691245\",\n" +
        "         \"2\": \"Camiguin Sur and Dinagat Island\",\n" +
        "         \"province\": \"Camiguin Sur and Dinagat Island\",\n" +
        "         \"3\": \"Walden's Hornbill\",\n" +
        "         \"commonname\": \"Walden's Hornbill\"\n" +
        "      },\n" +
        "      {\n" +
        "         \"0\": \"13.093068957060206\",\n" +
        "         \"latitude\": \"13.093068957060206\",\n" +
        "         \"1\": \"121.06598892373722\",\n" +
        "         \"longitude\": \"121.06598892373722\",\n" +
        "         \"2\": \"Mindoro\",\n" +
        "         \"province\": \"Mindoro\",\n" +
        "         \"3\": \"Philippine Fresh Water Crocodile\",\n" +
        "         \"commonname\": \"Philippine Fresh Water Crocodile\"\n" +
        "      },\n" +
        "      {\n" +
        "         \"0\": \"9.651642644962154\",\n" +
        "         \"latitude\": \"9.651642644962154\",\n" +
        "         \"1\": \"122.84131398239595\",\n" +
        "         \"longitude\": \"122.84131398239595\",\n" +
        "         \"2\": \"Panay And Negros\",\n" +
        "         \"province\": \"Panay And Negros\",\n" +
        "         \"3\": \"Visayan Warty Pig\",\n" +
        "         \"commonname\": \"Visayan Warty Pig\"\n" +
        "      },\n" +
        "      {\n" +
        "         \"0\": \"7.2396901503428\",\n" +
        "         \"latitude\": \"7.2396901503428\",\n" +
        "         \"1\": \"125.44315069027664\",\n" +
        "         \"longitude\": \"125.44315069027664\",\n" +
        "         \"2\": \"Davao City\",\n" +
        "         \"province\": \"Davao City\",\n" +
        "         \"3\": \"Philippine Eagle\",\n" +
        "         \"commonname\": \"Philippine Eagle\"\n" +
        "      },\n" +
        "      {\n" +
        "         \"0\": \"9.810806171435631\",\n" +
        "         \"latitude\": \"9.810806171435631\",\n" +
        "         \"1\": \"124.26143093023506\",\n" +
        "         \"longitude\": \"124.26143093023506\",\n" +
        "         \"2\": \"Bohol\",\n" +
        "         \"province\": \"Bohol\",\n" +
        "         \"3\": \"Philippine Tarsier\",\n" +
        "         \"commonname\": \"Philippine Tarsier\"\n" +
        "      },\n" +
        "      {\n" +
        "         \"0\": \"13.088847376649245\",\n" +
        "         \"latitude\": \"13.088847376649245\",\n" +
        "         \"1\": \"121.0535496566772\",\n" +
        "         \"longitude\": \"121.0535496566772\",\n" +
        "         \"2\": \"Mindoro\",\n" +
        "         \"province\": \"Mindoro\",\n" +
        "         \"3\": \"Tamaraw\",\n" +
        "         \"commonname\": \"Tamaraw\"\n" +
        "      }\n" +
        "   ]\n" +
        "}";

public ArrayList<Animals> animalList;
TextView latitude_text, longitude_text, province;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    latitude_text = findViewById(R.id.latitude_text);
    longitude_text = findViewById(R.id.longitude_text);
    province = findViewById(R.id.province);

    initView();
    animalList = extractAnimals(JSONString);

    AnimalAdapter animalAdapter = new AnimalAdapter(this, R.layout.animal_row_layout, animalList);
    animalname.setAdapter(animalAdapter);
    animalname.setThreshold(1);

    animalname.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
            Animals animals = (Animals) parent.getItemAtPosition(position);

                animalname.setText(animals.getCommonname());
                latitude_text.setText(animals.getLatitude());
                longitude_text.setText(animals.getLongitude());
                province.setText(animals.getProvince());

        }
    });
}

private void initView() {
    animalname = findViewById(R.id.animalname);

    animalList = new ArrayList<>();
}

private ArrayList<Animals> extractAnimals(String JSONString){
    ArrayList<Animals> list = new ArrayList<>();

    try{
        JSONObject rootjo = new JSONObject(JSONString);

        JSONArray userja = rootjo.getJSONArray("result");
        for(int i = 0; i<userja.length(); i++){
            JSONObject jo= userja.getJSONObject(i);

            String commonname = jo.optString("commonname");
            String latitude = jo.optString("latitude");
            String longitude = jo.optString("longitude");
            String province = jo.optString("province");

            Animals anim = new Animals(commonname, latitude, longitude, province);

            list.add(anim);

        }

    }catch (JSONException e) {
        e.printStackTrace();

    }
    return list;
  }

 }
public class AnimalAdapter extends ArrayAdapter<Animals>{

public ArrayList<Animals> animalList;
public ArrayList<Animals> tempList;
public ArrayList<Animals> suggestionList;



public AnimalAdapter(@NonNull Context context, int resource, @NonNull List<Animals> objects) {
    super(context, resource, objects);

    animalList = (ArrayList<Animals>) objects;
    tempList = new ArrayList<>(animalList);
    suggestionList = new ArrayList<>();

}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

    if(convertView == null){
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.animal_row_layout, parent,false);

        TextView textView = convertView.findViewById(R.id.simple_text);
        Animals animals = animalList.get(position);
        textView.setText(animals.getCommonname());
    }
    return convertView;
}

@NonNull
@Override
public Filter getFilter() {
    return filter;
}

Filter filter = new Filter() {
    @Override
    public CharSequence convertResultToString(Object resultValue) {
        Animals animals = (Animals) resultValue;
        return animals.getCommonname();
    }

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults filterResults = new FilterResults();

        if (constraint != null && constraint.length()>0) {
            suggestionList.clear();
            constraint = constraint.toString().trim().toLowerCase();

            for (Animals animals : tempList){
                if(animals.getCommonname().toLowerCase().contains(constraint)){
                    suggestionList.add(animals);
                }
            }
            filterResults.count = suggestionList.size();
            filterResults.values = suggestionList;
        }

        return filterResults;
    }

    @Override
    protected void publishResults(CharSequence charSequence, FilterResults results) {
        ArrayList<Animals> uList = (ArrayList<Animals>) results.values;

        if(results != null && results.count > 0){
            clear();
            for (Animals u : uList) {
                add(u);
                notifyDataSetChanged();
            }
        }

    }
};
public class Animals {

private String commonname;
private String latitude;
private String longitude;
private String province;

public Animals(String commonname, String latitude, String longitude, String province) {
    this.commonname = commonname;
    this.latitude = latitude;
    this.longitude = longitude;
    this.province = province;
}

public String getCommonname() {
    return commonname;
}

public void setCommonname(String commonname) {
    this.commonname = commonname;
}

public String getLatitude() {
    return latitude;
}

public void setLatitude(String latitude) {
    this.latitude = latitude;
}

public String getLongitude() {
    return longitude;
}

public void setLongitude(String longitude) {
    this.longitude = longitude;
}

public String getProvince() {
    return province;
}

public void setProvince(String province) {
    this.province = province;
}
}

animal\u row\u layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/simple_text"/>

<View
    android:layout_height="2dp"
    android:layout_width="match_parent"
    android:background="@color/colorPrimary" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context="com.example.admin.sampletransfer.MainActivity">

<AutoCompleteTextView
    android:id="@+id/animalname"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:completionThreshold="1"
    android:hint="Search Animals"/>

<TextView
    android:layout_marginTop="30dp"
    android:id="@+id/latitude_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<TextView
    android:layout_marginTop="30dp"
    android:id="@+id/longitude_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<TextView
    android:layout_marginTop="30dp"
    android:id="@+id/province"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

活动\u main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/simple_text"/>

<View
    android:layout_height="2dp"
    android:layout_width="match_parent"
    android:background="@color/colorPrimary" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context="com.example.admin.sampletransfer.MainActivity">

<AutoCompleteTextView
    android:id="@+id/animalname"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:completionThreshold="1"
    android:hint="Search Animals"/>

<TextView
    android:layout_marginTop="30dp"
    android:id="@+id/latitude_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<TextView
    android:layout_marginTop="30dp"
    android:id="@+id/longitude_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<TextView
    android:layout_marginTop="30dp"
    android:id="@+id/province"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />


@Anton请不要因为使用了android studio标记就用它来标记问题:只有当您对IDE本身有疑问时,才应该使用android studio标记,而不是您在其中编写(或想要编写)的任何代码。请看,@Anton请不要因为您使用了android studio标记就用它来标记问题:只有当您对IDE本身有疑问时,才应该使用android studio标记,而不是您在其中编写(或想要编写)的任何代码。看到了吗