Data is allocated in spinner but not displaying in

2019-09-15 16:16发布

Data is fetching from the Database mysql and it is not displaying in the spiner but it is displaying in the dropdown don't know where's the problem is..

here me complete code :-

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>

1条回答
干净又极端
2楼-- · 2019-09-15 16:35

You can try

Custom a adapter. 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;
    }
}

new adapter

adapter = new AdapterSpinner(this);
adapter.addAll(listItems);

from method onPostExecute

protected void onPostExecute(Void result) {
        listItems.addAll(list);
        adapter.clear();
        adapter.addAll(list);
        adapter.notifyDataSetChanged();
    }

good luck

查看更多
登录 后发表回答