将Json数组转换为Associative数组并将其绑定到AutoCompleteTextView--Android

将Json数组转换为Associative数组并将其绑定到AutoCompleteTextView--Android,android,arrays,json,Android,Arrays,Json,我有一个如下所示的数组 [{"InstituteID":"1","InstituteName":"Demo Institute"},{"InstituteID":"16","InstituteName":"Sheridan College"},{"InstituteID":"17","InstituteName":"iCent Prosp"},{"InstituteID":"18","InstituteName":"Seneca College"}] 我想将此数组转换为关联数组,并将机构名称设

我有一个如下所示的数组

[{"InstituteID":"1","InstituteName":"Demo Institute"},{"InstituteID":"16","InstituteName":"Sheridan College"},{"InstituteID":"17","InstituteName":"iCent Prosp"},{"InstituteID":"18","InstituteName":"Seneca College"}]
我想将此数组转换为关联数组,并将机构名称设置为AutoCompleteTextView

这是我的密码

    @Override
    protected void onPostExecute(String res) {

        try {

            JSONObject responseObject = new JSONObject(res);
            String status=responseObject.getString("Status");
            JSONArray detailsArray = responseObject.getJSONArray("InstituteList");

            String[] newarray = new Gson().fromJson(String.valueOf(detailsArray),String[].class);

            Log.i("Institute register assc" , String.valueOf(newarray));

            if(status.equals("true")) {

  autoTextView = (AutoCompleteTextView) findViewById(R.id.instname_field);
                ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1, ProgLanguages);
                autoTextView.setThreshold(1);
                autoTextView.setAdapter(arrayAdapter);

                Toast.makeText(getApplicationContext(), "Response successful",
                        Toast.LENGTH_LONG).show();
            }


        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
@覆盖
受保护的void onPostExecute(字符串res){
试一试{
JSONObject responseObject=新的JSONObject(res);
字符串状态=responseObject.getString(“状态”);
JSONArray detailsArray=responseObject.getJSONArray(“机构列表”);
String[]newarray=new Gson().fromJson(String.valueOf(detailsArray),String[].class);
Log.i(“学会寄存器关联”,String.valueOf(newarray));
if(status.equals(“true”)){
autoTextView=(AutoCompleteTextView)findViewById(R.id.instname_字段);
ArrayAdapter ArrayAdapter=新的ArrayAdapter(此,
android.R.layout.simple_list_item_1,编程语言);
autoTextView.setThreshold(1);
autoTextView.setAdapter(arrayAdapter);
Toast.makeText(getApplicationContext(),“响应成功”,
Toast.LENGTH_LONG).show();
}
}捕获(JSONException e){
e、 printStackTrace();
}
}
这是我在XML文件中自动完成视图的代码

   <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:id="@+id/autoCompt"
    android:orientation="vertical">

    <AutoCompleteTextView
        android:id="@+id/instname_field"
        android:layout_width="match_parent"
        android:hint="Type Institution Name"
        android:paddingLeft="10dp"
        android:textColor="#fff"
        android:background="#b8d1e5"
        android:layout_height="40dp"
        android:textSize="20dp"
        android:ems="10"/>
    </LinearLayout>

谢谢

添加日志


首先创建自定义的自动完成视图

public class CustomAutoCompleteView extends AutoCompleteTextView
{

public CustomAutoCompleteView(Context context)
{
    super(context);
}

public CustomAutoCompleteView(Context context, AttributeSet attrs)
{
    super(context, attrs);
}

public CustomAutoCompleteView(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
}

@Override
protected void performFiltering(final CharSequence text, final int keyCode)
{
    String filterText = "";
    super.performFiltering(filterText, keyCode);
}
}
为此使用自定义阵列适配器

public class CustomAdapter extends ArrayAdapter<AutoCompleteBean>
公共类CustomAdapter扩展了ArrayAdapter

在绑定后,请尝试此操作,这里我更改了用于获取机构名称数组的方法。这可能对你有用

    JSONObject responseObject = new JSONObject(res);
    String status=responseObject.getString("Status");
    ArrayList<String> listInstituteNames = new ArrayList<TextView>();
    JSONArray detailsArray = responseObject.getJSONArray("InstituteList");

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

          JSONObject obj = detailsArray.getJSONObject(i)

          listInstituteNames.add(obj.getString("InstituteName"))

    }

    autoTextView = (AutoCompleteTextView) findViewById(R.id.instname_field);
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
                            android.R.layout.simple_list_item_1, listInstituteNames);
    autoTextView.setThreshold(1);
    autoTextView.setAdapter(arrayAdapter);

    Toast.makeText(getApplicationContext(), "Response successful",Toast.LENGTH_LONG).show();
JSONObject responseObject=新的JSONObject(res);
字符串状态=responseObject.getString(“状态”);
ArrayList listInstituteNames=新的ArrayList();
JSONArray detailsArray=responseObject.getJSONArray(“机构列表”);

对于(inti=0;i只需将所有机构名称检索到字符串数组并传递它即可 对于这个简单的问题,不需要使用json

JSONObject responseObject = new JSONObject(res);
String status=responseObject.getString("Status");
JSONArray detailsArray = responseObject.getJSONArray("InstituteList");

String[] instituteNames = new String[detailsArray.length];
for (int i = 0; i <detailsArray.length() ; i++) {

      JSONObject obj = detailsArray.getJSONObject(i)

      //save the each institute name to this String[]
      instituteNames[i]=obj.getString("InstituteName");

}

autoTextView = (AutoCompleteTextView) findViewById(R.id.instname_field);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(<Activity-Name>.this,
                        android.R.layout.simple_list_item_1, instituteNames); //and pass here..
autoTextView.setThreshold(1);
autoTextView.setAdapter(arrayAdapter);

Toast.makeText(getApplicationContext(), "Response successful",Toast.LENGTH_LONG).show();
JSONObject responseObject=新的JSONObject(res);
字符串状态=responseObject.getString(“状态”);
JSONArray detailsArray=responseObject.getJSONArray(“机构列表”);
String[]instituteNames=新字符串[detailsArray.length];

对于(int i=0;i)你必须使用自定义的自动完成文本视图来实现它。你能告诉我这是怎么可能的吗?你的代码中有错误吗?当前代码的结果是什么?请看我的答案,这可能会有帮助you@JishadP:u在日志中为
log.i(“学会寄存器关联”,String.valueOf(newarray))获取的值是多少;
行?