如何在Listview中实现onClick

时间:2012-10-29 13:34:42

标签: android android-listview onclick

我想这很简单。

当我点击它时,我想要在行中烘烤文本。这是我的代码:

  

公共类AndroidSQLite扩展了Activity {private SQLiteAdapter   mySQLiteAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ListView listContent = (ListView)findViewById(R.id.contentlist);
    mySQLiteAdapter = new SQLiteAdapter(this);
    mySQLiteAdapter = new SQLiteAdapter(this);
    mySQLiteAdapter.openToRead();

    Cursor cursor = mySQLiteAdapter.queueAll();
    startManagingCursor(cursor);

    String[] from = new String[]{SQLiteAdapter.KEY_NOME};
    int[] to = new int[]{R.id.text};

    SimpleCursorAdapter cursorAdapter =
     new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);

    listContent.setAdapter(cursorAdapter);

    mySQLiteAdapter.close();
}
     

}

谢谢!

5 个答案:

答案 0 :(得分:5)

写下来

listContent.setOnItemClickListener(new OnItemClickListener() {

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

                Log.i("get ItemIDPosition",
                        "" + adapter.getItemIdAtPosition(position));
                Log.i("get ItemATPosition", "" + adapter.getItemAtPosition(position));

            }
        });

答案 1 :(得分:3)

您可以使用

listContent.setOnItemClickListener(new OnItemClickListener() {

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

            }

        });

答案 2 :(得分:1)

像这样:

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

            String item = ...;

            Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();

        }
    });

答案 3 :(得分:1)

尝试使用此代码点击

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

        String text = <Get Your from database is here in this string>;

        Toast.makeText(getBaseContext(), text, Toast.LENGTH_LONG).show();

    }
});

答案 4 :(得分:0)

使用项目点击事件而非onclick。

listview.setOnItemClickListener(...);

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)

arg2是列表中的索引。