Android studio,使用来自不同MYSQL数据库的不同数据加载2个微调器

时间:2017-02-13 14:21:38

标签: java mysql android-studio

我的应用程序具有行程功能,您创建新行程的部分包括从2个旋转器中选择下拉以显示不同的选项。来自MQsql数据库的数据和JSON响应对于两个集都是正确的。目前我已经能够获得一组数据加载到正确的微调器(无论是传输信息还是吸引力信息,而不是两者),或者我已经能够获得两组数据加载,但两者都是显示在相同的2个微调器中。我需要将它们分成分配的微调器,但需要帮助这样做,因为我不会做什么我做错了。

createItinerary class:

public class CreateItinerary extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

TextView txtDate;

private Spinner spinnerAttraction;
private Spinner spinnerTransport;
// array list for spinner adapter
private ArrayList<Category> categoriesList;
ProgressDialog pDialog;
List<String> lables = new ArrayList<String>();

private ArrayList<ItineraryAdapter>Entities;
private ArrayList<ItineraryAdapter>finalEntities;

LayoutInflater myInflator;
View myView;

DBManager db;
myAdapter adapter;

String NAME;
String LOCATION;
String TIME;

static  final int DIALOG_ID = 0;
int hour_x;
int min_x;

TextView TextTime;
String ItineraryName;
private String URL_ATTRACTIONS = "http://10.0.2.2/TravelApp/get_all_spinner.php";
private String URL_TRANSPORT = "http://10.0.2.2/TravelApp/get_all_transport_minor.php";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_itinerary);
    txtDate = (TextView) findViewById(R.id.tvSelectDate);

    myInflator = getLayoutInflater();
    myView = myInflator.inflate(R.layout.list_create_itinerary, null);
    spinnerAttraction = (Spinner) findViewById(R.id.spinnerAttraction);
    spinnerTransport = (Spinner) findViewById(R.id.spinnerTransport);

    db = new DBManager(this);

    categoriesList = new ArrayList<Category>();
    Entities = new ArrayList<ItineraryAdapter>();
    finalEntities = new ArrayList<ItineraryAdapter>();
    // spinner item select listener
    spinnerAttraction.setOnItemSelectedListener(this);
    spinnerTransport.setOnItemSelectedListener(this);

    new GetAttractions().execute();
    new GetTransport().execute();

    showTimePickerDialog();

    Bundle bundle = getIntent().getExtras();

    ItineraryName = bundle.getString("Itinerary Name");

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.home_button, 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.go_home){
        final TextView alertMessage = new TextView(this);
        alertMessage.setText(" All changes will be lost are you sure you want to return back to the home page? ");
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("Unsaved changes")
                .setView(alertMessage)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent i = new Intent(getApplicationContext(), QavelNav.class);
                        startActivity(i);
                    }
                })
                .setNegativeButton("No", null)
                .create();
        dialog.show();
    }
    return super.onOptionsItemSelected(item);
}

public void pickDate(View v) {
    DatePickerClass datepicker = new DatePickerClass();
    datepicker.setText(v);
    datepicker.show(getSupportFragmentManager(), "datepicker");
    System.out.println(getDate());
}

public String getDate() {
    String date;
    date = txtDate.getText().toString();
    return date;
}

public void addAttractionToItinerary(View v){
    Entities.add(new ItineraryAdapter(NAME,LOCATION,null));
    loadAttractions();
    System.out.println(ItineraryName);
}

public void loadAttractions(){
    adapter = new myAdapter(Entities);
    ListView ls = (ListView) findViewById(R.id.listCreateItinerary);
    ls.setAdapter(adapter);
    for(int i =0; i < finalEntities.size(); i++){
        System.out.println(finalEntities.get(i).NAME + " " + finalEntities.get(i).LOCATION + " " + finalEntities.get(i).TIME);
    }

}

public void onSave(View v){
    ContentValues values = new ContentValues();
    for(int i = 0; i <finalEntities.size();i++) {
        values.put(DBManager.ColItineraryName,ItineraryName);
        values.put(DBManager.ColDate,txtDate.getText().toString());
        values.put(DBManager.ColName,finalEntities.get(i).NAME );
        values.put(DBManager.ColLocation,finalEntities.get(i).LOCATION);
        values.put(DBManager.ColTime,finalEntities.get(i).TIME);
        long id = db.Insert("Itinerary",values);
        if (id > 0)
            Toast.makeText(getApplicationContext(),"Added to Itinerary", Toast.LENGTH_LONG).show();
        else
            Toast.makeText(getApplicationContext(),"cannot insert", Toast.LENGTH_LONG).show();
    }
}

