此活动从服务器获取数据,将其显示在spinner上。并根据微调器中的选择在listView上显示数据。
Spinner在下拉菜单中显示数据,但Spinner上没有文本,无论是在活动开始时还是在从Spinner中选择之后都没有。
On Activity load or after selection 。 PopUp Data on Spinner
ListView在微调器选择上工作正常。
通过此链接Data is allocated in spinner but not displaying in the spinner only visible in the dropdown
更新后的代码仍然存在。
activity_park_selection.xml
<Spinner
android:id="@+id/sp_district"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="230dp"
android:alpha=".3"
android:padding="8dp"
android:spinnerMode="dropdown"
android:background="@drawable/custom_spinner_background"
android:textAlignment="center"/>
ParkSelectionActivity.java
public class ParkSelectionActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
SQLiteHelper localDBHelper;
ArrayList<ParkDetails> ParkList = new ArrayList<ParkDetails>();
String[] distNameArray;
ArrayList<String> distNameArray1;
ArrayAdapter<String> adapter;
ListView parkGridView;
AutoCompleteTextView atv_search;
ArrayAdapter<String> distAdapter;
ParkDetails parkDetails;
String PARKID;
Spinner sp_district;
private ParkDetailsShowAdapter parklistadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_park_selection);
sp_district = (Spinner) findViewById(R.id.sp_district); // Spinner
parkGridView = (ListView) findViewById(R.id.gvParks);
// Spinner click listener
sp_district.setOnItemSelectedListener(this);
ArrayList<String> parksList;
parkGridView.setEmptyView(findViewById(R.id.empty_list_view));
localDBHelper = new SQLiteHelper(ParkSelectionActivity.this);
ParkList = localDBHelper.getParksList();
findViewById(R.id.ll_filterparks);
parklistadapter = new ParkDetailsShowAdapter(this, ParkList);
parklistadapter.upDateEntries(ParkList);
parkGridView.setAdapter(parklistadapter);
parklistadapter.notifyDataSetChanged();
loadDistrictsAdapter();
parkGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
parkDetails = new ParkDetails();
parkDetails = ParkList.get(position);
PARKID = parkDetails.getParkID();
new GetParkTicketsListAsyncTask().execute(PARKID);
}
});
}
private void loadDistrictsAdapter() {
distNameArray1 = new ArrayList<String>();
distNameArray = new String[ParkList.size()];
int i = 0;
for (ParkDetails beneficiary : ParkList) {
if (!distNameArray1.contains(beneficiary.getDistrict()))
distNameArray1.add(beneficiary.getDistrict());
i++;
}
distAdapter = new ArrayAdapter<String>(this, R.layout.autotextviewitem, distNameArray1);
sp_district.setAdapter(distAdapter);// Setting district in Spinner
distAdapter.notifyDataSetChanged();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
finish();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String s = sp_district.getItemAtPosition(position).toString();
ParkList = localDBHelper.getParksList(s);
parklistadapter = new ParkDetailsShowAdapter(ParkSelectionActivity.this, ParkList);
parklistadapter.upDateEntries(ParkList);
parkGridView.setAdapter(parklistadapter);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
public class GetParkTicketsListAsyncTask extends AsyncTask<String, Void, ArrayList<TicketDetails>> {
private final ProgressDialog dialog1 = new ProgressDialog(ParkSelectionActivity.this);
String pid = "-1";
@Override
protected void onPreExecute() {
this.dialog1.setCanceledOnTouchOutside(false);
this.dialog1.setMessage("Wait...");
this.dialog1.show();
}
@Override
protected ArrayList<TicketDetails> doInBackground(String... params) {
ArrayList<TicketDetails> res = WebServiceHelper.GetParkTicketsList(params[0]);
return res;
}
@Override
protected void onPostExecute(ArrayList<TicketDetails> result) {
if (this.dialog1.isShowing()) {
this.dialog1.dismiss();
}
if (result != null && result.size() > 0) {
SQLiteHelper placeData = new SQLiteHelper(ParkSelectionActivity.this);
placeData.insertTickets(result);
new GetParkAmenitiesListAsyncTask().execute(PARKID);
} else {
Toast.makeText(getApplicationContext(), "Network Error !!!", Toast.LENGTH_LONG).show();
}
}
}
public class GetParkAmenitiesListAsyncTask extends AsyncTask<String, Void, ArrayList<AmenityDetails>> {
private final ProgressDialog dialog1 = new ProgressDialog(ParkSelectionActivity.this);
String pid = "-1";
@Override
protected void onPreExecute() {
this.dialog1.setCanceledOnTouchOutside(false);
this.dialog1.setMessage("Wait...");
this.dialog1.show();
}
@Override
protected ArrayList<AmenityDetails> doInBackground(String... params) {
ArrayList<AmenityDetails> res = WebServiceHelper.GetParkAmenitiesRatesList(params[0]);
return res;
}
@Override
protected void onPostExecute(ArrayList<AmenityDetails> result) {
if (this.dialog1.isShowing()) {
this.dialog1.dismiss();
}
if (result != null && result.size() > 0) {
SQLiteHelper placeData = new SQLiteHelper(ParkSelectionActivity.this);
placeData.insertAmenitys(result);
Intent intent = new Intent(ParkSelectionActivity.this, UserHomeActivity.class);
intent.putExtra("ParkDetails", parkDetails);
startActivity(intent);
} else {
Intent intent = new Intent(ParkSelectionActivity.this, UserHomeActivity.class);
intent.putExtra("ParkDetails", parkDetails);
startActivity(intent);
}
}
}
}