将微调器数据传递到其他字段

时间:2019-04-07 11:27:19

标签: java android arrays

我有一个xml活动,它包含一个微调项,我想将微调的值传递给同一活动上的其他textView。附加了两个文件xml和java,旋转器名称为simpleSpinner,而我希望具有旋转器值的textView为district_id,请更新代码并为我提供帮助

package com.cp.comp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;


    public class add_comp extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
    String[] bankNames={"جنين","2","طولكرم","نابلس","قلقيلية","سلفيت","رام الله","اريحا","بيت لحم","القدس","الخليل"};
    private EditText shop_name, complainant_name, complainant_id, mobile;
    private EditText district_id, address_detail, comp_type, comp_detail;
    private ProgressBar loading;
    private static String URL_ADDCOMP = "http://172.23.50.55/CP/add_comp.php";
    private Button btn_add_comp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_comp);
        btn_add_comp = findViewById(R.id.btn_add_comp);
        loading = findViewById(R.id.loading);
        shop_name = findViewById(R.id.shop_name);
        complainant_name = findViewById(R.id.complainant_name);
        complainant_id = findViewById(R.id.complainant_id);
        mobile = findViewById(R.id.mobile);
        district_id = findViewById(R.id.district_id);
        address_detail = findViewById(R.id.address_detail);
        comp_type = findViewById(R.id.comp_type);
        comp_detail = findViewById(R.id.comp_detail);
        Spinner spin = (Spinner) findViewById(R.id.district_id);
        spin.setOnItemSelectedListener(this);

        //Creating the ArrayAdapter instance having the bank name list
        ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,bankNames);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);


        spin.setAdapter(aa);
        btn_add_comp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Add_comp();
            }
        }); 

     private void Add_comp() {
        loading.setVisibility(View.VISIBLE);
        btn_add_comp.setVisibility(View.GONE);
        final String shop_name        = this.shop_name.getText().toString().trim();
        final String complainant_name = this.complainant_name.getText().toString().trim();
        final String complainant_id   = this.complainant_id.getText().toString().trim();
        final String mobile           = this.mobile.getText().toString().trim();
        final String district_id      = this.district_id.getText().toString().trim();
        final String address_detail   = this.address_detail.getText().toString().trim();
        final String comp_type        = this.comp_type.getText().toString().trim();
        final String comp_detail      = this.comp_detail.getText().toString().trim();

    System.out.println("ya abed almoty " + comp_detail);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_ADDCOMP,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {
                        try{
                            JSONObject jsonObject = new JSONObject(response);
                            String success = jsonObject.getString("success");
                            System.out.println("ya habibi");
                            if (success.equals("1")) {
                                Toast.makeText(add_comp.this, "تم إرسال الشكوى بنجاح!", Toast.LENGTH_SHORT).show();
                                System.out.println("ya belal");
                            }


                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(add_comp.this, "ارسال خاطئ! " + e.toString(), Toast.LENGTH_SHORT).show();
                            loading.setVisibility(View.GONE);
                            btn_add_comp.setVisibility(View.VISIBLE);
                            System.out.println("ya jehad");
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(add_comp.this, "ارسال خاطئ! " + error.toString(), Toast.LENGTH_SHORT).show();
                        loading.setVisibility(View.GONE);
                        btn_add_comp.setVisibility(View.VISIBLE);
                        System.out.println("ya morad");
                    }
                })

        {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("shop_name", shop_name);
                params.put("complainant_name", complainant_name);
                params.put("complainant_id", complainant_id);
                params.put("mobile", mobile);
                params.put("district_id", district_id);
                params.put("address_detail", address_detail);
                params.put("comp_type", comp_type);
                params.put("comp_detail", comp_detail);
                System.out.println("ya fahed" +  params.put("comp_type", comp_type));


                System.out.println("ya vvvvvvvvv" +   bankNames.toString().trim());
                return params;
            }
        };
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);

    }
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
        Toast.makeText(getApplicationContext(), bankNames[position], Toast.LENGTH_LONG).show();
        String district_id = bankNames[position];


        System.out.println("bobobo " + district_id);
    }
        @Override
    public void onNothingSelected(AdapterView<?> arg0) {


    }}


