如何在列表视图中选择不使用ListActivity的行

时间:2011-04-21 05:19:28

标签: android listview

以下是代码:

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);

        }

    });
}

}

2 个答案:

答案 0 :(得分:2)

尝试这个,希望它会帮助你。

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

您可以使用Log或System.out.println();

获取ID号

答案 1 :(得分:1)

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

listPro.setOnItemClickListener(new OnItemClickListener() {

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


                }
            });

id参数将为您提供所需的ID。

  • 萨里尔。