Android - 按下按钮后显示列表视图。

时间:2013-11-25 23:08:29

标签: android android-listview onclick android-sqlite android-button

不确定从哪里开始,但在这里:

我有一个应用程序接受带按钮的文本条目。按下按钮时,文本条目的值将保存到变量中,并运行几个SQLite查询。一个基于文本条目从数据库中选择信息,另一个在数据库上运行插入。

在查询结束时,列表视图应显示在显示选择查询信息的按钮下方。

这一切都有效,除非我输入另一个值并按下按钮,没有任何反应,我有一个Log调用来判断按钮是否被按下而且也没有显示。

在按钮单击事件上,它调用另一个运行查询和列表视图的活动,当我在被调用活动上放置一个finish()时,它允许每次单击按钮时运行插入查询但不显示列表视图

这是我的orderEntry活动:

public class orderEntry extends Activity  {

private EditText itemNumb;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.orderentry);

    //final Intent searchItem = new Intent(getApplicationContext(),DBListActivity.class);

    itemNumb = (EditText) findViewById(R.id.editTextUPC);
    Button enterButton = (Button) findViewById(R.id.btnEnterItem);

    enterButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //Intent searchItem = new Intent(getApplicationContext(),DBListActivity.class);

            Intent searchItem = new Intent(getApplicationContext(),inputOrder.class);
            String itemNumberValue = itemNumb.getText().toString();
            searchItem.putExtra("itemnumval", itemNumberValue);

            Log.v("Button Pushed","Yes");

            startActivity(searchItem);

        }
    });

}

}

以下是我的listview活动:

public class inputOrder extends ListActivity {
/** Called when the activity is first created. */
private final String MY_DATABASE_NAME = "hdw";
ArrayList results = new ArrayList();

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


    Intent intent = getIntent();
    String itemNumberValue = intent.getExtras().getString("itemnumval");

    SQLiteDatabase myDB = this.openOrCreateDatabase(MY_DATABASE_NAME, SQLiteDatabase.OPEN_READWRITE, null);
    try {

        String itemSel = "IMITNO";
        String itemDescInsert = "IMITD1";
        String itemDesc = "Test";

        Cursor c = myDB.rawQuery("Select * from items where IMITNO = '"+itemNumberValue+"'",  null );

            /* Check if our result was valid. */
        if (c != null) {

        c.moveToFirst(); // it's very important to do this action otherwise your Cursor object did not get work
            int itemNumCol = c.getColumnIndex("IMITNO");
            int itemDescCol = c.getColumnIndex("IMITD1");
             /* Check if at least one Result was returned. */
            if (c.isFirst()) {
                int i = 0;
                  /* Loop through all Results */
                do {
                    i++;
                    String itemNum = c.getString(itemNumCol);
                    itemDesc = c.getString(itemDescCol);
                    String itemDescColumName = c.getColumnName(itemDescCol);

                       /* Add current Entry to results. */
                    results.add("" + i + ": " + itemNum + " " + itemDesc + " " + itemDescColumName + ")");
                } while (c.moveToNext());
            }
        }

        String insertSQL = "INSERT INTO orders(itemnumber, desc) VALUES ('"+itemNumberValue+"', '"+itemDesc+"')";
        Log.v("Insert:",insertSQL);
        myDB.execSQL(insertSQL);

    } catch (SQLiteException e) {
    } finally {
        if (myDB != null)
            myDB.close();
    }

    this.setListAdapter(new ArrayAdapter(this, R.layout.listlayout, results));

    finish();
}
}

任何帮助都将不胜感激。

0 个答案:

没有答案