Android - 如何创建可点击的列表视图? [英] Android - How to create clickable listview?

查看:36
本文介绍了Android - 如何创建可点击的列表视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我在 listview 中的所有列表项打开到一个新页面,所以每个 listview 项打开到一个我可以使用的新黑页上.我根本不知道如何实现这一点.我已经搜索了几个小时,但找不到我的解决方案的答案.如果有人可以展示和/或解释如何执行此操作而不是提供链接,我们将不胜感激,但两者都有帮助.

I want to make all my list items in the listview open up into a new page, so each listview item opens up onto a new black page that I can use. I don't know how to implement this at all. I have searched for hours on end and can't find an answer to my solution. It would be much appreciated if someone could show and/or explain how to do this instead of providing a link, but either is helpful.

这是我目前的代码:

  <string-array name="sections">
    <item >Pro Constructive</item>
    <item >Con Constructive</item>
    <item >1st Speaker Cross</item>
    <item >Pro Rebbutal</item>
    <item >Con Rebuttal</item>
    <item >2nd Speaker Cross</item>
    <item >Pro Summary</item>
    <item >Con Summary</item>
    <item >Grand Cross</item>
    <item >Pro Final Focus</item>
    <item >Con Final Focus</item>
</string-array>

这是在我的 string.xml 中

This is in my string.xml

    <ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:entries="@array/sections" >
</ListView>

这是在我的 activity_main.xml 中.

This is in my activity_main.xml.

我应该从哪里开始使列表中的每个项目都可点击并打开新页面?

Where do I go from here to make each item in my list clickable and able to open up onto a new page?

提前致谢!

Logcat 不再相关.

Logcat no longer relevant.

推荐答案

其实很简单:

这是带有 ListView 的 Activity,它实现了 OnItemClickListener:

This is your Activity with the ListView, it implements an OnItemClickListener:

public class MainActivity extends Activity implements OnItemClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        //* *EDIT* * 
        ListView listview = (ListView) findViewById(R.id.listView1);
        listview.setOnItemClickListener(this);
    }

    public void onItemClick(AdapterView<?> l, View v, int position, long id) {
        Log.i("HelloListView", "You clicked Item: " + id + " at position:" + position);
            // Then you start a new Activity via Intent
            Intent intent = new Intent();
            intent.setClass(this, ListItemDetail.class);
            intent.putExtra("position", position);
            // Or / And
            intent.putExtra("id", id);
            startActivity(intent);
    }

编辑

上面的代码将放在您的 MainActivity.java 中.我将类的名称更改为 MainActivity,将 contentView 更改为 setContentView(R.layout.activity_main) - 这些名称是在 Eclipse 中新创建的 Android 项目的名称.
另请参阅//* Edit * 下的 2 个新行 - 这些将设置监听器以单击列表中的项目.

The above code would be placed in your MainActivity.java. I changed the name of the class to MainActivity and the contentView to setContentView(R.layout.activity_main) - The names are those of a freshly created Android Project in Eclipse.
Please see also the 2 new lines under //* Edit * - those will set the Listener for clicks on items in the list.

您的 activity_main.xml 应如下所示:

Your activity_main.xml should look like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:entries="@array/sections" >
    </ListView>
</RelativeLayout>

`res/values/` 文件夹中的 array.xml(不是 string.xml)看起来像这样

The array.xml (not string.xml) in your `res/values/` folder looks like this

<resources>
    <string-array name="sections">
        <item >Pro Constructive</item>
        <item >Con Constructive</item>
        <item >1st Speaker Cross</item>
        <item >Pro Rebbutal</item>
        <item >Con Rebuttal</item>
        <item >2nd Speaker Cross</item>
        <item >Pro Summary</item>
        <item >Con Summary</item>
        <item >Grand Cross</item>
        <item >Pro Final Focus</item>
        <item >Con Final Focus</item>
    </string-array>
</resources>

注意:如果您复制 &粘贴此代码它应该可以工作.但是,由于您尚未创建 ListItemDetail.class,因此单击某个项目会出现错误.

以下是其外观示例:

N.B.: If you copy & paste this code it should work. But you will get an error by clicking on an Item because you haven't created the ListItemDetail.class yet.

Here is an example of how this could look:

您的 ListItemDetail.java:

Your ListItemDetail.java:

public class ListItemDetail extends Activity {

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

    Intent intent = getIntent();
    int position = intent.getIntExtra("position", 0);

    // Here we turn your string.xml in an array
    String[] myKeys = getResources().getStringArray(R.array.sections);

    TextView myTextView = (TextView) findViewById(R.id.my_textview);
    myTextView.setText(myKeys[position]);


    }

}

及其activity_listitem.xml

And its activity_listitem.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/my_textview"/>

</LinearLayout>

如果你复制过去这个代码它会起作用.

If you copy past this code it will work.

这篇关于Android - 如何创建可点击的列表视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