public void showTimePickerDialog(){
    TextTime = (TextView) myView.findViewById(R.id.tvCreateItineraryTime);
    TextTime.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            showDialog(DIALOG_ID);

        }
    });

}

@Override
protected Dialog onCreateDialog(int id){
    if(id== DIALOG_ID)
        return new TimePickerDialog(CreateItinerary.this,KTimePickerListner, hour_x, min_x,false);
    return null;
}

protected TimePickerDialog.OnTimeSetListener KTimePickerListner = new TimePickerDialog.OnTimeSetListener(){
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute){
        hour_x = hourOfDay;
        min_x = minute;
        Toast.makeText(CreateItinerary.this,hour_x+" : " + min_x, Toast.LENGTH_LONG).show();
        setTime(hour_x, min_x);
        TIME = hour_x + ":" + min_x;
        finalEntities.add(new ItineraryAdapter(NAME,LOCATION,TIME));

    }
};

public void setTime(int hour, int min){
    TextTime.setText(hour_x+":"+min_x);
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long l) {
    Toast.makeText(
            getApplicationContext(),
            parent.getItemAtPosition(position).toString() + " Selected" ,
            Toast.LENGTH_LONG).show();

    NAME = categoriesList.get(position).getName();
    LOCATION = categoriesList.get(position).getLocation();
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}

/**
 * Adding spinner data
 * */
private void populateSpinner() {
    for (int i = 0; i < categoriesList.size(); i++) {
        lables.add(categoriesList.get(i).getName() + " - " + categoriesList.get(i).getLocation());
    }

    // Creating adapter for spinner
    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, lables);

    // Drop down layout style - list view with radio button
    spinnerAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // attaching data adapter to spinner
    spinnerAttraction.setAdapter(spinnerAdapter);
    spinnerTransport.setAdapter(spinnerAdapter);
}

/**
 * Async task to get all food categories
 * */
private class GetAttractions extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(CreateItinerary.this);
        pDialog.setMessage("Fetching attraction categories..");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        ServiceHandler jsonParser = new ServiceHandler();
        String json = jsonParser.makeServiceCall(URL_ATTRACTIONS, ServiceHandler.GET);

        Log.e("Response: ", "> " + json);

        if (json != null) {
            try {
                JSONObject jsonObj = new JSONObject(json);
                if (jsonObj != null) {
                    JSONArray categories = jsonObj
                            .getJSONArray("attraction");

                    for (int i = 0; i < categories.length(); i++) {
                        JSONObject catObj = (JSONObject) categories.get(i);
                        Category cat = new Category(catObj.getInt("Id"),
                                catObj.getString("Name"), catObj.getString("Location"));
                        categoriesList.add(cat);
                    }
                }

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

        } else {
            Log.e("JSON Data", "Didn't receive any data from server!");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (pDialog.isShowing())
            pDialog.dismiss();
        populateSpinner();
    }
}
private class GetTransport extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(CreateItinerary.this);
        pDialog.setMessage("Fetching transport categories..");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        ServiceHandler jsonParser = new ServiceHandler();
        String json = jsonParser.makeServiceCall(URL_TRANSPORT, ServiceHandler.GET);

        Log.e("Response: ", "> " + json);

        if (json != null) {
            try {
                JSONObject jsonObj = new JSONObject(json);
                if (jsonObj != null) {
                    JSONArray categories = jsonObj
                            .getJSONArray("transport");

                    for (int i = 0; i < categories.length(); i++) {
                        JSONObject catObj = (JSONObject) categories.get(i);
                        Category cat = new Category(catObj.getInt("Id"),
                                catObj.getString("Name"), catObj.getString("Location"));
                        categoriesList.add(cat);
                    }
                }

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

        } else {
            Log.e("JSON Data", "Didn't receive any data from server!");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (pDialog.isShowing())
            pDialog.dismiss();
        populateSpinner();
    }

}

class myAdapter extends BaseAdapter {


    public ArrayList<ItineraryAdapter> listItem;
    ItineraryAdapter ac;
    public myAdapter(ArrayList<ItineraryAdapter> listItem) {
        this.listItem = listItem;

    }

    @Override
    public int getCount() {
        return listItem.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {

        ac = listItem.get(position);

        TextView Name = (TextView) myView.findViewById(R.id.tvCreateItineraryName);
        Name.setText(ac.NAME);

        TextView Location = (TextView) myView.findViewById(R.id.tvCreateItineraryLocation);
        Location.setText(ac.LOCATION);


        /*
        Button buttonDelete = (Button)myView.findViewById(R.id.buttonDelete);
        buttonDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Entities.remove(position);
                finalEntities.remove(position);
                loadAttractions();
            }
        });*/
        return myView;

    }
}
}

感谢任何可以帮助我的人。

0 个答案:

没有答案