将字符串从一个MainActivity传递到另一个DB类Java

时间:2015-10-21 07:33:55

标签: java c++ string sqlite

如何从填充符onItemSelected事件填充的TextView中传递字符串,以便在DBController类中进行查询。我无法实现意图:

Main14Activity

Nidd.setText(imc_met.substring(imc_met.lastIndexOf("Id") + 3));
String ado = Nidd.getText().toString();

DBController

String selectQuery = "SELECT  * FROM proinfo WHERE Name= " + .... ;

意图不起作用,因为我不知道如何使这个工作。函数getintent()。在DB类中返回红色。

Main14Activity Code:

public class Main14Activity extends ListActivity {
TextView lbl;
DBController controller = new DBController(this);
ListView lv;
final Context context = this;
ListAdapter adapter;
ArrayList<HashMap<String, String>> myList;
ArrayList<String> myList1;
Spinner spinner12;
TextView Nidd;


@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main142);
    lbl = (TextView) findViewById(R.id.txtresulttext);
    Nidd = (TextView) findViewById(R.id.Nidd);

    lv = getListView();
    spinner12 = (Spinner) findViewById(R.id.spinner12);
    myList1 = controller.getAllProducts1();
    Spinner My_spinner = (Spinner) findViewById(R.id.spinner12);
    ArrayAdapter adapter1 = new ArrayAdapter(this, R.layout.spinner_row,
            myList1);
    My_spinner.setAdapter(adapter1);
    controller = new DBController(getApplicationContext());
    SQLiteDatabase db = controller.getWritableDatabase();
    String tableName = "proinfo";

    db.execSQL("delete from " + tableName);

    try {

        try {
            String filename = "/Download/jobtex.csv";
            String path = Environment.getExternalStorageDirectory() + filename;
            FileReader file = new FileReader(path);
            BufferedReader buffer = new BufferedReader(file);
            ContentValues contentValues = new ContentValues();
            String line = "";
            db.beginTransaction();
            while ((line = buffer.readLine()) != null) {
                String[] str = line.split(",", 3);  // defining 3 columns with null or blank field //values acceptance
                //Job, Nid
                // String company = str[0].toString();
                String company = str[0].toString();
                String Name = str[1].toString();
                // String Price = str[2].toString();
                contentValues.put("Company", company);
                contentValues.put("Name", Name);
                // contentValues.put("Price", Price);

                db.insert(tableName, null, contentValues);
                lbl.setText("Successfully Updated Database.");


            }
            db.setTransactionSuccessful();
            db.endTransaction();
        } catch (IOException e) {
            if (db.inTransaction())
                db.endTransaction();
            Dialog d = new Dialog(this);
            d.setTitle(e.getMessage().toString() + "first");
            d.show();
            // db.endTransaction();
        }


        myList = controller.getAllProducts();
        if (myList.size() != 0) {
            ListView lv = getListView();
            ListAdapter adapter = new SimpleAdapter(Main14Activity.this, myList,
                    R.layout.v, new String[]{"Company", "Name"}, new int[]{
                    R.id.txtproductcompany, R.id.txtproductname});

            setListAdapter(adapter);

            lbl.setText("Data Imported");


            myList1 = controller.getAllProducts1();
            My_spinner = (Spinner) findViewById(R.id.spinner12);
            adapter1 = new ArrayAdapter(this, R.layout.spinner_row,
                    myList1);
            My_spinner.setAdapter(adapter1);

            My_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                                           int arg2, long arg3) {

                    String imc_met = spinner12.getSelectedItem().toString();

                    Nidd.setText(imc_met.substring(imc_met.lastIndexOf("Id") + 3));
                    String ado = Nidd.getText().toString();
                    Fragment fragment = new Fragment();

                   ???????????????
                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub

                }
            });

        }
    } finally {

    }
}

}

DBController:DB Class

public class DBController extends SQLiteOpenHelper  {




private static final String LOGCAT = null;
public DBController(Context applicationcontext) {
    super(applicationcontext, "PrdouctDB.db", null, 1);  // creating DATABASE
    Log.d(LOGCAT, "Created");
}
@Override
public void onCreate(SQLiteDatabase database) {

    String query;
    query = "CREATE TABLE IF NOT EXISTS proinfo ( Id INTEGER PRIMARY KEY, Company TEXT,Name TEXT)";
    database.execSQL(query);


}






@Override
public void onUpgrade(SQLiteDatabase database, int version_old,
                      int current_version) {
    String query;
    query = "DROP TABLE IF EXISTS proinfo" ;
    database.execSQL(query);
    onCreate(database);
}
public ArrayList<HashMap<String, String>> getAllProducts() {
    ArrayList<HashMap<String, String>> proList;

    proList = new ArrayList<HashMap<String, String>>();
    String selectQuery = "SELECT  * FROM proinfo WHERE Name=" + ??????????????????? ;
    SQLiteDatabase database = this.getWritableDatabase();
    Cursor cursor = database.rawQuery(selectQuery, null);


    if (cursor.moveToFirst()) {
        do {
            //Id, Company,Name,Price
            HashMap<String, String> map = new HashMap<String, String>();

            map.put("Id", cursor.getString(0));
            map.put("Company", cursor.getString(1));
            map.put("Name", cursor.getString(2));
           // map.put("Price", cursor.getString(3));

            proList.add(map);


        } while (cursor.moveToNext());
    }
    return proList;
}
public ArrayList<String> getAllProducts1() {
    ArrayList<String> proList1;

    proList1 = new ArrayList<String>();
    String selectQuery = "SELECT  * FROM proinfo ";
    SQLiteDatabase database = this.getWritableDatabase();
    Cursor cursor = database.rawQuery(selectQuery, null);


    if (cursor.moveToFirst()) {
        do {
            String add = "Job Id";
            String as = "Site:";
            String space = " ";
            proList1.add( as + space + cursor.getString(1) + space + add + space + cursor.getString(2));




        } while (cursor.moveToNext());
    }
    cursor.close();
    database.close();
    return proList1;
}


}

0 个答案:

没有答案