如何将Spinner项目调整为TexView

时间:2017-10-23 20:51:45

标签: android listview android-spinner

我的应用程序在数据库中插入数据并将此数据保存在listView中。当用户单击listview中的项目时,textView表单中可以使用此信息。但是如何在textView中调整微调项呢?

package cm.mavis.crud2;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

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

import java.util.HashMap;


public class TampilPegawai extends AppCompatActivity implements View.OnClickListener{

private TextView editTextId;
private Spinner spinneragence;
private TextView editTextName;
private TextView editTextDesg;
private TextView editTextSalary;

private Button buttonUpdate;
private Button buttonDelete;

private String id;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tampil_pegawai);

    Intent intent = getIntent();

    id = intent.getStringExtra(Configuration.EMP_ID);

    spinneragence = (Spinner)findViewById(R.id.spinner1);
    editTextName = (TextView) findViewById(R.id.editTextName);
    editTextDesg = (TextView) findViewById(R.id.editTextDesg);
    editTextSalary = (TextView) findViewById(R.id.editTextSalary);

    buttonUpdate = (Button) findViewById(R.id.buttonUpdate);
    buttonDelete = (Button) findViewById(R.id.buttonDelete);

    buttonUpdate.setOnClickListener(this);
    buttonDelete.setOnClickListener(this);

    getEmployee();
}

private void getEmployee(){
    class GetEmployee extends AsyncTask<Void,Void,String>{
        ProgressDialog loading;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(TampilPegawai.this,"Fetching...","Patientez...",false,false);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();
            showEmployee(s);
        }

        @Override
        protected String doInBackground(Void... params) {
            RequestHandler rh = new RequestHandler();
            String s = rh.sendGetRequestParam(Configuration.URL_GET_EMP,id);
            return s;
        }
    }
    GetEmployee ge = new GetEmployee();
    ge.execute();
}

private void showEmployee(String json){
    try {
        JSONObject jsonObject = new JSONObject(json);
        JSONArray result = jsonObject.getJSONArray(Configuration.TAG_JSON_ARRAY);
        JSONObject c = result.getJSONObject(0);
        String agence = c.getString(Configuration.TAG_AGENCE);
        String name = c.getString(Configuration.TAG_NOM);
        String desg = c.getString(Configuration.TAG_POSITION);
        String sal = c.getString(Configuration.TAG_SALAIRE);

        spinneragence.
        editTextName.setText(name);
        editTextDesg.setText(desg);
        editTextSalary.setText(sal);

    } catch (JSONException e) {
        e.printStackTrace();
    }
}


private void updateEmployee(){
    final String agence = spinneragence.getSelectedItem().toString();
    final String name = editTextName.getText().toString().trim();
    final String desg = editTextDesg.getText().toString().trim();
    final String salary = editTextSalary.getText().toString().trim();

    class UpdateEmployee extends AsyncTask<Void,Void,String>{
        ProgressDialog loading;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(TampilPegawai.this,"Mise a jour...","Patientez...",false,false);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();
            Toast.makeText(TampilPegawai.this,s,Toast.LENGTH_LONG).show();
        }

        @Override
        protected String doInBackground(Void... params) {
            HashMap<String,String> hashMap = new HashMap<>();
            hashMap.put(Configuration.KEY_EMP_ID,id);
            hashMap.put(Configuration.KEY_EMP_NOM,name);
            hashMap.put(Configuration.KEY_EMP_AGENCE,agence);
            hashMap.put(Configuration.KEY_EMP_POSITION,desg);
            hashMap.put(Configuration.KEY_EMP_SALAIRE,salary);

            RequestHandler rh = new RequestHandler();

            String s = rh.sendPostRequest(Configuration.URL_UPDATE_EMP,hashMap);

            return s;
        }
    }

    UpdateEmployee ue = new UpdateEmployee();
    ue.execute();
}

private void deleteEmployee(){
    class DeleteEmployee extends AsyncTask<Void,Void,String> {
        ProgressDialog loading;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(TampilPegawai.this, "Mise a jour...", "Patientez...", false, false);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();
            Toast.makeText(TampilPegawai.this, s, Toast.LENGTH_LONG).show();
        }

        @Override
        protected String doInBackground(Void... params) {
            RequestHandler rh = new RequestHandler();
            String s = rh.sendGetRequestParam(Configuration.URL_DELETE_EMP, id);
            return s;
        }
    }

    DeleteEmployee de = new DeleteEmployee();
    de.execute();
}

private void confirmDeleteEmployee(){
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setMessage("Voulez vous supprimer ce salarié?");

    alertDialogBuilder.setPositiveButton("Oui",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    deleteEmployee();
                    startActivity(new Intent(TampilPegawai.this,TampilSemuaPgw.class));
                }
            });

    alertDialogBuilder.setNegativeButton("Non",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {

                }
            });

    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();
}

@Override
public void onClick(View v) {
    if(v == buttonUpdate){
        updateEmployee();
    }

    if(v == buttonDelete){
        confirmDeleteEmployee();
    }
}
} 

请帮我纠正这段代码。微调器的选择是在数据库中正确插入,但如何以textView形式显示?

1 个答案:

答案 0 :(得分:0)

在Android中,您可以将UI元素设置为不是它们的样式。例如,

class ViewController2: UIViewController, UITableViewDataSource, UITableViewDelegate {

let images = ["image1", "image2", "image3"]

// here are my functions for the my TableViewCell2

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return (images.count)
}

// the identifier of my prototype cell in the TableView is cell (and nome is just a label)

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell2
    cell.nome.text = images [indexPath.row]
    cell.profilepic.image = UIImage(named: images[indexPath.row])
    return (cell)
}

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

/* I put the tableView func inside the botton's action cause I want that my pic3 (declared in
ViewController3 as UIImage) is the pic I click in the TableView of my ViewController2 */

@IBAction func bottone(_ sender: Any) {
    func tableView2(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell2
        cell.nome.text = images [indexPath.row]
        cell.profilepic.image = UIImage(named: images[indexPath.row])
        performSegue(withIdentifier: "page3", sender: cell.profilepic.image)
        return (cell)
    }
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "page3" {
        let viewCont: ViewController3 = segue.destination as! ViewController3
        viewCont.pic3 = sender as! UIImage
    }
}
}

然后,

<style name="SpinnerText" parent="android:Widget.TextView"/>

你可能不得不调整它以获得你想要的东西,但你明白了。