我试图开发一个用于安排汽车约会的应用程序。现在,我在Android Studio的应用程序中显示了数据,我想要做一些聪明的事情,例如,当我按下按钮时,它可以进行评估并重新排列列表约会。
import org.json.JSONException;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private TextView mTextMessage;
String[] dataDetail;
ArrayList<dataDetail> allDetail = new ArrayList<>();
ListView detailList;
final Context context = this;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
detailList = findViewById(R.id.dataView);
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
new GetData().execute();
}
private class GetData extends AsyncTask<Void, Void, ArrayList<dataDetail>> {
protected ArrayList<dataDetail> doInBackground(Void... voids) {
HttpURLConnection urlConnection;
InputStream in = null;
try {
URL url = new URL("http://10.0.2.2:8080/projectServer/DataDetailDB?getdata=true");
urlConnection = (HttpURLConnection) url.openConnection();
in = new BufferedInputStream(urlConnection.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
String response = convertStreamToString(in);
System.out.println("Server response = " + response);
try {
JSONArray jsonArray = new JSONArray(response);
dataDetail = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
String customername = jsonArray.getJSONObject(i).get("customerName").toString();
String carname = jsonArray.getJSONObject(i).get("carName").toString();
String appointmentdate = jsonArray.getJSONObject(i).get("appointmentDate").toString();
String email = jsonArray.getJSONObject(i).get("email").toString();
String issuedescribe = jsonArray.getJSONObject(i).get("issueDescribe").toString();
dataDetail tData = new dataDetail(customername, carname, appointmentdate, email, issuedescribe);
allDetail.add(tData);
System.out.println("customername = " + customername + "carname = " + carname + "appointmentdate = " + appointmentdate +
"email = " + email + "describe = " + issuedescribe);
dataDetail [i] = "Name: " + customername + "\n" + "Appointment Date: " + appointmentdate;
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(ArrayList<dataDetail> dataDetailArrayList) {
super.onPostExecute(dataDetailArrayList);
ArrayAdapter theList = new ArrayAdapter(context, android.R.layout.simple_list_item_1, dataDetail);
detailList.setAdapter(theList);
}
}
public String convertStreamToString(InputStream is) {
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
}
我当时正在考虑使用启发式方法进行评估,例如:
W1 *工作时间+ W2 *可靠的车辆+ W3 *工作之间的距离+ W4 *成本。
根据每次约会的结果,然后重新排列列表顺序,重要或更高分数将排在列表顶部。
现在我不知道如何在Java中启动或实现它。任何人都可以给我一些建议或评论,这将非常有帮助。
谢谢