Android 从自定义 ListView 访问元素

发布于 2024-10-07 05:07:15 字数 2922 浏览 0 评论 0原文

这个问题的标题可能很糟糕,但很难确定一个标题。所以这是问题。

我有一个应用程序,可以打开数据库并根据内容创建自定义 ListView。因此,此过程涉及一些文件:

Main.java - opens the database and stores the List<MyClass> of contents
main.xml - main activity layout with the ListView
MyAdapter.java - extends BaseAdapter and calls MyAdapterView based on the context
MyAdapaterView.java - inflates the View from MyAdapater based on row.xml
row.xml - layout of each custom row of the ListView

这工作正常。我是 Android 新手,但从结构上来说,这似乎是每个人都建议构建自定义 ListView 的方式。

如何从 ListView 中检索数据?例如,行的一部分是复选框。如果用户按下复选框来激活/停用特定行,我如何通知主应用程序?

谢谢,

编辑:

Main.java

public class MyApplication extends Activity 
    implements OnItemClickListener, OnItemLongClickListener
{
    private List<MyClass> objects;
    private ListView lvObjects;
    private MyAdapter myAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        objects = new ArrayList<MyClass>(); // setup the list
        lvObjects = (ListView)findViewById(R.id.lvObjectList);
        lvObjects.setOnItemClickListener(this);
        lvObjects.setOnItemLongClickListener(this);

        loadDatabase(DATABASE);

        myAdapter = new MyAdapter(this, objects);
        lvObjects.setAdapter(myAdapter); 
    }

    ...

    public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
    {       
        // This is executed when an item in the ListView is short pressed
    }

    public void onItemLongClick(AdapterView<?> parent, View v, int position, long id) 
    {
        // This is executed when an item in the ListView is long pressed

        registerForContextMenu(lvObjects);
        v.showContextMenu();
    }

MyAdapter.java

public class MyAdapter extends BaseAdapter
{
    private Context context;
    private List<MyClass> list;

    public RuleAdapter(Context context, List<MyClass> list)
    {
        this.context = context;
        this.list = list;
    }

    ...

    public View getView(int position, View view, ViewGroup viewGroup) 
    {
        MyClass entry = list.get(position);
        return new MyAdapterView(context,entry);
    }
}

MyAdapterView.java

public class MyAdapterView extends LinearLayout
{   
    public MyAdapterView(Context context, MyClass entry) 
    {
        super(context);

        this.setOrientation(VERTICAL);
        this.setTag(entry);

        View v = inflate(context, R.layout.row, null);

        // Set fields based on entry object

        // When this box is checked or unchecked, a message needs to go
        // back to Main.java so the database can be updated
        CheckBox cbActive = (CheckBox)v.findViewById(R.id.cbActive);

        addView(v);
    }
}

The title for this question might be poor, but but it was hard to nail down a title. So here is the question.

I have an application that opens a database and creates a custom ListView based on the contents. So there are a few files involved in this process:

Main.java - opens the database and stores the List<MyClass> of contents
main.xml - main activity layout with the ListView
MyAdapter.java - extends BaseAdapter and calls MyAdapterView based on the context
MyAdapaterView.java - inflates the View from MyAdapater based on row.xml
row.xml - layout of each custom row of the ListView

This works fine. I am new to Android, but structurally this seems to be how everyone recommends building custom ListViews.

How do I retrieve data from the ListView? For instance, part of the row is a checkbox. If the user presses the checkbox to activate/deactivate the particular row, how do I notify the main application about that?

Thanks,

EDIT:

Main.java

