2016-07-20 7 views
-1

ここでは、ユーザーからfav sportsを取得するMultiAutocompleteTextViewがあります。スポーツは現在配列に格納されています。この値をMySQLデータベースの配列に格納します。ここで配列をMySqlのデータベースに入れてください。

は私のコードです:あなたは、AndroidでのMySQLデータベースを持っていない

String[] str = {"Foot Ball", "Volley Ball", "Basket Ball", 
      "Golf", "F1", "Marathon "}; 

    final MultiAutoCompleteTextView mt = (MultiAutoCompleteTextView) 
      findViewById(R.id.MyTags); 

    mt.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); 

    ArrayAdapter<String> adp = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, str); 
    mt.setThreshold(1); 
    mt.setAdapter(adp); 


    Button add=(Button)findViewById(R.id.add); 
    assert add != null; 
    add.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String text = mt.getText().toString(); 

      if (text != null && text.length() > 0) { 
       text = text.substring(0, text.length() - 1); 
      } 
      String sports[] = text.split(","); // countries array will have all the countries entered in multiAutoCompleteTextView 


       for (String s : countries) { 
        Toast.makeText(Profile.this, s, Toast.LENGTH_SHORT).show(); 

      } 

     } 
    }); 

} 
+0

あなたは試してみたい場合はsqliteやRealmを使用するよりも、Androidデバイスにデータを保存してください。 Webサービスを作成する必要がある以上にサーバーにデータを保存し、データをサーバーに送信するためのWebサービスを呼び出す場合。 – iAndroid

答えて

0

、あなたはそのどちらかSqliteを上のAndroid上で行うか、MySQLサーバ上のWebサービスポストデータを使用して試すことができます。

+0

外部Mysqlサーバーを使用している場合 –

+0

外部mysqlサーバーに送信するだけでデータを受け取ったときに、データを「ポスト」または「取得」パラメータとして受け取るWebサービスを作成できます。あなたはそれのための任意のWebベースの言語を使用することができます。 (Asp.net、PHP、Javaなど) –

0

MultiAutoCompleteTextViewから複数の分割テキストを取得し、配列に格納し、アンドロイドからMySQLへ渡すために:

public class WorkNow extends AppCompatActivity { 
public static final String URL_ADD = "YOUR LINK"; 
public static String KEY_C="tags"; 



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

    Button add = (Button) findViewById(R.id.buttonAdd); 


    String[] str = {"India", "China", "Japan", 
      "Nepal", "Italy", "Germany "}; 

    final MultiAutoCompleteTextView mt = (MultiAutoCompleteTextView) 
      findViewById(R.id.multiAutoCompleteTextView1); 

    mt.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); 

    ArrayAdapter<String> adp = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, str); 
    mt.setThreshold(1); 
    mt.setAdapter(adp); 


    assert add != null; 
    add.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String text = mt.getText().toString(); 

      if (text != null && text.length() > 0) { 
       text = text.substring(0, text.length() - 1); 
      } 
      String countries[] = text.split(","); // countries array will have all the countries entered in multiAutoCompleteTextView 

      for (final String s : countries) { 
       Toast.makeText(WorkNow.this, s, Toast.LENGTH_SHORT).show(); 

       class AddEmployee extends AsyncTask<Void, Void, String> { 

        ProgressDialog loading; 

        @Override 
        protected void onPreExecute() { 
         super.onPreExecute(); 
         loading = ProgressDialog.show(WorkNow.this, "Adding...", "Wait...", false, false); 
        } 

        @Override 
        protected void onPostExecute(String s) { 
         super.onPostExecute(s); 
         loading.dismiss(); 
         Toast.makeText(WorkNow.this, s, Toast.LENGTH_LONG).show(); 
        } 

        @Override 
        protected String doInBackground(Void... v) { 
         HashMap<String, String> params = new HashMap<>(); 
         params.put(KEY_C, s); 


         RequestHandler rh = new RequestHandler(); 
         String res = rh.sendPostRequest(URL_ADD, params); 
         return res; 
        } 
       } 

       AddEmployee ae = new AddEmployee(); 
       ae.execute(); 
      } 
     } 
    }); 
} 


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

} 

そしてここでは、PHPのコードです:

<?php 
$con = mysqli_connect("YOUR_HOST_NAME", "USER_ID", "PASSWOR", "YOUR_DATABASE"); 
if($_SERVER['REQUEST_METHOD']=='POST'){ 

    //Getting values 
    $tags = $_POST['tags']; 



    //Creating an sql query 
    $sql = "INSERT INTO tags (tags) VALUES ('$tags')"; 

    //Importing our db connection script 


    //Executing query to database 
    if(mysqli_query($con,$sql)){ 
     echo 'Employee Added Successfully'; 
    }else{ 
     echo 'Could Not Add Employee'; 
    } 

    //Closing the database 
    mysqli_close($con); 
} 
+0

それは正しい方法です:) –

関連する問題