简体   繁体   English

Android - 如何创建可点击的列表视图?

[英]Android - How to create clickable 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. 我想将listview中的所有列表项打开到一个新页面,因此每个listview项都会打开一个我可以使用的新黑页。 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. 如果有人能够展示和/或解释如何执行此操作而不是提供链接,将非常感激,但要么是有帮助的。

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>

This is in my string.xml 这是在我的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>

This is in my activity_main.xml. 这是在我的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? 我从哪里开始使我的列表中的每个项目都可以点击并且能够打开到新页面?

Thanks in advance! 提前致谢!

EDIT: 编辑:

Logcat no longer relevant. Logcat不再相关。

In fact it is quite easy: 事实上它很容易:

This is your Activity with the ListView, it implements an OnItemClickListener: 这是您使用ListView的Activity,它实现了一个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);
    }

Edit 编辑

The above code would be placed in your MainActivity.java. 上面的代码将放在您的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. 我将类的名称更改为MainActivity ,将contentView更改为setContentView(R.layout.activity_main) - 这些名称是Eclipse中新创建的Android项目的名称。
Please see also the 2 new lines under //* Edit * - those will set the Listener for clicks on items in the list. 另请参阅// * Edit *下的2个新行 - 这些行将设置Listener以获取列表中项目的点击次数。

Your activity_main.xml should look like this: 您的activity_main.xml应如下所示:

<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>

The array.xml (not string.xml) in your `res/values/` folder looks like this `res / values /`文件夹中的array.xml (不是string.xml)如下所示

<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>

NB: 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. 但是,由于尚未创建ListItemDetail.class ,因此单击Item会出现错误。

Here is an example of how this could look: 以下是一个示例:

Your ListItemDetail.java: 你的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]);


    }

}

And its activity_listitem.xml 以及它的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. 如果您复制过去这段代码就行了。

you can populate listview from array in string.xml as like this 您可以在string.xml中从数组中填充listview,如下所示

String[] myKeys = getResources().getStringArray(R.array.sections);
ListView mListView = (ListView)findViewById(R.id.listView1);
mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myKeys));  

and clicking on the listview items is quite simple and easy method.just use setOnItemClickListener 并且单击listview项是非常简单和方便的方法。 只需使用setOnItemClickListener

mListView.setOnItemClickListener(new OnItemClickListener() 
{
    public void onItemClick(AdapterView<?> arg0,View arg1, int position, long arg3) 
    {

        Intent n = new Intent(getApplicationContext(), yourclass.class);
        n.putExtra("position", position);
        startActivity(n);
    }
});

You use the onListItemClick function to set up your Intent that loads the next activity and passes any data across. 您可以使用onListItemClick函数来设置加载下一个活动并传递任何数据的Intent。

public void onListItemClick(ListView parent, View view, int position, long id)
{
    Intent intent = new Intent(this, AnotherActivity.class);
    intent.putExtra("position", position);
    startActivity(intent);
}

listView= (ListView) findViewById(R.id.listview); listView =(ListView)findViewById(R.id.listview);

  • List item 项目清单

     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if(position==0){ Intent i=new Intent(MainActivity.this,Main3Activity.class); startActivity(i); } } }); 
  1. Inside fragment.java with Array Adapter 在fragment.java里面有Array Adapter

String[] menuItems = {"Default C02", "Default 02 "}; String [] menuItems = {“默认C02”,“默认02”};

    ListView listView = (ListView) root.findViewById(R.id.main_menu);

    ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(
        getActivity()
        ,android.R.layout.simple_list_item_1
        ,menuItems
    );

    listView.setAdapter(listViewAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        public void onItemClick(AdapterView<?> l, View v, int position, long id){
            Log.i("menuItems", "You clicked Item: " + id + " at position:" + position);



        }
    });

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

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