从 ListView 启动 Activity

发布于 2024-12-21 00:57:01 字数 1903 浏览 2 评论 0原文

我的列表包括位于“标题”行下方的八个项目,全部包含在我的应用程序的选项卡中。我无法弄清楚如何根据列表中单击的项目更改为活动(或可能是不同的选项卡)。

我目前正在扩展 Activity 类,不确定这是否是一个问题。我尝试在扩展 ListActivity 类的同时使用 onListItemClick ;但这导致应用程序崩溃。

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class Tab2 extends Activity {

    private ListView listView1;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab2);

        Cinema cinema_data[] = new Cinema[]{
            new Cinema(R.drawable.blue, "Blue Cinema"),
            new Cinema(R.drawable.green, "Green Cinema"),
            new Cinema(R.drawable.purple, "Purple Cinema"),
            new Cinema(R.drawable.red, "Red Cinema"),
            new Cinema(R.drawable.yellow, "Gold Cinema"),
            new Cinema(R.drawable.blue, "Cyan Cinema"),
            new Cinema(R.drawable.green, "Lime Cinema"),
            new Cinema(R.drawable.purple, "Magenta Cinema")
        };

        CinemaAdapter adapter = new CinemaAdapter(this,
                R.layout.listview_item_row, cinema_data);

        listView1 = (ListView)findViewById(R.id.listView1);

        View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
        listView1.addHeaderView(header);

        listView1.setAdapter(adapter);

    }   
}

任何帮助将不胜感激!

编辑:

protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        try {
            Intent i = new Intent("android.lab.two.Tab1");
            startActivity(i);
        } catch(Exception e){
            e.printStackTrace();
        }
    }

My list includes eight items positioned beneath a 'header' row, all contained within a tab on my application. I cannot figure out how to change to an activity (or possibly a different tab) based upon the item clicked on in the list.

I'm currently extending the Activity class, not sure if this is an issue. I've attempted to use the onListItemClick whilst extending the ListActivity class; this however caused the application to crash.

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class Tab2 extends Activity {

    private ListView listView1;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab2);

        Cinema cinema_data[] = new Cinema[]{
            new Cinema(R.drawable.blue, "Blue Cinema"),
            new Cinema(R.drawable.green, "Green Cinema"),
            new Cinema(R.drawable.purple, "Purple Cinema"),
            new Cinema(R.drawable.red, "Red Cinema"),
            new Cinema(R.drawable.yellow, "Gold Cinema"),
            new Cinema(R.drawable.blue, "Cyan Cinema"),
            new Cinema(R.drawable.green, "Lime Cinema"),
            new Cinema(R.drawable.purple, "Magenta Cinema")
        };

        CinemaAdapter adapter = new CinemaAdapter(this,
                R.layout.listview_item_row, cinema_data);

        listView1 = (ListView)findViewById(R.id.listView1);

        View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
        listView1.addHeaderView(header);

        listView1.setAdapter(adapter);

    }   
}

any help will be greatly appreciated!

edit:

protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        try {
            Intent i = new Intent("android.lab.two.Tab1");
            startActivity(i);
        } catch(Exception e){
            e.printStackTrace();
        }
    }

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

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

发布评论

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

评论(5

楠木可依 2024-12-28 00:57:01

如果 OnItemClickListener 不起作用,您可以尝试在适配器中设置它:

在适配器的 getView 方法中,您可以将 OnClickListener 添加到行视图,如下所示:

public View getView(int arg0, View arg1, ViewGroup arg2) {
//Create the view for your row
    AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
    ViewGroup.LayoutParams.WRAP_CONTENT, 64);
    TextView rowView = new TextView(getApplicationContext());
    rowView.setLayoutParams(lp);
    rowView.setText("your value");
    rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           try {
                //Starting a new intent ( here a Dial Activity )
                Intent newIntent = new Intent(Intent.ACTION_DIAL);
                newIntent.setData(Uri.parse("tel:"+value));
                startActivity(newIntent);
            } catch (ActivityNotFoundException e) {
                Log.e("your application", "Dial failed", e);
            }
        }  
    });
}

