使用下拉微调器中的选定项查询数据库

时间:2015-08-08 17:54:12

标签: java android sqlite

我有一个可以写入和删除数据库的应用程序。我有2个微调器,它们是从该数据库中的2个不同列生成的。我想对与给定下拉微调器中所选项匹配的所有记录运行查询。截至目前一切正常,但是当我从下拉列表中选择一个项目时,没有任何反应。我知道我错过了一些不确定的地方或地点。请帮忙

这是我的Dbhelper.Java

package com.gamingbrothers.pat.passwordencrypter;
//Created by Pat 8/6/15

import android.app.AlertDialog;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.List;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

Dbhelper myDB;
Button btnAdd;
Button btnDelete;
Button btnView;
Button btnUpdate;
Spinner spinner;
Spinner spinner2;
String itemSelected;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myDB = new Dbhelper(this);

    //initializing objects
    editUserName = (EditText) findViewById(R.id.editText_user);
    editPassword = (EditText) findViewById(R.id.editText_pass);
    editApplication = (EditText) findViewById(R.id.editText_app);
    editID = (EditText) findViewById(R.id.editText_id);
    editCustomer = (EditText) findViewById(R.id.editText_customer);
    btnAdd = (Button) findViewById(R.id.button_Add);
    btnDelete = (Button) findViewById(R.id.button_Delete);
    btnView = (Button) findViewById(R.id.button_View);
    btnUpdate = (Button) findViewById(R.id.button_update);
    spinner = (Spinner) findViewById(R.id.spinner);
    spinner2 = (Spinner) findViewById(R.id.spinner2);


    AddData();
    viewAll();
    UpdateData();
    DeleteData();
    loadSpinnerData();
    loadSpinner2Data();



}


public void loadSpinner2Data() {
    //db handler
    Dbhelper db2 = new Dbhelper(getApplicationContext());

    //Spinner drop elem.
    List<String> apps = db2.getAllApps();

    //Create adapter for spinner
    ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, apps);

    // Drop down layout style - list view with radio button
    dataAdapter2
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    //attaching data adapter
    spinner2.setAdapter(dataAdapter2);
}


private void loadSpinnerData() {
    // database handler
    Dbhelper db = new Dbhelper(getApplicationContext());

    // Spinner Drop down elements
    List<String> customers = db.getAllCustomers();

    // Creating adapter for spinner
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, customers);


    // Drop down layout style - list view with radio button
    dataAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);


    // attaching data adapter to spinner
    spinner.setAdapter(dataAdapter);

}


public void DeleteData() {
    btnDelete.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Integer deletedRows = myDB.deleteData(editID.getText().toString());
                    if(deletedRows >0)
                        Toast.makeText(MainActivity.this, "Data Deleted", Toast.LENGTH_LONG).show();
                    else
                        Toast.makeText(MainActivity.this, "Data not Deleted", Toast.LENGTH_LONG).show();
                }
            }
    );
}

//creating update data function
public void UpdateData() {
    btnUpdate.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean isUpdated = myDB.updateData(editID.getText().toString(),
                            editUserName.getText().toString(),
                            editPassword.getText().toString(),
                            editApplication.getText().toString(),
                            editCustomer.getText().toString());
                    //Parse update completion
                    if (isUpdated == true)
                        Toast.makeText(MainActivity.this, "Data Updated", Toast.LENGTH_LONG).show();
                    else
                        Toast.makeText(MainActivity.this, "Data not Updated", Toast.LENGTH_LONG).show();

                }
            });
}

//add data to database
public void AddData() {
    btnAdd.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean isInserted = myDB.insertData(editUserName.getText().toString(),
                            editPassword.getText().toString(),
                            editApplication.getText().toString(),
                            editCustomer.getText().toString());
                    //parse add completion
                    if (isInserted == true)
                        Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_LONG).show();
                    else
                        Toast.makeText(MainActivity.this, "Data not Inserted", Toast.LENGTH_LONG).show();
                }
            });
}

//Get results from database when button is clicked
public void viewAll() {
    btnView.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Cursor result = myDB.getAllData();
                    //Check if database is empty
                    if (result.getCount() == 0) {
                        //Show Message
                        showMessage("Error", "No Data Found");
                        return;
                    }

                    StringBuffer buffer = new StringBuffer();
                    while (result.moveToNext()) {
                        buffer.append("ID:" + result.getString(0) + "\n");
                        buffer.append("User Name:" + result.getString(1) + "\n");
                        buffer.append("Password:" + result.getString(2) + "\n");
                        buffer.append("App:" + result.getString(3) + "\n");
                        buffer.append("Customer:" + result.getString(4) + "\n\n");
                    }

                    //Show all data
                    showMessage("Data", buffer.toString());
                }
            }
    );
}

//show data
public void showMessage(String title,String Message){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(Message);
    builder.show();
}


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    String label = parent.getItemAtPosition(position).toString();
    myDB.getChoiceData().toString();
    Toast.makeText(parent.getContext(), "Here is your information: "+ label,Toast.LENGTH_LONG).show();


}

@Override
public void onNothingSelected(AdapterView<?> parent) {

    }
}

这是我的MainActivity.Java

$app->get('/category/:name', function($name){
  //the render files are found under the templates folder
header('Location: searchPage.php?crs_category=$name');
});

如果你需要我发布我的XML只是问,但我认为这不是必要的,我没有发布我的logcat,因为它没有给我一个错误。

1 个答案:

答案 0 :(得分:0)

从您发布的代码中,您似乎缺少选定的项目侦听器,例如,将以下内容添加到您的onCreate:

 spinner.setOnItemSelectedListener(this);
 spinner2.setOnItemSelectedListener(this);