在微调器选择之后填充textview

gr8qqesn  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(286)

从sql数据库填充文本视图时遇到困难。我有一个列填充微调器,然后我希望两个textview由来自微调器选择的同一行的mysql列填充。我找不到要添加到onselecteditem部分的正确代码。
主活动.java

public class MainActivity extends AppCompatActivity implements OnItemSelectedListener{

    Context c;
    TextView colorDensity;
    Spinner colorSpinner= findViewById(R.id.colorSpinner);

    ArrayList<String> colors=new ArrayList<>();

    final static String urlAddress = "http://www.burtkuntzhandjobs.org/dbcolors.php";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new Downloader(MainActivity.this,urlAddress,colorSpinner).execute();
        colorDensity = (TextView)findViewById(R.id.colorDensity);
        colorSpinner.setOnItemSelectedListener(this);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        Toast.makeText(this,"Select Color", Toast.LENGTH_SHORT).show();
    }
}

数据分析器.java

public class DataParser extends AsyncTask<Void,Void,Integer> {

    Context c;
    Spinner colorSpinner;
    String jsonData;

    ProgressDialog pd;
    ArrayList<String> colors=new ArrayList<>();

    public DataParser(Context c, Spinner colorSpinner, String jsonData) {
        this.c = c;
        this.colorSpinner = colorSpinner;
        this.jsonData = jsonData;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pd = new ProgressDialog(c);
        pd.setTitle("Parse");
        pd.setMessage("Parsing");
        pd.show();
    }

    @Override
    protected Integer doInBackground(Void...params) {
        return this.parseData();
    }

    @Override
    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);

        pd.dismiss();

        if(result == 0){
            Toast.makeText(c,"Unable to Parse",Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(c,"Parse Successful",Toast.LENGTH_SHORT).show();
            ArrayAdapter adapter = new ArrayAdapter(c,android.R.layout.simple_list_item_1,colors);
            colorSpinner.setAdapter(adapter);
        }
    }

    private int parseData() {
        try {
            JSONArray ja=new JSONArray(jsonData);
            JSONObject jo=null;
            colors.clear();
            Colors s=null;

            for (int i = 0; i < ja.length(); i++) {
                jo = ja.getJSONObject(i);

                int ui = jo.getInt("ui");
                String color=jo.getString("color");
                String density = jo.getString("density");
                String strainer = jo.getString("strainer");

                s = new Colors();
                s.setIu(ui);
                s.setColor(color);
                s.setDensity(density);
                s.setStrainer(strainer);

                colors.add(color);
            }
            return 3;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return 0;
    }
}

下载程序.java

public class Downloader extends AsyncTask<Void,Void,String> {

    Context c;
    String urlAddress;
    Spinner colorSpinner;

    ProgressDialog pd;

    public Downloader(Context c, String urlAddress, Spinner colorSpinner) {
        this.c = c;
        this.urlAddress = urlAddress;
        this.colorSpinner = colorSpinner;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pd = new ProgressDialog(c);
        pd.setTitle("Fetch");
        pd.setMessage("Fetching");
        pd.show();
    }

    @Override
    protected String doInBackground(Void...params) {
        return this.downloadData();
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        pd.dismiss();

        if(s == null) {
            Toast.makeText(c,"Unable to Retrieve",Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(c,"Success",Toast.LENGTH_SHORT).show();

            DataParser parser=new DataParser(c,colorSpinner,s);
            parser.execute();
        }
    }

    private String downloadData() {
        HttpURLConnection con= (HttpURLConnection) Connector.connect(urlAddress);
        if(con == null) {
            return null;
        }

        InputStream is = null;
        try {
            is = new BufferedInputStream(con.getInputStream());
            BufferedReader br=new BufferedReader(new InputStreamReader(is));

            String line = null;
            StringBuffer response=new StringBuffer();

            if(br != null){
                while ((line=br.readLine()) !=null) {
                    response.append(line+"\n");
                }
                br.close();

            } else {
                return null;
            }
            return response.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(is != null){
                try{
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return  null;
    }
}
ie3xauqp

ie3xauqp1#

进行以下更改并添加所需代码,
1.移除 Spinner colorSpinner= findViewById(R.id.colorSpinner); 从类变量。
2.添加 Spinner colorSpinner= findViewById(R.id.colorSpinner); 在oncreate`method中。
看这个尖头,

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_spinner);
    Spinner colorSpinner= findViewById(R.id.colorSpinner);
    new Downloader(this, urlAddress,colorSpinner).execute();
    colorDensity = (TextView)findViewById(R.id.colorDensity);
    colorSpinner.setOnItemSelectedListener(this);
}

3.访问颜色列表 DataParser 班级,

public class DataParser extends AsyncTask<Void,Void,Integer> {
Context c;
Spinner colorSpinner;
String jsonData;

ProgressDialog pd;
ArrayList<String> colors=new ArrayList<>();
private static ArrayList<Colors> colorsList=new ArrayList<>(); // add this line

public DataParser(Context c, Spinner colorSpinner, String jsonData) {
    this.c = c;
    this.colorSpinner = colorSpinner;
    this.jsonData = jsonData;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();

    pd = new ProgressDialog(c);
    pd.setTitle("Parse");
    pd.setMessage("Parsing");
    pd.show();
}

@Override
protected Integer doInBackground(Void...params) {
    return this.parseData();
}

@Override
protected void onPostExecute(Integer result) {
    super.onPostExecute(result);

    pd.dismiss();

    if(result == 0){
        Toast.makeText(c,"Unable to Parse",Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(c,"Parse Successful",Toast.LENGTH_SHORT).show();
        ArrayAdapter adapter = new ArrayAdapter(c,android.R.layout.simple_list_item_1,colors);
        colorSpinner.setAdapter(adapter);
    }
}

private int parseData() {
    try {
        JSONArray ja=new JSONArray(jsonData);
        JSONObject jo=null;
        colors.clear();
        Colors s=null;

        for (int i = 0; i < ja.length(); i++) {
            jo = ja.getJSONObject(i);

            int ui = jo.getInt("ui");
            String color=jo.getString("color");
            String density = jo.getString("density");
            String strainer = jo.getString("strainer");

            s = new Colors();
            s.setUi(ui);
            s.setColor(color);
            s.setDensity(density);
            s.setStrainer(strainer);

            colors.add(color);
            colorsList.add(s); // add this line
        }
        return 3;
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return 0;
}

public static List<Colors> getColorsList() { // add this method
    return colorsList;
}
}

4.相应地设置密度 onItemSelected() 活动类的方法。

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    List<Colors> colorsList = DataParser.getColorsList();
    colorDensity.setText(colorsList.get(position).getDensity());
}

相关问题