如何使用mysql数据库在AutoCompleteTextView中获取数据?

时间:2016-09-30 05:55:46

标签: android mysql autocompletetextview

我使用了android教程中的演示,但它产生了像

这样的错误
    Error:(116, 36) error: cannot find symbol variable menu
Note: C:\Users\sachin\Desktop\AddVisit\app\src\main\java\com\example\sachin\addvisit\MainActivity.java uses or overrides a deprecated API.

完整的代码如下:

Mainactivity.java

    package com.example.sachin.addvisit;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MainActivity extends Activity {

    InputStream is=null;
    String result=null;
    String line=null;

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

        try
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://10.0.2.2/portal/autocomplete.php");
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            Log.e("Pass 1", "connection success ");
        }
        catch(Exception e)
        {
            Log.e("Fail 1", e.toString());
            Toast.makeText(getApplicationContext(), "Invalid IP Address",Toast.LENGTH_LONG).show();
        }

        try
        {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
            Log.e("Pass 2", "connection success ");
        }
        catch(Exception e)
        {
            Log.e("Fail 2", e.toString());
        }

        try
        {
            JSONArray JA=new JSONArray(result);
            JSONObject json= null;
            final String[] str1 = new String[JA.length()];
            for(int i=0;i<JA.length();i++)
            {
                json=JA.getJSONObject(i);
                str1[i]=json.getString("name");
            }

            final AutoCompleteTextView text = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
            final List<String> list = new ArrayList<String>();

            for(int i=0;i<str1.length;i++)
            {
                list.add(str1[i]);
            }

            Collections.sort(list);

            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getApplicationContext(),
                    android.R.layout.simple_spinner_item, list);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            text.setThreshold(1);
            text.setAdapter(dataAdapter);

            text.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
// TODO Auto-generated method stub
                    Toast.makeText(getBaseContext(), list.get(arg2).toString(), Toast.LENGTH_SHORT).show();
                }
            });

        }
        catch(Exception e)
        {
            Log.e("Fail 3", e.toString());
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

activity_main

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<AutoCompleteTextView android:id="@+id/autoCompleteTextView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="60dp"
    android:ems="13"
    android:hint="AutoCompleteTextView"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <requestFocus />
</AutoCompleteTextView>

    </RelativeLayout>

我想在我的 AutoCompleteTextview 中从数据库中获得建议我尝试了很多,但它根本不起作用。 please refer this link i tried this demo

logcat的

   10-01 10:14:04.119 2921-2921/com.example.sachin.addvisit E/Fail 1: android.os.NetworkOnMainThreadException
10-01 10:14:04.152 2921-2921/com.example.sachin.addvisit E/Fail 2: java.lang.NullPointerException: lock == null
10-01 10:14:04.153 2921-2921/com.example.sachin.addvisit E/Fail 3: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference

1 个答案:

答案 0 :(得分:0)

错误来自此方法。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

错误为cannot find symbol variable menu,原因为uses or overrides a deprecated API.

您可能正在使用旧版本的appcompat库。在build.gradle中引用新版本的appcompat。

<强>更新

根据更新的问题,您将获得android.os.NetworkOnMainThreadException。当应用程序尝试在其主线程上执行网络操作时,将引发此异常。要解决此问题,请在AsyncTask中运行您的代码。

同时将其添加到AndroidManifest.xml文件。

<uses-permission android:name="android.permission.INTERNET"/>