我在名称为(activity_add_comp)的活动中有一个名称为(@ + id / spinner)的微调框。我需要将微调器值传递给名称为(@ + id / district_id)的textView并抛出代码:“ params.put(” district_id“,district_id);”。我的意思是将Spinner值设置为district_id而不是self。
我的Java代码是:
package com.cp.comp;
import android.content.Intent;
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.TextView;
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"));
System.out.println("rtrtrt" + jsonObject);
}
}
catch(JSONException e){
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result){
listItems.addAll(list);
adapter.notifyDataSetChanged();
}
}
}
activity_add_comp.xml:
<?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="number"
android:digits="0123456789"
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/spinner"
android:layout_width="match_parent"
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>
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/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
/>
</LinearLayout>
答案 0 :(得分:0)
import React, { useState } from 'react';
const Main = props => {
const [state, setState] = useState({
noteArray: [],
noteText: '',
});
const addNote = () => {
if(state.noteText){
var d = new Date();
let x = {
'date': d.getFullYear() +
"/" + (d.getMonth() + 1 ) +
"/" + d.getDate(),
'note': state.noteText
}
setState({...state, noteArray: [...state.noteArray, x]});
console.log(state.noteArray);
}
}
.......................the rest of code
}
OR
String text = mySpinner.getSelectedItem().toString();
editText.setText(text);
答案 1 :(得分:0)
public class add_comp extends AppCompatActivity {
//String will be fetched from spinner
String spinnerText;
//Your spinner
Spinner sp;
//Your edittext
TextView textview;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_comp);
//This will do exactly what you need by getting the selected item string for you
spinnerText = sp.getSelectedItem().toString();
//This will pass the selected string into your edittext
textview.setText(spinnerText);
}
}
答案 2 :(得分:0)
先生,它给了我这样的错误: java.lang.NullPointerException:尝试在com.cp.comp.add_comp.Add_comp(add_comp.java:114)上的空对象引用上调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)' 请在您的代码中记录我的评论
public class add_comp extends AppCompatActivity {
//String will be fetched from spinner
String spinnerText;
//Your spinner
Spinner sp;
//Your edittext
TextView textview;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_comp);
//This will do exactly what you need by getting the selected item string for you
spinnerText = sp.getSelectedItem().toString(); My comment: here is the error
//This will pass the selected string into your edittext
textview.setText(spinnerText);
}
}
答案 3 :(得分:0)
首先使用类实现ONItemClickListner,然后覆盖
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id)
,然后选择值后,将在方法getApplicationContext(),data[position]
中获得结果
这是mainactivity的代码
spin.setOnItemSelectedListener(this);
//Creating the ArrayAdapter instance having the country list
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,country);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);