在Android中使用mysql填充Spinner

时间:2018-10-26 05:30:34

标签: android android-recyclerview spinner

我正在尝试从RecyclerView的数据库中获取下拉列表项,但是每当我尝试加载recyclerView Activity时,应用程序就会崩溃。

代码形成我的RecyclerView适配器:

public class Register_Adapter  extends RecyclerView.Adapter<Register_Adapter.MyHolder> {
//Line number 40
private Context context;
List<dataRegComplaint> data = Collections.emptyList();
List<productlist> data1 = new ArrayList<>(  );
Spinner product;
String total;
public static final int CONNECTION_TIMEOUT = 100000;
public static final int READ_TIMEOUT = 150000;
View v;

public Register_Adapter(complaintReg complaintReg,List<dataRegComplaint> data) {
this.context = complaintReg;
this.data = data;
}

@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.container_register, viewGroup, false);
    return new MyHolder( v );    
 //Line number 60
}

@Override
public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
    final dataRegComplaint current=data.get(i);
    myHolder.client.setText(current.getClientName());
    myHolder.location.setText("Location: " + current.getAddress());
    myHolder.category.setText("Reason: " + current.getCategory());
    myHolder.locationid = current.getlocationid();


    myHolder.complaint_register.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            total = current.getlocationid();
            Log.e( TAG,"Location id :"+total );
           context.startActivity( new Intent( context, Register_New_Complaint.class ) );
        }
    } );

}

@Override
public int getItemCount() {
    return data.size();
}

public class MyHolder extends RecyclerView.ViewHolder{
    TextView client,location,category;
    Button complaint_register;

    String locationid;
    public MyHolder(@NonNull View itemView) {

        super( itemView );

        client = (TextView) itemView.findViewById( R.id.textclient );
        location = (TextView) itemView.findViewById( R.id.textlocation );
        product  = (Spinner) itemView.findViewById( R.id.textproduct1 );
        category = (TextView) itemView.findViewById( R.id.textcategory );
        complaint_register = (Button) itemView.findViewById( R.id.button_register );

       product.setOnItemSelectedListener( (AdapterView.OnItemSelectedListener) context );
    }
//Line number 105
}

private class GetProduct extends AsyncTask<String,Void,String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }


    @Override
    protected String doInBackground(String... strings) {
        HttpURLConnection conn;
        URL url = null;
        try {
            url = new URL( "http://100.98.115.205:8089/productlist.php" );
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {


            conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout( READ_TIMEOUT );
            conn.setConnectTimeout( CONNECTION_TIMEOUT );
            conn.setRequestMethod( "POST" );


            conn.setDoOutput( true );
            OutputStream outputStream = conn.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter( new OutputStreamWriter( outputStream, "UTF-8" ) );
            String post_data = URLEncoder.encode( "total", "UTF-8" ) + "=" + URLEncoder.encode(total, "UTF-8" );
            Log.e( TAG, "POST DATAv :"+post_data );
            bufferedWriter.write( post_data );
            bufferedWriter.flush();
            bufferedWriter.close();

        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            return e1.toString();
        }


        try {

            int response_code = conn.getResponseCode();

            // Check if successful connection made
            if (response_code == HttpURLConnection.HTTP_OK) {

                // Read data sent from server
                InputStream input = conn.getInputStream();
                BufferedReader reader = new BufferedReader( new InputStreamReader( input ) );
                StringBuilder result = new StringBuilder();
                String line;

                while ((line = reader.readLine()) != null) {
                    result.append( line );
                }

                // Pass data to onPostExecute method
                return (result.toString());

            } else {

                return ("unsuccessful");
            }

        } catch (IOException e) {
            e.printStackTrace();
            return e.toString();
        } finally {
            conn.disconnect();
        }


    }


    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute( result );
        Log.e( TAG, "RESULT :" +result );

        try {

            JSONArray jArray = new JSONArray( result );

            // Extract data from json and store into ArrayList as class objects
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json_data = jArray.getJSONObject( i );
                productlist fishData = new productlist(
                        json_data.getString( "prod_name" ) );
                data1.add( fishData );

                Log.e( TAG, "DATA reesult :" +fishData );
            }
            populateSpinner();

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
private void populateSpinner() {
    List<String> lables = new ArrayList<String>();

    for(int i = 0; i < data1.size(); i++){

        lables.add( data1.get( i ).getSiteid());
        Log.e( TAG, "Spinner :" +lables.add( data1.get( i ).getSiteid()) );
    }

    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>( context,android.R.layout.simple_spinner_dropdown_item, lables );
    spinnerAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
    product.setAdapter( spinnerAdapter );

}
}

LogCat:

 java.lang.ClassCastException: nikhil.loginapp.com.complaintReg cannot be cast to android.widget.AdapterView$OnItemSelectedListener
                  at nikhil.loginapp.com.Register_Adapter$MyHolder.<init>(Register_Adapter.java:105)
                  at nikhil.loginapp.com.Register_Adapter.onCreateViewHolder(Register_Adapter.java:60)
                  at nikhil.loginapp.com.Register_Adapter.onCreateViewHolder(Register_Adapter.java:40)

我正在PostExecute函数中获取数据,但此后它在Spinner中显示错误并且应用程序崩溃。

有人帮助我。

0 个答案:

没有答案