if the OnItemClickListener doesn't work, you can try setting it in your adapter :

in your adapter, in the getView method, you can add an OnClickListener to your row view like this :

public View getView(int arg0, View arg1, ViewGroup arg2) {
//Create the view for your row
    AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
    ViewGroup.LayoutParams.WRAP_CONTENT, 64);
    TextView rowView = new TextView(getApplicationContext());
    rowView.setLayoutParams(lp);
    rowView.setText("your value");
    rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           try {
                //Starting a new intent ( here a Dial Activity )
                Intent newIntent = new Intent(Intent.ACTION_DIAL);
                newIntent.setData(Uri.parse("tel:"+value));
                startActivity(newIntent);
            } catch (ActivityNotFoundException e) {
                Log.e("your application", "Dial failed", e);
            }
        }  
    });
}
[浮城] 2024-12-28 00:57:01

您只需在列表视图上设置点击监听器即可。你得到这个位置并从那里执行你的逻辑。下面的代码应该可以帮助您入门。我不知道你遇到了什么错误,但不会出错。

    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Intent myIntent = new Intent(view.getContext(),
                    NextClass.class);

            startActivity(myIntent);
        }
    });

You just set the click listener on your listview. You get the position and perform you logic from there. The code below should help you get started. I don't know what error you were getting, but there is not much that can go wrong.

    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Intent myIntent = new Intent(view.getContext(),
                    NextClass.class);

            startActivity(myIntent);
        }
    });
望她远 2024-12-28 00:57:01
  1. 使用 TabHost 中的 setCurrentTab 方法根据列表项索引更改选项卡。
  2. 对此活动本身的射击意图使其保持在单一顶部发射模式和然后使用捆绑的数据通过选项卡主机更改选项卡。

我希望这有帮助..

  1. Use the setCurrentTab method in TabHost to change tab based on the list item index.
  2. Fire intent to this activity itself keeping it in the single top launch modes & then using the bundled data change the tab via tab host.

I hope this helps..

瀞厅☆埖开 2024-12-28 00:57:01

您无法从 TabHost 启动活动。你在一个内部类中。您必须使用嵌套类。
不要在 onClickListener 中启动 Intent,而是尝试记录一些内容,您会发现它可以工作。
Log.d("LOG","消息");

You cannot start an activity from a TabHost. You are in a inner class. You must use NestedClasses.
Instead of starting an Intent in your onClickListener, try to Log something, and you will see that will work.
Log.d("LOG","Message");

苦行僧 2024-12-28 00:57:01

如果您在选项卡(活动)中设置了 onListItemClick() 方法,那么您必须使用

protected void onListItemClick(ListView l, View v, int position, long id) 
{
            super.onListItemClick(l, v, position, id);

            Intent i = new Intent(this,tab1.class);
            startActivity(i);           
}

如果您设置 onListItemClick() 方法是在其他类中定义的,那么您必须使用

protected void onListItemClick(ListView l, View v, int position, long id) 
{
            super.onListItemClick(l, v, position, id);

           Intent i = new Intent(v.getContext(),tab1.class);
           startActivity(i);            
}

如果 tab1(活动) 是主活动并且您必须调用 tab2 (活动)在 tab1(活动)内,那么你必须使用这种方式......

protected void onListItemClick(ListView l, View v, int position, long id) 
{
                super.onListItemClick(l, v, position, id);

                System.exit(0);               
}

if you set onListItemClick() method inside your tab(activity) then you have to use

protected void onListItemClick(ListView l, View v, int position, long id) 
{
            super.onListItemClick(l, v, position, id);

            Intent i = new Intent(this,tab1.class);
            startActivity(i);           
}

if you set onListItemClick() method is define in other class then you have to use

protected void onListItemClick(ListView l, View v, int position, long id) 
{
            super.onListItemClick(l, v, position, id);

           Intent i = new Intent(v.getContext(),tab1.class);
           startActivity(i);            
}

if tab1(activity) is a main activity and you have to call tab2(activity) inside the tab1(activity) then you have to use this way...

protected void onListItemClick(ListView l, View v, int position, long id) 
{
                super.onListItemClick(l, v, position, id);

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