2015-12-14 114 views
1

我想用我的ListView上點擊的項目替換我的EditTexts。我用光標沿着ListView上的項目移動。但是,即使當我點擊它上面的第二個項目時,第一個項目也會顯示出來。我做錯了什麼?爲什麼我的光標不能在我的ListView中工作?

這裏是我的ListView onItemClicked代碼:

lv_people_info.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override   
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      // TODO Auto-generated method stub 
      people_info2 = new ArrayList<String>(); 


      Cursor cursor2 = db.query(PeopleEntry.TABLE_NAME,// Table 
        allColumns, // The columns to return 
        null, // The columns for the WHERE clause 
        null, // The values for the WHERE clause 
        null, // don't group the rows 
        null, // don't filter by row groups 
        null, // The sort order 
        null); // Limits the number of rows returned by the query 

      cursor2.moveToFirst(); 

      while (!cursor2.isAfterLast()) { 
       people_info2.add(cursor2.getString(1)); 
       people_info2.add(cursor2.getString(2)); 
       people_info2.add(cursor2.getString(3)); 

       et_name.setText(people_info2.get(0)); 
       et_amount.setText(people_info2.get(1)); 
       et_description.setText(people_info2.get(2)); 

       cursor2.moveToNext(); 

      } 

      cursor2.close(); 
     } 

    }); 

這是當我點擊ListView中的第二行會發生什麼:

enter image description here

+1

這段代碼應該顯示**總是最後一個**表項後**任意一行點擊** – Selvin

+0

@Selvin第一個,實際上。視圖是從數組開頭的內容設置的。 – njzk2

+0

是啊,你是對的...整個問題是沒有'int position,long id'傳遞給'onItemClick'回調函數 – Selvin

回答

-1

下面的代碼可以正常工作..

Inside Listview clickevent 

private SQLiteDatabase myDataBase=myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); 
String sql=select name,amount,description from yourTableName 
Cursor cr = myDataBase.rawQuery(sql, null); 

現在你在這裏添加

people_info2.add(cr .getString(1)); 
people_info2.add(cr .getString(2)); 
people_info2.add(cr .getString(3)); 

et_name.setText(people_info2.get(0)); 
et_amount.setText(people_info2.get(1)); 
et_description.setText(people_info2.get(2)); 
+0

[按排列編程](https://en.wikipedia.org/wiki/Programming_by_permutation)achivment gain ...這甚至不會編譯原因字面不是在aphostropes中......也是這樣的'db.rawQuery'是相同的作爲'db.query'爲什麼它會有幫助? – Selvin

+0

'cr'和'.getString'之間也有空格。這個答案中的零努力 –

+0

更好地被稱爲「」十億猴編程風格「。' – pskink

0

我想將我的EditTexts替換爲在ListView上單擊的項目。

那麼,這就是你應該做的,而不是使用遊標。你的列表視圖已經填充了來自Cursor.的數據,因此實際上不需要再次查詢它!

只需從行中接收您的onClickListener()中的必要信息。可以使用ViewHolder模式。創建名爲ViewHolder的簡單類,保留對您的名稱,數量,說明字段的引用。然後在您的newView()方法中創建新的ViewHolder並使用setTag()將其附加到您的視圖。

bindview()將數據綁定到它。然後,在OnClickListener中使用來自viewgetTag()

您可以在這裏看到https://developer.android.com/training/improving-layouts/smooth-scrolling.html

很好的例子

不要在UI線程查詢數據庫!在OnClick方法中查詢數據庫是一個錯誤。如果在閱讀完文章後,仍想查詢數據庫,至少應使用CursorLoaderAsyncTask

+0

'OnClickListener'因爲他正在使用'ListView'而不是'RecyclerView' ... ViewHolder應該只保留對行中(dums !!!)視圖的引用,而不是數據...和*我想用我的ListView上的項目替換我的EditTexts。* prolly表示:我將wana選定的行的數據綁定到EditTexts中 – Selvin

+0

Hi @RexSplode,很抱歉,我對此很陌生。因此,在創建ViewHolder類之後,如何將在ListView上單擊的數據附加到ViewHolder中? –

+0

將數據附加到bindview()方法中的行。在你的onClick()中,你只需調用getTag()來接收ViewHolder實例。之後,您可以調用viewHolder.textView.getText()來接受單擊行中名稱textView的文本。然後,editText.setText(retrieveString); – RexSplode

相關問題