简体   繁体   English

在仍在添加项目的同时向每个listview项目添加onItemClickListener

[英]Adding onItemClickListener to each listview item while items are still being added

I'm not really sure how to explain this problem, but I'll do my best. 我不太确定如何解释这个问题,但我会尽力而为。 I have an app that records sound and after you're done recording, you have to rename the file and then the file is added to listview in another activity. 我有一个可以记录声音的应用程序,在完成记录后,您必须重命名该文件,然后将该文件添加到另一个活动中的listview中。

I have tab layout, so adding files to listview is a little bit more complicated, here's how I do it: 我具有选项卡布局,因此将文件添加到listview有点复杂,这是我的操作方法:

Recording activity: 记录活动:

if (getParent() instanceof FileNameProvider) {
    ((FileNameProvider) getParent()).onNewFileName(newFileName);
}

Tab layout activity: 选项卡布局活动:

public void onNewFileName(Editable filename) {
    LocalActivityManager activityManager = getLocalActivityManager();
    getTabHost().setCurrentTabByTag("Library");
    RecordedLibrary recLib = (RecordedLibrary) activityManager.getActivity("Library");
    recLib.setFileName(filename);
}

And finally, I get the new filename in my library (listview) activity: 最后,我在库(列表视图)活动中获得了新文件名:

public void setFileName(final Editable filename) {
    Log.d("2", "Set filename from first activity " + filename);
}

So, everytime I set a filename, it's automatically added to listview with this code: 因此,每次我设置文件名时,它都会使用以下代码自动添加到listview中:

public void setFileName(final Editable filename) {
    Log.d("2", "Set filename from first activity " + filename);

    //LISTVIEW (declared globally)
    fileNames.add(filename.toString()); 
    listView = (ListView) findViewById (R.id.mainListView);
    listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, fileNames);
    listView.setAdapter(listAdapter);

Now what I want to do next is to also automatically add onItemClickListener for each item added. 现在,我接下来要做的就是为添加的每个项目自动添加onItemClickListener。 I know I could do this with switch statement like this: 我知道我可以使用以下switch语句来做到这一点:

switch (position){
    case 0:
        //code
        break;
}

But this isn't possible in my case because everytime I record a file, this file has a different path, since there's a different name. 但这在我的情况下是不可能的,因为每次记录文件时,该文件都有不同的路径,因为名称不同。 This is how I tried to do it: 这是我尝试执行的操作:

listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
            Toast toast = Toast.makeText(getApplicationContext(), filename, Toast.LENGTH_SHORT);
            toast.show();
        }

    });

This doesn't work either because everytime I add a new file to the listview, the filename variable changes, so this code works only as long as I only add one item to listview. 这也不起作用,因为每次我将新文件添加到列表视图时,文件名变量都会更改,因此该代码仅在我仅将一个项目添加到列表视图时才起作用。 As soon as I add the second item to listview, toast will show the second file's name no matter which item I click. 一旦将第二个项目添加到列表视图中,无论我单击哪个项目,toast都会显示第二个文件的名称。

I hope everyone understand's the problem. 希望每个人都明白问题所在。 Let me know if I should add any more info. 让我知道是否应该添加更多信息。

SOLUTION: 解:

listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
            Toast toast = Toast.makeText(getApplicationContext(), fileNames.get(arg2), Toast.LENGTH_SHORT);
            toast.show();
        }

    });

As you can see, I simply changed the 'filaname' to 'fileNames.get(arg2)', please see this answer for more details. 如您所见,我只是将'filaname'更改为'fileNames.get(arg2)',请查看此答案以获取更多详细信息。

You should NOT create and set a new ListAdapter each and every time you set a file name. 每次设置文件名时,都不要创建和设置新的ListAdapter。

Adapters should only be created once and attached to a ListView once per lifecycle, which is probably why the second time you add a file, only the second file name is being returned. 适配器只能创建一次,并在每个生命周期中一次附加到ListView,这可能就是为什么第二次添加文件时,仅返回第二个文件名的原因。 You are overriding the previous Adapter and data each time you call setFileName . 每次调用setFileName时,您将覆盖以前的Adapter和数据。

You don't need to add an onClickListener to every object in your list. 您无需向列表中的每个对象添加onClickListener As you indicated above that you're use a seperate activity to house your ListView I would (1) make that activity extend ListActivity (2) use onListItemClick 如上文所述,您将使用单独的活动来容纳ListView我将(1)将该活动扩展为ListActivity (2)使用onListItemClick

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

}

This way if you keep track of which file is added in which order (above you mention you're using an ArrayList to store file names) you could use the position or id value (depending on what kind of adapter is backing your list) to retrieve the file name. 这样,如果您跟踪哪个文件以什么顺序添加(上面提到过,您正在使用ArrayList来存储文件名),则可以使用position或id值(取决于支持列表的适配器类型)检索文件名。 You can play around with which collection works best for retrieving the file data (though the array list probably works fine if you're adding and removing these values in a predictable order). 您可以尝试使用哪个集合最适合检索文件数据(尽管如果以可预测的顺序添加和删除这些值,则数组列表可能会很好地工作)。

You can also do this using the onItemClickListener you have in place. 您也可以使用onItemClickListener进行此操作。 Arg2 is the position of the item in the list. Arg2是项目在列表中的位置。 Just take that value and get the file you need from the ArrayList storing the file extention. 只需取该值并从存储文件扩展名的ArrayList获取所需的文件即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM