如何制作可点击的列表视图

发布于 2024-12-10 08:13:15 字数 1278 浏览 0 评论 0原文

好的。我正在开发一个项目,我已经创建了列表视图,但我想单击它并转到另一个页面,例如,当您单击按钮时,它会转到另一个页面,完全一样,这是我到目前为止的代码:

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class listV extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setListAdapter( new ArrayAdapter<String>(this, R.layout.listview,Food));

    ListView list = getListView();
    list.setTextFilterEnabled(true);
    list.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), ((TextView)      arg1).getText(), Toast.LENGTH_SHORT).show();
        }

    });

}




static final String[] Food = new String[]{
"Physical Activity" , "Healthy Diet", "Childhood Obesity"

};

}

任何帮助将不胜感激。 我只是一个初学者,所以请尝试详细解释。

Ok. I'm working on a project, I have already created the listview, but I want to click on it and go to another page e.g like when you click a button it goes to another page, exactly like that, this is my code so far:

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class listV extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setListAdapter( new ArrayAdapter<String>(this, R.layout.listview,Food));

    ListView list = getListView();
    list.setTextFilterEnabled(true);
    list.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), ((TextView)      arg1).getText(), Toast.LENGTH_SHORT).show();
        }

    });

}




static final String[] Food = new String[]{
"Physical Activity" , "Healthy Diet", "Childhood Obesity"

};

}

Any help would be appreciated.
I'm just a beginner so please try to explain in detail.

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

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

发布评论

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

评论(2

甲如呢乙后呢 2024-12-17 08:13:15

看起来你已经完成了 95%。在侦听器的 onItemClick 方法中,您只需像平常一样启动新活动即可。您可以使用 onItemClick 的第三个参数来为您提供单击的列表视图项的位置,并使用它来区分您调用的活动或将其传递到单个活动中:

@Override
public void onItemClick(AdapterView<?> arg0, View position, int arg2, long arg3) {

        Intent i;
        if( position == 1 ){           
            i = new Intent(listV.this, MyFirstActivity.class);
        } else if (position == 2){           
            i = new Intent(listV.this, MySecondActivity.class);
        } else if (position == 3) {
            i = new Intent(listV.this, MyThirdActivity.class);
        } else {
            return;
        }           
        startActivity(i);

}

Looks like you're 95% of the way there. In your onItemClick method of the listener, you just have to start the new activity like you normally would. You can use the 3rd argument of the onItemClick to give you the position of the listview item that was click and use that to differentiate the activity you call OR pass it into a single activity:

@Override
public void onItemClick(AdapterView<?> arg0, View position, int arg2, long arg3) {

        Intent i;
        if( position == 1 ){           
            i = new Intent(listV.this, MyFirstActivity.class);
        } else if (position == 2){           
            i = new Intent(listV.this, MySecondActivity.class);
        } else if (position == 3) {
            i = new Intent(listV.this, MyThirdActivity.class);
        } else {
            return;
        }           
        startActivity(i);

}
通知家属抬走 2024-12-17 08:13:15

我自己是一个相当新的程序员,我正在尝试创建同样的东西。一个 ListView,用户可以单击列表中的每个单独项目,每个项目将启动自己的活动。请根据您之前提供的帮助检查我的代码。

package com.tylerbmc.test;

import android.app.ListActivity;

import android.content.Intent;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Main extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
            getResources().getStringArray(R.array.abdominals)));

    ListView list = getListView();
    list.setOnItemClickListener(new OnItemClickListener() {


        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {

            Intent i;

            if (position == 1) {
                i = new Intent(Main.this, Second.class);
                startActivity(i);
            }
            else if (position == 2) {
                i = new Intent(Main.this, Third.class);
                startActivity(i);
            }




        }
    }); 

}

}

I am a fairly new programmer myself and I am attempting to create the same thing. A ListView where the user is able to click each individual item in the list and each item will start its own activity. Please check out my code based on the help you gave earlier.

package com.tylerbmc.test;

import android.app.ListActivity;

import android.content.Intent;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Main extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
            getResources().getStringArray(R.array.abdominals)));

    ListView list = getListView();
    list.setOnItemClickListener(new OnItemClickListener() {


        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {

            Intent i;

            if (position == 1) {
                i = new Intent(Main.this, Second.class);
                startActivity(i);
            }
            else if (position == 2) {
                i = new Intent(Main.this, Third.class);
                startActivity(i);
            }




        }
    }); 

}

}

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