-1

我得到了这个代码:

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很陌生,所以请帮助我。

4

1 回答 1

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); 
    
于 2012-11-21T16:22:02.207 回答