Как заполнить мой Spinner на основе значения в моем TextView?

Я новичок в программировании Android и работаю над проектом, который извлекает данные из БД MySQL.

Теперь у меня есть два файла Java. Первый выбирает сотрудников и их идентификаторы. У него есть Spinners, который при щелчке копирует значение имени и идентификатора сотрудников в TextViews и передает данные в следующее действие через Intent.

В то время как второй выбирает расписание сотрудников на основе идентификатора сотрудника. Это второе действие должно заполнять Spinner на основе идентификатора сотрудников, который был передан из первого действия.

Первое занятие:

public class FirstActivity extends Activity {
JSONObject jObj;
JSONArray jArray;
ProgressDialog pd;
ArrayList < String > aestheticianList;
ArrayList < Items > items;
Button btnTransact;
String service_name, price;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner_farmers_aesthetician);

new DownloadJSON().execute();

btnTransact = (Button) findViewById(R.id.btnTransact);
btnTransact.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(cp000l3.flawlessfaceandbodyclinic.project.transact.FarmersAestheticianActivity.this,
cp000l3.flawlessfaceandbodyclinic.project.transact.FarmersDateTimeActivity.class);
String service_name = ((TextView) findViewById(R.id.service_name)).getText().toString();
String price = ((TextView) findViewById(R.id.price)).getText().toString();
String full_name = ((TextView) findViewById(R.id.full_name)).getText().toString();
String aid = ((TextView) findViewById(R.id.aid)).getText().toString();

i.putExtra("service_name", service_name);
i.putExtra("price", price);
i.putExtra("full_name", full_name);
i.putExtra("aid", aid);

startActivityForResult(i, 100);
}
});

TextView txtService_name = (TextView) findViewById(R.id.service_name);
TextView txtPrice = (TextView) findViewById(R.id.price);
Intent i = getIntent();
service_name = i.getStringExtra("service_name");
price = i.getStringExtra("price");

txtService_name.setText(service_name);
txtPrice.setText(price);
}
private class DownloadJSON extends AsyncTask < Void, Void, Void > {

@Override
protected Void doInBackground(Void...params) {
// TODO Auto-generated method stub
items = new ArrayList < Items > ();
aestheticianList = new ArrayList < String > ();

try {
jObj = JSONParser.getJSONfromURL("http://192.168.1.9:8013/flawlessadmin/storescripts/transaction/final/get_aesthetician2.php");
try {
jArray = jObj.getJSONArray("aesthetician");
for (int i = 0; i < jArray.length(); i++) {
jObj = jArray.getJSONObject(i);

Items item = new Items();

item.setFull_name(jObj.optString("full_name"));
item.setAid(jObj.optString("aid"));

items.add(item);
aestheticianList.add(jObj.optString("full_name"));
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void args) {
Spinner spin = (Spinner) findViewById(R.id.spin_aesthetician);

spin.setAdapter(new ArrayAdapter < String > (FarmersAestheticianActivity.this,
android.R.layout.simple_spinner_dropdown_item, aestheticianList));

spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView <? > arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
TextView txt_full_name = (TextView) findViewById(R.id.full_name);
TextView txt_aid = (TextView) findViewById(R.id.aid);

txt_full_name.setText(items.get(position).getFull_name());
txt_aid.setText(items.get(position).getAid());

////////////////////////////////

////////////////////////////////
}

@Override
public void onNothingSelected(AdapterView <? > parent) {
// TODO Auto-generated method stub

}
});
}
}
}

Второе занятие:

public class SecondActivity extends Activity {
JSONObject jObj;
JSONArray jArray;
ProgressDialog pd;
ArrayList < String > dateTimeList;
ArrayList < Items > items;
Button btnTransact;
String service_name, price, full_name, aid, tid;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner_farmers_datetime);

new DownService().execute();

btnTransact = (Button) findViewById(R.id.btnTransact);
btnTransact.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(cp000l3.flawlessfaceandbodyclinic.project.transact.FarmersDateTimeActivity.this,
cp000l3.flawlessfaceandbodyclinic.project.transact.FarmersConfirmationActivity.class);
String service_name = ((TextView) findViewById(R.id.service_name)).getText().toString();
String price = ((TextView) findViewById(R.id.price)).getText().toString();
String aid = ((TextView) findViewById(R.id.aid)).getText().toString();
String full_name = ((TextView) findViewById(R.id.full_name)).getText().toString();
String tid = ((TextView) findViewById(R.id.tid)).getText().toString();
String datetime = ((TextView) findViewById(R.id.datetime)).getText().toString();

i.putExtra("service_name", service_name);
i.putExtra("price", price);
i.putExtra("aid", aid);
i.putExtra("full_name", full_name);
i.putExtra("tid", tid);
i.putExtra("date_time", datetime);

startActivityForResult(i, 100);
}
});

TextView txtService_name = (TextView) findViewById(R.id.service_name);
TextView txtPrice = (TextView) findViewById(R.id.price);
TextView txtFull_name = (TextView) findViewById(R.id.full_name);
TextView txtAid = (TextView) findViewById(R.id.aid);
Intent i = getIntent();
service_name = i.getStringExtra("service_name");
price = i.getStringExtra("price");
full_name = i.getStringExtra("full_name");
aid = i.getStringExtra("aid");

txtService_name.setText(service_name);
txtPrice.setText(price);
txtFull_name.setText(full_name);
txtAid.setText(aid);
}
private class DownService extends AsyncTask < Void, Void, Void > {

@Override
protected Void doInBackground(Void...params) {
// TODO Auto-generated method stub
items = new ArrayList < Items > ();
dateTimeList = new ArrayList < String > ();

/////////////////////////
List < NameValuePair > param = new ArrayList < NameValuePair > ();
param.add(new BasicNameValuePair("aid", aid));
/////////////////////////

try {
String jObj = JSONParser.makeHttpRequest2("http://192.168.1.9:8013/flawlessadmin/storescripts/transaction/final/get_time2.php", "GET", param); //("http://192.168.1.9:8013/flawlessadmin/storescripts/transaction/final/get_time.php");
try {
JSONObject jsonObj = new JSONObject(jObj);
for (int i = 0; i < jArray.length(); i++) {
jsonObj = jArray.getJSONObject(i);

Items item = new Items();

item.setDateTime(jsonObj.optString("date_time"));
item.setTid(jsonObj.optString("tid"));

items.add(item);
dateTimeList.add(jsonObj.optString("date_time"));
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void args) {
Spinner spin = (Spinner) findViewById(R.id.spin_datetime);

spin.setAdapter(new ArrayAdapter < String > (FarmersDateTimeActivity.this,
android.R.layout.simple_spinner_dropdown_item, dateTimeList));

spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView <? > arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
TextView txt_datetime = (TextView) findViewById(R.id.datetime);
TextView txt_tid = (TextView) findViewById(R.id.tid);

txt_datetime.setText(items.get(position).getDateTime());
txt_tid.setText(items.get(position).getTid());

////////////////////////////////

////////////////////////////////
}

@Override
public void onNothingSelected(AdapterView <? > parent) {
// TODO Auto-generated method stub

}
});
}
}
}

1

Решение

во втором занятии:

Bundle extras = getIntent().getExtras();
int id = extras.getInt("Id");  // get the id passed from first activty
0

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]