Android,自定义AutoCompleteTextView没有显示建议

时间:2014-06-30 12:03:55

标签: android autocompletetextview

我正在尝试使用Array Adapter显示自定义建议,建议数据来自数据库。我使用onTextChange从数据库获取数据但未显示建议​​。使用Neo4j数据库的代码是可以的,我从数据库获取值。我是Android新手。请问有谁能告诉我问题出在哪里? 这是我的代码。 在此先感谢。

matri_suggestion xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/imageView_sugg"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:orientation="vertical" >
        <TextView
            android:id="@+id/matri_sugg_name"
            android:layout_width="120dp"
            android:layout_height="wrap_content"            
            android:text="Name" />

        <TextView
            android:id="@+id/matri_sugg_proEdu"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:text="Profesn,Education" />

        <TextView
            android:id="@+id/matri_sugg_cityDist"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CurrentCity,Dist" />
    </LinearLayout>

</LinearLayout>

MainActivity `

public class MainActivity extends Activity {
    AutoCompleteTextView search;
    String resultList;
    String matri_photo, matri_name, matri_prof_degree, matri_town_dist;
    List<AutoCompleteItems> clist = new ArrayList<AutoCompleteItems>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        search = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);

        search.addTextChangedListener(new TextWatcher() {

            public Context context;

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {

                String temp = arg0.toString();

                if (temp.length() > 0) {

                    try {

                        resultList = new Matri_Sugg(context).execute(temp)
                                .get();
                        setJSONdata(resultList);

                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (ExecutionException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                    int arg2, int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub

            }
        });

        AutocompleteAdapter adapter = new AutocompleteAdapter(this,
                R.layout.matri_suggessions, clist);

        search.setAdapter(adapter);

    }



    void setJSONdata(String result) {
        try {

            JSONObject obj = new JSONObject(resultList);
            // Log.d("in setJSONdata", resultList);
            final JSONArray dataArray = obj.getJSONArray("data");
            Log.d("data Array", dataArray.length() + "");
            MainActivity ma=new MainActivity();

            try {

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

                    ma.matri_photo = dataArray.getJSONArray(i).getString(0);
                    ma.matri_name = dataArray.getJSONArray(i).getString(1) + " "
                            + dataArray.getJSONArray(i).getString(2);
                    ma.matri_prof_degree = dataArray.getJSONArray(i).getString(3)
                            + " " + dataArray.getJSONArray(i).getString(4);
                    ma.matri_town_dist = dataArray.getJSONArray(i).getString(5)
                            + " , " + dataArray.getJSONArray(i).getString(6);

                    AutoCompleteItems aci = new AutoCompleteItems(
                            R.drawable.npf, ma.matri_name, ma.matri_prof_degree,
                            ma.matri_town_dist);
                    ma.clist.add(aci);

                }



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

        }

        catch (JSONException e) {

            e.printStackTrace();

        } catch (ArrayIndexOutOfBoundsException r) {

            r.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

` AsynchTask用于从数据库获取值。 的 Matri_Sugg

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

    String result;
    List<String> resultarray;
    Array[][] res;
    URL url;
    String queryString, matri_photo, matri_name, matri_prof_degree, matri_town_dist ;
    Context context;

    public Matri_Sugg(Context context) {
        // TODO Auto-generated constructor stub
        this.context = context;

    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub\

        String query = params[0];

        try {

            DBConnect.connect();

            queryString = "{\"query\":\"match (n:Woftos_Profile),n-[r:Has_Matrimony]->m where m.Status='Active' and n.Gender<>'Male' and (n.First_Name =~ '(?i)"+query+".*' or n.Last_Name=~ '(?i)"+query+".*') return distinct m.Profile_Photo, n.First_Name,n.Last_Name,n.Profession,n.Degree,n.Town_C,n.District,id(m),id(n),n.Gender LIMIT 5\", \"params\":{}}";

            DBConnect.sendQuery(queryString);

            result = DBConnect.readInputStream();

            Log.d("server response", result);

        } catch (MalformedURLException e) {

            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (NullPointerException e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        return result;
    }
}

AutoCompleteItems

    public class AutoCompleteItems {
    public int Matri_pic;
    public String Name=null;
    public String Prof_Edu=null;
    public String City_Dist=null;
    public AutoCompleteItems(int image, String name, String prof_edu, String city_dist){

        this.Matri_pic=image;
        this.Name=name;
        this.Prof_Edu=prof_edu;
        this.City_Dist=city_dist;

    }

}

AutoCompleteAdapter

public class AutocompleteAdapter extends ArrayAdapter<AutoCompleteItems>{

    public List<AutoCompleteItems> data;
    public Context context;
    public View row;
    public int resource = R.layout.matri_suggessions;

    public AutocompleteAdapter(Context context, int resource,
            List<AutoCompleteItems> objects) {
        super(context, resource, objects);

        this.data = new ArrayList<AutoCompleteItems>();
        this.context = context;
        this.data.addAll(objects);

    }

    static class DataHolder {
        ImageView iv;
        TextView txt_name, txt_prof_edu, txt_city_dist;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        row = convertView;
        DataHolder holder = new DataHolder();
        if (row == null) {

            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(resource, parent, false);

            holder.iv = (ImageView) row.findViewById(R.id.imageView_sugg);
            holder.txt_name = (TextView) row.findViewById(R.id.matri_sugg_name);
            holder.txt_prof_edu = (TextView) row.findViewById(R.id.matri_sugg_proEdu);
            holder.txt_city_dist = (TextView) row.findViewById(R.id.matri_sugg_cityDist);

            row.setTag(holder);

        } else {

            holder = (DataHolder) row.getTag();
            AutoCompleteItems info = data.get(position);

            holder.iv.setImageResource(info.Matri_pic);
            holder.txt_name.setText(info.Name);
            holder.txt_prof_edu.setText(info.Prof_Edu);
            holder.txt_city_dist.setText(info.City_Dist);

        }
        return row;
    }

}

0 个答案:

没有答案