2011-04-21 16 views
0

下面是代碼:如何在列表視圖中選擇一個行不使用ListActivity

public class Main extends Activity { 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    // Find your views 
    final ListView listPro = (ListView) findViewById(R.id.listProperty); 

    DBAdapter db = new DBAdapter(this); 

    db.open(); 

    final Cursor c = db.getAllProperties(); 
    startManagingCursor(c); 

    // Create the adapter 
    MainAdapter adapter = new MainAdapter(this, c); 
    listPro.setAdapter(adapter); 

    // here is where I think I should put the code to select the row but I 
      // haven't been able to figure out how to do it. 
      // all I need to get out is the row id number.. please help 

    Button addPropBut = (Button) findViewById(R.id.mainButton); 
    addPropBut.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 

      Intent i7 = new Intent(Main.this, Home.class); 
      startActivity(i7); 

     } 

    }); 
} 

} 
+0

你選擇什麼元素? – Robert 2011-04-21 05:25:39

回答

2

試試這個,希望它會幫助你。

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    } 

您可以通過使用Log或System.out.println()獲取ID號碼;

1

而不是使用Button的setOnClickListener使用ListView的setOnItemClickListener。

listPro.setOnItemClickListener(new OnItemClickListener() { 

       @Override 
       public void onItemClick(AdapterView<?> parent, View view, 
         int position, long id) { 


       } 
      }); 

id參數會給你所需的id。

  • Salil。
+0

Salil你真棒!我一直在尋找這個好幾天。就是這麼簡單。謝謝!!! – xappz 2011-04-21 14:17:19

+0

@ user712343 - 很高興我能幫到你。如果這個答案有幫助,那麼你能否將其標記爲已接受的答案。 – Salil 2011-04-21 17:14:35

相關問題