使用 Android AdapterView.OnItemClick Listener 重定向到新的空白 activity

Using Android AdapterView.OnItemClick Listener to redirect to a new blank activity

嘿,所以我按照教程构建了一个列表视图。一切都已填充,想要单击其中一项并将其重定向到另一个空白 activity。列表视图中的所有项目都是在我的仪表板中创建和填充的 Class:

    import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;


public class Dashboard extends ActionBarActivity {

    private List<DashboardItems> myDash = new ArrayList<DashboardItems>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dashboard);

        populateDashboardItemList();
        populateDashboardListView();
        dashboardOnclick();

    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_dashboard, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getItemId()) {
            case R.id.homescreen:
                homescreenItem();
                return true;
            case R.id.dashboard:
                dashboardItem();
                return true;
            case R.id.about:
                aboutItem();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void homescreenItem(){
        startActivity(new Intent(Dashboard.this, Home.class));
    }

    private void dashboardItem(){
        startActivity(new Intent(Dashboard.this, Dashboard.class));
    }

    private void aboutItem(){
        new AlertDialog.Builder(this)
                .setTitle("About")
                .setMessage("Welcome to Save Me! An interactive and intuitive way to protect yourself during emergency situations and keep your location privacy. Made for a Dissertation and Developed by Ankhit Sharma")
                .setNeutralButton("OK" , new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).show();
    }

    private void populateDashboardItemList() {
        myDash.add(new DashboardItems("Profile" , "Use this to create/edit your profile" , R.drawable.profile));
        myDash.add(new DashboardItems("Location" , "Use this to set/view your true/dummy location" , R.drawable.location));
        myDash.add(new DashboardItems("Emergency Contacts" , "Use this to select/view your emergency contacts" , R.drawable.contacts));
    }

    private void populateDashboardListView() {
        ArrayAdapter<DashboardItems> adapter = new myDashboardAdapter();
        ListView list = (ListView) findViewById(R.id.dashboardListview);
        list.setAdapter(adapter);
    }

    private void dashboardOnclick() {
        ListView list = (ListView) findViewById(R.id.dashboardListview);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {

                DashboardItems clickedDash = myDash.get(position);
                String message = "You clicked position" + position
                        + "which is" + clickedDash.getItemName();
                Toast.makeText(Dashboard.this, message, Toast.LENGTH_LONG).show();
            }
        });
    }

    private class myDashboardAdapter extends ArrayAdapter<DashboardItems>{

        public myDashboardAdapter(){
            super(Dashboard.this, R.layout.dashboarditemslayout, myDash);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
           //This makes sure we have a view to work with
            View itemView = convertView;
            if (itemView == null){
                itemView = getLayoutInflater().inflate(R.layout.dashboarditemslayout, parent, false);
        }

           //Find DashboardItem to work with

            DashboardItems currentDashboardItems = myDash.get(position);

           //Image Icon
            ImageView imageView = (ImageView)itemView.findViewById(R.id.dashboardicon);
            imageView.setImageResource(currentDashboardItems.getItemID());

            //Profile
            TextView profView = (TextView)itemView.findViewById(R.id.dashtextprofile);
            profView.setText(currentDashboardItems.getItemName());

            //Description
            TextView descView = (TextView)itemView.findViewById(R.id.dashtextprofiledescription);
            descView.setText(currentDashboardItems.getItemDescription());


           return itemView;
    }


    }
}

现在,在 OnItemClick 函数中,我尝试了不同的组合以使其引用另一个 class(我的 UserProfile.java),但它不起作用。当前函数单击它并显示项目的位置和各自的名称以及一条消息。我想知道您是否可以告诉我启动适配器视图意图的正确方法,因为它对我来说都是全新的?

UserProfile.java class 是空白 activity。

Ps。 OnItemClick 与 OnClick 除了 item 是针对项目而 onClick 是整个区域之外的主要区别是什么?

private void dashboardOnclick() {
    ListView list = (ListView) findViewById(R.id.dashboardListview);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {

            DashboardItems clickedDash = myDash.get(position);
            String message = "You clicked position" + position
                    + "which is" + clickedDash.getItemName();
            Toast.makeText(Dashboard.this, message, Toast.LENGTH_LONG).show();
            Intent intent;
            // i do not know how your dashitems are declared
            // but the idea here is you have already gotten the clicked item so what you do is compare them
           // with the available item you have in your dasboarditem like this
           if(clickedDash ==DashboardItems.Profile){ // assuming DashboardItems.Profile is your profile for profile item
              intent = new Intent(Dashboard.this,UserProfile.class); // you place your intent here in the onitemclick method, in the if equal profile condition
            }else if(clickedDash==DashboardItems.Location){ // you can continue with the others, also here
            // assuming location item is declared in DashboardItems as location
                intent = new Intent(Dashboard.this, UserLocation.class);
              }else{
                // put the 3rd item
              }
            // so it is triggered when you click an item
            startActivity(intent); // then you start it this way
            // that's all you need to do
        }
    });
}

希望这就是你想要的