android arrayadapter类构造函数有错误

eufgjt7s  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(386)
package rappiddevelopers.destinationadvisor;

import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

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.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import rappiddevelopers.destinationadvisor.models.TranslatorModel;

public class MainActivity extends AppCompatActivity {

    private AutoCompleteTextView autoCompleteTextView;

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

        autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);

        new HttpGetTask().execute("http://192.168.0.107/abc/translator.php");

    }

    public class HttpGetTask extends AsyncTask<String, String, List<TranslatorModel>> {

        @Override
        protected List<TranslatorModel> doInBackground(String... params) {
            HttpURLConnection connection = null;
            BufferedReader reader = null;

            try {
                URL url = new URL(params[0]);
                connection = (HttpURLConnection) url.openConnection();
                connection.connect();

                InputStream stream = connection.getInputStream();

                reader = new BufferedReader(new InputStreamReader(stream));

                StringBuffer buffer = new StringBuffer();

                String line = "";
                while ((line = reader.readLine()) != null) {
                    buffer.append(line);
                }

                String finalJson = buffer.toString();
                JSONArray parentArray = new JSONArray(finalJson);

                List<TranslatorModel> translatorModelList = new ArrayList<>();

                for(int i= 0; i<parentArray.length();i++) {

                    JSONObject finalObject = parentArray.getJSONObject(i);

                    TranslatorModel translatorModel = new TranslatorModel();

                        translatorModel.setEnglish(finalObject.getString("englishSentence"));

                        translatorModelList.add(translatorModel);
                    }

                return translatorModelList;

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            } finally {
                if (connection != null) {
                    connection.disconnect();
                }

                try {
                    if (reader != null) {
                        reader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(List<TranslatorModel> data) {

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);

            autoCompleteTextView.setAdapter(adapter);
            autoCompleteTextView.setThreshold(1);

            super.onPostExecute(data);
        }
    }
}

希望将json字符串对象(EnglishEntence)中的值显示到autocompletetextview中。
translatormodel类具有用于JSonObject的getter和setter方法。
在arrayadapter声明行上发现错误。

gg0vcinb

gg0vcinb1#

您使用的是自定义对象而不是字符串,因此需要创建custum适配器来扩展arrayadapter,如下所示

public class MyClassAdapter extends ArrayAdapter<MyClass> {

    private static class ViewHolder {
        private TextView itemView;
    }

    public MyClassAdapter(Context context, int textViewResourceId, ArrayList<MyClass> items) {
        super(context, textViewResourceId, items);
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(this.getContext())
            .inflate(R.layout.listview_association, parent, false);

            viewHolder = new ViewHolder();
            viewHolder.itemView = (TextView) convertView.findViewById(R.id.ItemView);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        MyClass item = getItem(position);
        if (item!= null) {
            // My layout has only one TextView
                // do whatever you want with your string and long
            viewHolder.itemView.setText(String.format("%s %d", item.reason, item.long_val));
        }

        return convertView;
    }
}

相关问题