0

我在某些似乎无法修复的代码部分遇到问题。当我单击 Shop1 时,ListView 中会出现三个项目,即 Location、Contact 和 Appointment。我想知道当我选择其中一个时如何进入不同的活动。例如,如果我想单击位置,我希望它带我到不同的布局文件。帮助会很棒!

以下是一些照片:

未点击

未点击

在此处输入图像描述

已点击

在此处输入图像描述

单击位置以转到不同的活动

这是代码:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;


public class MainActivity extends Activity 
{
private ListView list;
private ArrayAdapter<String> adapter;
private RibbonMenuView rbmView;
private Button test;
private ListView rbmListView, rbmListView2;
private ArrayAdapter<String> adapt, adapter2;

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

    /** Init our views **/
    rbmView = (RibbonMenuView) findViewById(R.id.ribbonMenuView1);
    list = (ListView) findViewById(R.id.listView1);

    /** Calls "hideMenu()" after each click similiar to the Facebook or Google+  application **/
    test = (Button) findViewById(R.id.button);
    test.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v) 
        {
            // shows how to hide the menu
            if (rbmView != null)
            {
                rbmView.hideMenu();
            }
        }
    });

    /** This is the most important ListView, updating the main list in the Activity **/
    final String[] items_list = { "Shop1", "Shop2", "Shop3" };
    adapt = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, items_list);
    rbmListView = (ListView) findViewById(R.id.rbm_listview);
    if (rbmListView != null)
    {
        rbmListView.setOnItemClickListener(new OnItemClickListener() 
        {
            /*
             * (non-Javadoc)
             * @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, android.view.View, int, long)
             */
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
            {
                switch (position)
                {
                case 0:
                    String[] items = { "Location", "Contact", "Appointment" };
                    adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, items);
                    break;

                case 1:
                    String[] items1 = { "Location", "Contact", "Appointment" };
                    adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, items1);
                    break;

                case 2:
                    String[] items2 = { "Location", "Contact", "Appointment" };
                    adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, items2);
                    break;


                default:
                    String[] itemsd = { "A", "B", "C", "D" };
                    adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, itemsd);
                    break;
                }

                // always update these
                if (list != null)
                {
                    list.setAdapter(adapter);
                    list.setClickable(true);
                    list.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            switch ((int)l)
                            {
                                case 0: // Location
                                    //Intent intent = new Intent(getApplicationContext(), NEW_ACTIVITY.class);
                                    //startActivity(intent);
                                    break;
                                case 1: // Contact
                                    //Intent intent = new Intent(getApplicationContext(), NEW_ACTIVITY.class);
                                    //startActivity(intent);
                                    break;
                                case 2: // Appointment
                                    //Intent intent = new Intent(getApplicationContext(), NEW_ACTIVITY.class);
                                    //startActivity(intent);
                                    break;
                            }
                        }
                    });
                }

                if (rbmView != null)
                {
                    rbmView.hideMenu();
                }
            }
        });
        rbmListView.setAdapter(adapt);
    }

    /** This is the second ListView on the menu **/
    final String[] items_list2 = { "About", "Deals", "Contact Developer", };
    adapter2 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, items_list2);
    rbmListView2 = (ListView) findViewById(R.id.rbm_listview2);
    if (rbmListView2 != null)
    {
        rbmListView2.setOnItemClickListener(new OnItemClickListener()
        {
            /*
             * (non-Javadoc)
             * @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, android.view.View, int, long)
             */
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                switch (position)
                {
                    case 0:
                        startActivity(new Intent(MainActivity.this, About.class));
                        break;

                    case 1:
                        startActivity(new Intent(MainActivity.this, DealsCode.class));
                        break;

                    case 2:
                        startActivity(new Intent(MainActivity.this, ContactDev.class));
                        break;
                }
                /*Toast.makeText(MainActivity.this, items_list2[position], Toast.LENGTH_SHORT).show();
                if (rbmView != null)*/
                {
                    rbmView.hideMenu();
                }
            }
        });
        rbmListView2.setAdapter(adapter2);
    }
}

/**  Allows users, even API < 5, to use the back button **/
/*
 * (non-Javadoc)
 * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
 */
public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    switch (keyCode)
    {
    case KeyEvent.KEYCODE_BACK:
        // if view is showing, close it
        // all other back button clicks are handled by the phone
        if (rbmView != null)
        {
            if (rbmView.isMenuShowing())
            {
                new Handler().postDelayed(new Runnable()
                {
                    public void run()
                    {
                        rbmView.hideMenu();
                    }
                }, 50);
            } 
            else
            {
                return super.onKeyDown(keyCode, event);
            }
        }
        break;
    default:
        return super.onKeyDown(keyCode, event);

    }

    return true;
}

/*
 * (non-Javadoc)
 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
 */
public boolean onCreateOptionsMenu(Menu menu) 
{
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
}

/*
 * (non-Javadoc)
 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
 */
public boolean onOptionsItemSelected(MenuItem item) 
{
    switch (item.getItemId()) 
    {
    case R.id.test:
        // toggle our menu
        if (rbmView != null)
        {
            rbmView.toggleMenu();
        }
        break;

    default:
        return super.onOptionsItemSelected(item);

    }

    return true;
}
}

如果还有什么需要请告诉我!

4

1 回答 1

1

尝试使用startActivity(new Intent(getBaseContext(), activityToStart.class));

为什么要将作为 ID 的 float 转换为 int?您应该只使用作为位置的 int 值。

     list.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            switch (i)
                            {
                                case 0: // Location
startActivity(new Intent(getBaseContext(), LocationActivity.class));

                                    break;
                                case 1: // Contact
;startActivity(new Intent(getBaseContext(), ContactActivity.class));

                                    break;
                                case 2: // Appointment
startActivity(new Intent(getBaseContext(), AppointmentActivity.class));

                                    break;
                            }
                        }
                    });

如果您知道所单击项目的“行 ID”,则它们是一种使用浮点值的方法。

于 2013-10-18T04:27:07.733 回答