and the xml file is :




 <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="30dp"
    android:paddingTop="80dp"
    android:paddingRight="30dp"
    tools:context="com.cp.comp.add_comp">


    <EditText
        android:id="@+id/shop_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="اسم المحل"
        android:inputType="textPersonName"

        android:textColor="@color/colorText" />

    <EditText
        android:id="@+id/complainant_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:hint="اسم المشتكي"
        android:inputType="textPersonName"
        android:textColor="@color/colorText" />

    <EditText
        android:id="@+id/complainant_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="هوية المشتكي"
        android:inputType="textPersonName"

        android:textColor="@color/colorText" />

    <EditText
        android:id="@+id/mobile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:hint="موبايل المشتكي"
        android:inputType="textPersonName"
        android:textColor="@color/colorText" />



    <EditText
        android:id="@+id/address_detail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:hint="عنوان المحل التفصيلي"
        android:inputType="textPersonName"
        android:textColor="@color/colorText" />

    <EditText
        android:id="@+id/comp_type"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="طبيعة الشكوى"
        android:inputType="textPersonName"

        android:textColor="@color/colorText" />

    <EditText
        android:id="@+id/comp_detail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:hint="تفصيل الشكوى"
        android:inputType="textPersonName"
        android:textColor="@color/colorText" />
    <ProgressBar
        android:id="@+id/loading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:visibility="gone" />
    <EditText
        android:id="@+id/district_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="المحافظة"
        android:inputType="textPersonName"
        android:textColor="@color/colorText" />

    <Spinner
        android:id="@+id/simpleSpinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"

        android:layout_marginTop="10dp" />


    <Button
        android:id="@+id/btn_add_comp"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:layout_marginTop="30dp"
        android:backgroundTint="@color/colorPrimaryDark2"
        android:text="ارسال"
        android:textColor="@android:color/white" />

</LinearLayout>

3 个答案:

答案 0 :(得分:0)

要更改TextView的文本,只需执行以下操作:

myTextView.setText(district_id);

答案 1 :(得分:0)

package com.cp.comp;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class add_comp extends AppCompatActivity implements AdapterView.OnItemSelectedListener{

private EditText shop_name, complainant_name, complainant_id, mobile;
private EditText   district_id, address_detail, comp_type, comp_detail;
private EditText myTextView;
private ProgressBar loading;
private static String URL_ADDCOMP = "http://172.23.50.55/CP/add_comp.php";
private Button btn_add_comp;

ArrayList<String> listItems=new ArrayList<>();
ArrayAdapter<String> adapter;
Spinner sp;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_comp);
    sp=(Spinner)findViewById(R.id.spinner);
    adapter=new ArrayAdapter<String>(this,R.layout.spinner_layout,R.id.txt,listItems);
    System.out.println("frfrfrfr " + adapter);
    sp.setAdapter(adapter);

    btn_add_comp = findViewById(R.id.btn_add_comp);
    loading = findViewById(R.id.loading);
    shop_name = findViewById(R.id.shop_name);
    complainant_name = findViewById(R.id.complainant_name);
    complainant_id = findViewById(R.id.complainant_id);
    mobile = findViewById(R.id.mobile);
    district_id = findViewById(R.id.district_id);
    address_detail = findViewById(R.id.address_detail);
    comp_type = findViewById(R.id.comp_type);
    comp_detail = findViewById(R.id.comp_detail);


    btn_add_comp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Add_comp();
        }
    });
}
private void Add_comp() {

    loading.setVisibility(View.VISIBLE);
    btn_add_comp.setVisibility(View.GONE);
    final String shop_name        = this.shop_name.getText().toString().trim();
    final String complainant_name = this.complainant_name.getText().toString().trim();
    final String complainant_id   = this.complainant_id.getText().toString().trim();
    final String mobile           = this.mobile.getText().toString().trim();
    final String district_id      = this.district_id.getText().toString().trim();
    final String address_detail   = this.address_detail.getText().toString().trim();
    final String comp_type        = this.comp_type.getText().toString().trim();
    final String comp_detail      = this.comp_detail.getText().toString().trim();
    System.out.println("ya abed almoty");
    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_ADDCOMP,
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    try{
                        JSONObject jsonObject = new JSONObject(response);
                        String success = jsonObject.getString("success");
                        System.out.println("ya habibi");

                        if (success.equals("1")) {
                            Toast.makeText(add_comp.this, "تم إرسال الشكوى بنجاح!", Toast.LENGTH_SHORT).show();
                            System.out.println("ya belal");
                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(add_comp.this, "ارسال خاطئ! " + e.toString(), Toast.LENGTH_SHORT).show();
                        loading.setVisibility(View.GONE);
                        btn_add_comp.setVisibility(View.VISIBLE);
                        System.out.println("ya jehad");
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(add_comp.this, "ارسال خاطئ! " + error.toString(), Toast.LENGTH_SHORT).show();
                    loading.setVisibility(View.GONE);
                    btn_add_comp.setVisibility(View.VISIBLE);
                    System.out.println("ya morad");
                }
            })

    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("shop_name", shop_name);
            params.put("complainant_name", complainant_name);
            params.put("complainant_id", complainant_id);
            params.put("mobile", mobile);
            params.put("district_id", district_id);
            params.put("address_detail", address_detail);
            params.put("comp_type", comp_type);
            params.put("comp_detail", comp_detail);

            System.out.println("ya fahed" +  params.put("comp_type", comp_type));
            System.out.println("ya mymymymym" + myTextView);

            return params;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);


}
public void onStart(){
    super.onStart();
    add_comp.BackTask bt=new add_comp.BackTask();
    bt.execute();
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    Toast.makeText(getApplicationContext(),parent.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
    parent.getItemAtPosition(position).toString();
    System.out.println("mrmrmr " + parent.getItemAtPosition(position).toString());
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}

private class BackTask extends AsyncTask<Void,Void,Void> {
    ArrayList<String> list;
    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://172.23.50.55/CP/select_district_name.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);
            for(int i=0;i<jArray.length();i++){
                JSONObject jsonObject=jArray.getJSONObject(i);

                // add interviewee name to arraylist
                list.add(jsonObject.getString("dis_name"));
                System.out.println("txt_txt" + result);


            }
        }
        catch(JSONException e){
            e.printStackTrace();
        }
        return null;
    }
    protected void onPostExecute(Void result){
        listItems.addAll(list);
        adapter.notifyDataSetChanged();


    }

}
}

