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

时间:2015-06-20 06:05:44

标签: android arrays json

我有一个像下面的数组..

[{"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();
        }
    }

这是我在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>

...谢谢

添加了日志

enter image description here

3 个答案:

答案 0 :(得分:1)

试试这个,在这里我改变你用于获取机构名称数组的方法。这可能对你有用。

    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();

答案 1 :(得分:0)

首先创建自定义自动填充视图

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);
}
}

为此使用自定义ArrayAdapter

public class CustomAdapter extends ArrayAdapter<AutoCompleteBean>

比绑定后

答案 2 :(得分:-1)

只需将所有学院名称检索到字符串数组并传递它 没有必要使用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();