2011-09-22 34 views
0

嗨下面是一些代碼ive一直在玩,但是當我調試它永遠不會得到onitemclicklisten例程任何人都可以幫助嗎?程序似乎永遠不會去onclick聽

package sanderson.swords.mobilesales; 

import java.util.ArrayList; 
import java.util.HashMap; 

import android.app.Activity; 
import android.app.ListActivity; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 

import android.widget.AdapterView.OnItemClickListener; 

public class OrderProductSearch extends Activity { 
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>(); 
    HashMap<String,String> item = new HashMap<String,String>(); 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    try{ 
     setContentView(R.layout.orderproducts); 
    } 
    catch (Exception e) { 
     // 
     String shaw=""; 
     shaw = e.getMessage(); 
    } 

    //Create view of the list where content will be stored 
    final ListView listContent = (ListView)findViewById(R.id.orderproductlistview); 

    //Set for fast scrolling 
    listContent.setFastScrollEnabled(true); 

    //Create instance of the database 
    final DbAdapter db = new DbAdapter(this); 

    //Open the Database and read from it 
    db.openToRead(); 

    //Routine to call all product sub groups from the database 
    final Cursor cursor = db.getAllSubGroupProduct(); 
    //Manages the cursor 
    startManagingCursor(cursor); 
    int i=0; 
    cursor.moveToFirst(); 
    while (cursor.getPosition() < cursor.getCount()) { 
     item.put("ProdName",cursor.getString(2)); 
     item.put("ProdSize", cursor.getString(3)); 
     item.put("ProdPack",cursor.getString(4)); 
     item.put("OrdQty","0"); 

     //list.add(item); 
     list.add(i, item); 
     item = new HashMap<String,String>(); 
     cursor.moveToNext(); 
     i = i + 1; 

    } 

    String[] from = new String[] {"ProdName", "ProdSize", "ProdPack", "OrdQty"}; 

    int[] to = new int[] { R.id.productlinerow, R.id.productlinerow2, R.id.productlinerow3, R.id.productlinerow4}; 
    SimpleAdapter notes = new SimpleAdapter(OrderProductSearch.this,list,R.layout.productlinerow,from,to);     
    listContent.setAdapter(notes); 


    //Close the database 
    db.close();  



    listContent.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) {  

      String neil = "test"; 

      neil = neil + "test"; 

     } 
     }); 


    } 




} 
+0

你如何驗證它沒有得到onItemClick? –

+0

該列表是否顯示其所有項目?偵聽器例程僅在單擊項目時調用 - 實際上是否有項目? –

+0

嗨屏幕顯示我的列表沒有問題,我甚至可以在edittext中輸入qty。邁克我在例程中放置了一些簡單的代碼,並在那裏放置了一個斷點 – shawrie

回答

0

驗證點擊偵聽器的更簡單的方法是在其中添加一些logcat打印,例如,

listContent.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View view, 
      int position, long id) {  

     Log.d(TAG, "Click!"); 
    } 
    }); 
+0

嘗試過,但它永遠不會在那裏? – shawrie

+0

我不太確定,但可能應該嘗試使用OnItemSelectedListener(http://developer.android.com/reference/android/widget/AdapterView.OnItemSelectedListener.html)。 至少很容易嘗試...... :) – orjan

0

如果您在

listContent.setOnItemClickListener(new OnItemClickListener() { 

設置一個斷點,當你點擊它可能不會打破。

嘗試

String neil = "test"; 

和調試設置斷點。

+0

嗨試過,但它永遠不會到那裏? – shawrie

+0

也許這是OnItemSelectedListener作爲orjan下面提到,嘗試 – Jack