ListView 和隐藏的 Id。怎么可能呢?

发布于 2024-11-01 01:58:27 字数 520 浏览 0 评论 0原文

我正在开发一个 Android 应用程序,现在我已经实现了一个 ListView,它显示连接到数据库的课程列表。

我想知道如何在名称中包含一个隐藏的 ID(来自数据库),以便一旦用户单击元素,应用程序就会转到所选课程的相对视图。

如何在课程视图内导航期间维护 id?

目前我的代码只是从数据库加载课程名称并在列表视图中设置:

ArrayAdapter<String> result = new ArrayAdapter<String>(CourseActivity.this, android.R.layout.simple_list_item_1);

for (CourseRecord c : get()) 
 result.add(c.getFullname());

lv.setAdapter(result);

显然我也可以执行 c.getid() 但我不知道将 id 放在哪里。

非常感谢。

PS:也许有人也有一个非常漂亮的列表视图图形?

I'm developing an application for android and now I have implemented a ListView that shows a list of courses, connected to a database.

I would like to know how to include, with the name, an hidden id (that come from the db) so that once the user click on the elements the app goes to the relative view of the selected courses.

And how can I maintain the id during the navigation inside the course-view?

At the moment my code just load the name of the courses from the db and set in the list view:

ArrayAdapter<String> result = new ArrayAdapter<String>(CourseActivity.this, android.R.layout.simple_list_item_1);

for (CourseRecord c : get()) 
 result.add(c.getFullname());

lv.setAdapter(result);

obviously I'm able to do also c.getid() but I don't where to put the id.

Thank you very much.

P.S.: Maybe does someone have also a really nice graphics of list view?

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

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

发布评论

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

评论(3

記憶穿過時間隧道 2024-11-08 01:58:27

像这样改变你的阵列适配器。

private ArrayAdapter<String> result = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1){
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
        }
        v.setTag(getMyIdForPosition(position));
        return convertView;
    }
};

并有一个项目单击处理程序来接收选定的 id

   private OnItemClickListener itemClickedHandler = new OnItemClickListener() {
    public void onItemClick(AdapterView parent, View v, int position, long id)
    {
        String myId = (String)v.getTag();
        doYourStuff(myId);
    }
    };

将侦听器分配给列表

myList= (ListView)findViewById(R.id.history);
myList.setOnItemClickListener(itemClickedHandler); 

change your array adapter like this.

private ArrayAdapter<String> result = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1){
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
        }
        v.setTag(getMyIdForPosition(position));
        return convertView;
    }
};

and have an item click handler to recieve the selected ids

   private OnItemClickListener itemClickedHandler = new OnItemClickListener() {
    public void onItemClick(AdapterView parent, View v, int position, long id)
    {
        String myId = (String)v.getTag();
        doYourStuff(myId);
    }
    };

assign the listener to the list

myList= (ListView)findViewById(R.id.history);
myList.setOnItemClickListener(itemClickedHandler); 
已下线请稍等 2024-11-08 01:58:27

您可以将 id 存储在隐藏的 TextView 中。在列表项 XML 中,将 'android:visibility="gone"' 添加到 TextView。同样,在点击处理程序中,您可以从文本视图中读取 id。

You can store the id in a hidden TextView. In the list item XML, add 'android:visibility="gone"' to the TextView. Likewise in the click handler you can read the id from the textview.

扭转时空 2024-11-08 01:58:27

您还可以使用 View 的 setTag(Object object) 方法存储 id。使用 getTag() 方法从该视图中提取该 id。

You can also store id using setTag(Object object) method of a View. Use getTag() method to extract that id from that view.

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