gpt4 book ai didi

Android ListView item点击事件监听引用当前activity委托(delegate)方法

转载 作者:行者123 更新时间:2023-11-28 21:53:13 26 4
gpt4 key购买 nike

我正在尝试 Android 开发,希望之前没有人问过这个问题,因为我不知道我的问题的正确 Android 术语。

我主要做iOS编程。我基本上想做的是创建一个 UITableView,它在每个单元格上都有一个点击事件,控制台应该只显示类似“水果”的内容,但我正在尝试使用 Android 执行此操作。

到目前为止,我为 onItemClickEventListener 准备了这个:

public class MainViewController extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main_view_controller);

// Get list view from xml
final ListView listView = (ListView) findViewById(R.id.listView);

// Define data source to show in list view
String[] items = new String[] {"Apple", "Banana", "Orange", "Mango", "Watermelon",
"Apple", "Banana", "Orange", "Mango", "Watermelon",
"Apple", "Banana", "Orange", "Mango", "Watermelon",
"Apple", "Banana", "Orange", "Mango", "Watermelon",
"Apple", "Banana", "Orange", "Mango", "Watermelon"};

// ------------------------------------------------------------------------
// Need to use Adapters to send data to list view
//
// First parameter: Context
// Second parameter: Layout for the row
// Third parameter: ID of the TextView to which data is written
// Forth parameter: array of data
// ------------------------------------------------------------------------

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, items);

listView.setAdapter(adapter);

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

android.util.Log.i("Console Output: ", "Fruits!");



}
});

...

这行得通,但我想知道我是否可以做类似的事情:

// psuedocode
void onCreate()
{
...

// ------------------------------------------------------------
// this doesn't work when I tried it, said something about
// cannot apply the onItemClick method on my ViewController
// and can only be applied on AdapterViews
//
// I thought View Controllers are meant to handle these kind of
// thing not Views ?
// ------------------------------------------------------------
listView.setOnItemClickListener(MainViewController.this);
}

// ----------------------------------------------------------------
// override the delegate method here instead of doing it
// inline in the onCreate() method above
// ----------------------------------------------------------------
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

android.util.Log.i("Console Output: ", "Fruits!");

});

我不确定内联添加点击监听器方法是否是一个好习惯,我正在努力避免疯狂的缩进 block 。

我说的有道理吗? :D

最佳答案

匿名方法(您使用的前一种方法)更难调试

您应该在 Activity 中实现 OnItemClickListener 或创建一个实现 OnItemClickListener 的私有(private)类来处理点击

关于Android ListView item点击事件监听引用当前activity委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27496745/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com