数据是从数据库mysql中获取的,并且它没有显示在spiner中,但它显示在下拉列表中,不知道问题出在哪里..
在这里我完整的代码: -
main.java
public class MainActivity extends AppCompatActivity {
ArrayList<String> listItems = new ArrayList<>();
Spinner spn;
ArrayAdapter<String> adapter;
ProgressDialog mProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spn= (Spinner)findViewById(R.id.selectcmpny);
adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_layout, R.id.txt1,listItems);
spn.setAdapter(adapter);
adapter.notifyDataSetChanged();
// spn.setSelection(0);
}
public void onStart() {
super.onStart();
BackTask bt = new BackTask();
bt.execute();
}
//for Spinner
private class BackTask extends AsyncTask<Void, Void, Void> {
ArrayList<String> list;
@Override
protected void onPreExecute() {
super.onPreExecute();
list = new ArrayList<>();
}
protected Void doInBackground(Void... params) {
InputStream is = null;
String result = "";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.abcdefg.com/o_cmpany.php");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
// Get our response as a String.
is = entity.getContent();
} catch (IOException e) {
e.printStackTrace();
}
//convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"));
String line = null;
while ((line = reader.readLine()) != null) {
result += line;
}
is.close();
//result=sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
// parse json data
try {
JSONArray jArray = new JSONArray(result);
list.add("Please Select Company");
for (int i = 0; i < jArray.length(); i++) {
JSONObject jsonObject = jArray.getJSONObject(i);
// add interviewee name to arraylist
list.add(jsonObject.getString("name"));
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result) {
listItems.addAll(list);
}
}
}
main.xml中
<Spinner
android:background="#ff0"
android:spinnerMode="dropdown"
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/selectcmpny"
/>
spinner_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="sachin"
android:background="#87f2f9f7"
android:textSize="20dp"
android:textColor="#000"
android:padding="10dp"
/>
</LinearLayout>
答案 0 :(得分:1)
你可以尝试
自定义适配器。 AdapterSpinner.java
public class AdapterSpinner extends ArrayAdapter<String> {
public AdapterSpinner(@NonNull Context context) {
super(context, 0);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.spinner_layout, null);
TextView txv = (TextView) convertView.findViewById(R.id.txt1);
String text = getItem(position);
txv.setText(text);
return convertView;
}
@Override
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.spinner_layout, null);
TextView txv = (TextView) convertView.findViewById(R.id.txt1);
String text = getItem(position);
txv.setText(text);
return convertView;
}
}
新适配器
adapter = new AdapterSpinner(this);
adapter.addAll(listItems);
来自onPostExecute方法的
protected void onPostExecute(Void result) {
listItems.addAll(list);
adapter.clear();
adapter.addAll(list);
adapter.notifyDataSetChanged();
}
祝你好运