dis_name是我想传递给district_id的值

答案 2 :(得分:0)

package com.cp.comp;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class add_comp extends AppCompatActivity {
    private EditText shop_name, complainant_name, complainant_id, mobile;
    private EditText  district_id, address_detail, comp_type, comp_detail;
    private ProgressBar loading;
    private static String URL_ADDCOMP = "http://172.23.50.55/CP/add_comp.php";
    private Button btn_add_comp;



ArrayList<String> listItems=new ArrayList<>();
ArrayAdapter<String> adapter;
Spinner sp;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_comp);

    btn_add_comp = findViewById(R.id.btn_add_comp);
    loading = findViewById(R.id.loading);
    shop_name = findViewById(R.id.shop_name);
    complainant_name = findViewById(R.id.complainant_name);
    complainant_id = findViewById(R.id.complainant_id);
    mobile = findViewById(R.id.mobile);
    district_id = findViewById(R.id.district_id);
    address_detail = findViewById(R.id.address_detail);
    comp_type = findViewById(R.id.comp_type);
    comp_detail = findViewById(R.id.comp_detail);



    sp=(Spinner)findViewById(R.id.spinner);
    adapter=new ArrayAdapter<String>(this,R.layout.spinner_layout,R.id.txt,listItems);
    sp.setAdapter(adapter);
    btn_add_comp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Add_comp();
        }
    });
}
private void Add_comp() {
    loading.setVisibility(View.VISIBLE);
    btn_add_comp.setVisibility(View.GONE);
    final String shop_name        = this.shop_name.getText().toString().trim();
    final String complainant_name = this.complainant_name.getText().toString().trim();
    final String complainant_id   = this.complainant_id.getText().toString().trim();
    final String mobile           = this.mobile.getText().toString().trim();
    final String district_id      = this.district_id.getText().toString().trim();
    final String address_detail   = this.address_detail.getText().toString().trim();
    final String comp_type        = this.comp_type.getText().toString().trim();
    final String comp_detail      = this.comp_detail.getText().toString().trim();
    System.out.println("ya abed almoty");
    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_ADDCOMP,
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    try{
                        JSONObject jsonObject = new JSONObject(response);
                        String success = jsonObject.getString("success");
                        System.out.println("ya habibi");
                        if (success.equals("1")) {
                            Toast.makeText(add_comp.this, "تم إرسال الشكوى بنجاح!", Toast.LENGTH_SHORT).show();
                            System.out.println("ya belal");
                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(add_comp.this, "ارسال خاطئ! " + e.toString(), Toast.LENGTH_SHORT).show();
                        loading.setVisibility(View.GONE);
                        btn_add_comp.setVisibility(View.VISIBLE);
                        System.out.println("ya jehad");
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(add_comp.this, "ارسال خاطئ! " + error.toString(), Toast.LENGTH_SHORT).show();
                    loading.setVisibility(View.GONE);
                    btn_add_comp.setVisibility(View.VISIBLE);
                    System.out.println("ya morad");
                }
            })

    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("shop_name", shop_name);
            params.put("complainant_name", complainant_name);
            params.put("complainant_id", complainant_id);
            params.put("mobile", mobile);
            params.put("district_id", district_id);
            params.put("address_detail", address_detail);
            params.put("comp_type", comp_type);
            params.put("comp_detail", comp_detail);
            System.out.println("ya fahed" +  params.put("comp_type", comp_type));


            return params;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);


}
public void onStart(){
    super.onStart();
    add_comp.BackTask bt=new add_comp.BackTask();
    bt.execute();
}
private class BackTask extends AsyncTask<Void,Void,Void> {
    ArrayList<String> list;
    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://172.23.50.55/CP/select_district_name.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);
            for(int i=0;i<jArray.length();i++){
                JSONObject jsonObject=jArray.getJSONObject(i);
                // add interviewee name to arraylist
                list.add(jsonObject.getString("dis_name"));


            }
        }
        catch(JSONException e){
            e.printStackTrace();
        }
        return null;
    }
    protected void onPostExecute(Void result){
        listItems.addAll(list);
        adapter.notifyDataSetChanged();

    }
}
}

dis_name是我想传递给district_id的值