来自新字符串查询中的表的字符串

时间:2012-11-21 16:08:41

标签: java android sqlite

我收到了这段代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_clicked);
    String query = "SELECT ROWID _id, linie"
            + " FROM linie";

    ArrayList<String> HaltestellenListe = CursorToArray(getApplicationContext(), query, new String[] {});

    final ListView lvD = (ListView) findViewById(R.id.lvClicked);


    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.activity_clicked, R.id.tvClicked, HaltestellenListe);
    lvD.setAdapter(arrayAdapter);
    lvD.setDividerHeight(8);



    lvD.setOnItemClickListener(new OnItemClickListener() { 
             @Override
             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                  String linie = lvD.getItemAtPosition(position).toString();

                 }
        });






}

public ArrayList<String> CursorToArray(Context ApplicationContext, String query, String[] whereClause){
    ArrayList<String> ArrayList = new ArrayList<String>();

    DataBaseHelper dbHelper = new DataBaseHelper(getBaseContext());

    Cursor c = dbHelper.dbHelper(ApplicationContext).rawQuery(query, whereClause);
    dbHelper.close();

    c.moveToFirst();


    while (!c.isAfterLast()){

        if (c.isNull(c.getColumnIndex("linie")) == false){
            ArrayList.add(c.getString(c.getColumnIndex("linie")));  
        }


        c.moveToNext();
    }
    c.close();

    return ArrayList;
}

使用setonitemclicklistener我得到点击位置的字符串。如何将此字符串用于新的字符串查询,如下所示:

lvD.setOnItemClickListener(new OnItemClickListener() { 
             @Override
             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                  setContentView(R.layout.activity_clicked);
                  String newQuery = "SELECT ROWID _id, linie" + "FROM linie" + "WHERE name =" + lvD.getItemAtPosition(position).toString();                   
                  ArrayList<String> ClickedList = CursorToArray(getApplicationContext(), newQuery, new String[] {});
                  final ListView lvD = (ListView) findViewById(R.id.lvClicked);                 
                  ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.activity_clicked, R.id.tvClicked, ClickedList);
                  lvD.setAdapter(arrayAdapter);
                  lvD.setDividerHeight(8);

                 }
        });

我希望新文本位于同一列表视图中 基本上我想使用新查询中第一个查询的字符串。 我是非常新的java所以请帮助我。

1 个答案:

答案 0 :(得分:1)

  1. 您需要在查询中放置名称值的引号,并在其间放置空格,如下所示:

    String newQuery = "SELECT ROWID _id, linie" + " FROM linie " + 
               " WHERE name = '" + lvD.getItemAtPosition(position).toString()+"'";
    
  2. 由于您的lvd是最终版,因此可直接在您的方法中使用。删除下面的行以找到它:

    //remove this line
    final ListView lvD = (ListView) findViewById(R.id.lvClicked);