public class MyApplication extends Activity 
    implements OnItemClickListener, OnItemLongClickListener
{
    private List<MyClass> objects;
    private ListView lvObjects;
    private MyAdapter myAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        objects = new ArrayList<MyClass>(); // setup the list
        lvObjects = (ListView)findViewById(R.id.lvObjectList);
        lvObjects.setOnItemClickListener(this);
        lvObjects.setOnItemLongClickListener(this);

        loadDatabase(DATABASE);

        myAdapter = new MyAdapter(this, objects);
        lvObjects.setAdapter(myAdapter); 
    }

    ...

    public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
    {       
        // This is executed when an item in the ListView is short pressed
    }

    public void onItemLongClick(AdapterView<?> parent, View v, int position, long id) 
    {
        // This is executed when an item in the ListView is long pressed

        registerForContextMenu(lvObjects);
        v.showContextMenu();
    }

MyAdapter.java

public class MyAdapter extends BaseAdapter
{
    private Context context;
    private List<MyClass> list;

    public RuleAdapter(Context context, List<MyClass> list)
    {
        this.context = context;
        this.list = list;
    }

    ...

    public View getView(int position, View view, ViewGroup viewGroup) 
    {
        MyClass entry = list.get(position);
        return new MyAdapterView(context,entry);
    }
}

MyAdapterView.java

public class MyAdapterView extends LinearLayout
{   
    public MyAdapterView(Context context, MyClass entry) 
    {
        super(context);

        this.setOrientation(VERTICAL);
        this.setTag(entry);

        View v = inflate(context, R.layout.row, null);

        // Set fields based on entry object

        // When this box is checked or unchecked, a message needs to go
        // back to Main.java so the database can be updated
        CheckBox cbActive = (CheckBox)v.findViewById(R.id.cbActive);

        addView(v);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寻找一个思念的角度 2024-10-14 05:07:15

如何从
列表视图?

如果您想知道 ListView 中的哪个项目被单击,可以使用 OnItemClickListenerListview.setOnItemClickListenerHello ListView 教程中有一个示例。

一旦您知道选择了哪个项目,您就知道该项目在整个Adapter 中的位置,并且您拥有View。如果需要,您可以使用视图来获取子元素,通常情况下,位置足以让您知道单击了“模型”的哪一部分。在您的示例中,它会准确地告诉您 List 中的哪个项目被单击。

如果您想区分被单击的列表项和列表项中的可聚焦视图(如复选框),那么就涉及更多一点。默认情况下,该复选框会将焦点从列表项中移出。不过,您可以切换此设置并将其设置为 false,具体取决于您想要执行的操作。有关详细信息,请参阅此问题

如果需要区分ListView项单击和CheckBox单击,有几种方法。一种方法是仅维护适配器内是否单击某个项目的状态。

例如:

MovieAdapter

MyMovies (ListView)

然后你只需使用普通的“onListItemClick”和适配器来告诉什么被点击了,什么没有被点击。 (这只是一种方法,还有多种方法。)

How do I retrieve data from the
ListView?

If you want to know which item in the ListView was clicked, you use an OnItemClickListener and Listview.setOnItemClickListener. There's an example of this in the Hello ListView tutorial.

Once you know which item is selected, you then know the position of that item in the overall Adapter, and you have the View. You can use the View to get sub-elements if needed, generally though the position is enough to let you know what part of your "model" was clicked. In your example it would tell you exactly which item in your List<MyClass> was clicked.

If you want to distinguish between the list item being clicked, and a focusable view in the list item (like a checkbox), then that's a little more involved. The checkbox, by default, will hikack the focus from the list item. You can toggle this though and make it false, depending on what you're trying to do. See this question for more info.

If you need to distinguish between the ListView item click and the CheckBox click, there are several ways. One way is to just maintain the state of whether or not an item is clicked inside the adapter.

For example:

MovieAdapter

MyMovies (ListView)

Then you just use the normal "onListItemClick" and the adapter to tell what is clicked and what isn't. (This is just one way to do this, there are several.)

苯莒 2024-10-14 05:07:15

那么您有一个创建视图的自定义光标适配器吗?当您在 getView() 中创建视图时,只需在该视图上设置一个侦听器来侦听您想要的任何内容。可能是点击时

So you have a custom cursoradapter that creates the views? When you create the view in getView(), just set up a listener on that view to listen for whatever you want. Probably onClick

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文