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

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

问题描述

我想在列表视图我所有的列表项开到一个新的页面,所以每个列表视图项可通往一个新的黑色页面,我可以使用。我不知道如何实现这在所有。我搜索了几个小时就结束,并不能找到一个答案,我的解决方案。这将是pciated如果有人能证明和/或解释如何做,而不是提供一个链接这么多AP $ P $,但无论是有帮助的。

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.

下面是我的code到目前为止:

Here is my code so far:

  <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的活动,它实现了一个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);
    }

修改

以上code将被放置在您MainActivity.java。我改变了类的名称 MainActivity 和内容查看到的setContentView(R.layout.activity_main) - 名称是那些一个新创建的Andr​​oid项目在Eclipse中。请参见下// 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>

array.xml (不string.xml)在`RES /价值/`文件夹看起来像这样

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>

N.B:如果您复制和;复制粘贴此code,它应该工作。但你会点击一个项目,因为你还没有创建得到一个错误的 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>

如果您要复制过去,这code它将工作。

If you copy past this code it will work.

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

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