如何用listView项打开不同的活动?

时间:2015-05-14 10:13:24

标签: java android listview

我有一个包含9个不同字符串的String-Array,并使用此字符串创建了一个listView;现在,当我点击listView的每个元素时,我想打开一个不同的Activity。我已经尝试了

row.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        startActivity(new Intent((Activity)context, Workouts.class));
    }
});

但它只为所有项目打开相同的活动。如果你能给我一个可行的代码,那将是非常好的,因为我已经尝试过管理这个问题。

MainActivity:

public class MainActivity extends Activity implements OnItemClickListener {

    private DrawerLayout drawerLayout;
    private ListView listView;

    private ActionBarDrawerToggle drawerListener;
    private MyAdapter MyAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        drawerLayout=(DrawerLayout) findViewById(R.id.drawerLayout);

        listView=(ListView)findViewById(R.id.drawerList);
        MyAdapter=new MyAdapter(this);
        listView.setAdapter(MyAdapter);
        listView.setOnItemClickListener(this);

        drawerListener= new ActionBarDrawerToggle(this, drawerLayout,     
        R.drawable.listmenu,
        R.string.drawer_open, R.string.drawer_close){

            @Override
            public void onDrawerClosed(View drawerView) {
                // TODO Auto-generated method stub
                super.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, " Drawer Opened ",   
                Toast.LENGTH_SHORT).show();
            }

        };

        drawerLayout.setDrawerListener(drawerListener); 

        getActionBar().setHomeButtonEnabled(true);
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub

        if (drawerListener.onOptionsItemSelected(item))
        {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);

        drawerListener.onConfigurationChanged(newConfig);
    }


    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onPostCreate(savedInstanceState);
        drawerListener.syncState();
    }

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

    public void selectItem(int position) {        
        listView.setItemChecked(position, true);
    }

    public  void setTitle(String title)
    {
        getActionBar().setTitle(title);

    class MyAdapter extends BaseAdapter {
        private Context context;
        String[] planets;

        int[] images = { R.drawable.home, R.drawable.workout, R.drawable.week,
            R.drawable.gallery, R.drawable.statistics, R.drawable.settings};

            public MyAdapter(Context context) {
                this.context=context;
                planets=context.getResources().getStringArray(R.array.planets);
            }

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return planets.length;
            }

            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return planets[position];
            }

            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub

                View row =null;

                if (convertView==null)
                {
                    LayoutInflater inflater = (LayoutInflater)            
                        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                    row=inflater.inflate(R.layout.custom_row, parent, false);
                }
                else
                {
                    row = convertView                    
                }

                row.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent((Activity) context, Workouts.class));
                    }
                });

                TextView titleTextView = (TextView) row.findViewById(R.id.textView1);
                ImageView titleImageView = (ImageView) row.findViewById(R.id.imageView1);

                titleTextView.setText(planets[position]);
                titleImageView.setImageResource(images[position]);
                return row;    
            }
        }
    }

4 个答案:

答案 0 :(得分:2)

您可以根据位置传递给不同的活动。

row.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

         Switch(position){


         case 0:
              //startActivity 1
              break;
         case 1:
              //startActivity 2
              break;

         /**** continue **********/
         }

    }
});

答案 1 :(得分:1)

==&gt;在您的MainActivity中替换以下代码

public class MainActivity extends Activity implements OnItemClickListener      {

 private DrawerLayout drawerLayout;
 private ListView listView;


private ActionBarDrawerToggle drawerListener;
 private MyAdapter MyAdapter;




 @Override
protected void onCreate(Bundle savedInstanceState){


     super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
 drawerLayout=(DrawerLayout) findViewById(R.id.drawerLayout);

 listView=(ListView)findViewById(R.id.drawerList);
 MyAdapter=new MyAdapter(this);
 listView.setAdapter(MyAdapter);
 listView.setOnItemClickListener(this);

  drawerListener= new ActionBarDrawerToggle(this, drawerLayout,     
  R.drawable.listmenu,
    R.string.drawer_open, R.string.drawer_close){


  @Override
  public void onDrawerClosed(View drawerView) {
    // TODO Auto-generated method stub
    super.onDrawerClosed(drawerView);
}

@Override
        public void onDrawerOpened(View drawerView) {
            // TODO Auto-generated method stub
    Toast.makeText(MainActivity.this, " Drawer Opened ",   
 Toast.LENGTH_SHORT).show();


    }

};

    drawerLayout.setDrawerListener(drawerListener); 

    getActionBar().setHomeButtonEnabled(true);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    }

 @Override
  public boolean onOptionsItemSelected(MenuItem item) {
   // TODO Auto-generated method stub

if(drawerListener.onOptionsItemSelected(item))

   {

return true;

  }

return super.onOptionsItemSelected(item);


  }

    @Override
public void onConfigurationChanged(Configuration newConfig) {
  // TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);

drawerListener.onConfigurationChanged(newConfig);
}


   @Override
protected void onPostCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onPostCreate(savedInstanceState);
 drawerListener.syncState();
    }

      @Override
  public void onItemClick(AdapterView<?> parent, View view, int position,        
 long id) {
if (position == 0) {
        startActivity(new Intent((Activity) context, A.class));
    }
    if (position == 1) {
        startActivity(new Intent((Activity) context, B.class));
    }
    if (position == 2) {
        startActivity(new Intent((Activity) context, C.class));
    }
    if (position == 3) {
        startActivity(new Intent((Activity) context, D.class));
    }
    if (position == 4) {
        startActivity(new Intent((Activity) context, E.class));
    }
    if (position == 5) {
        startActivity(new Intent((Activity) context, F.class));
    }
    if (position == 6) {
        startActivity(new Intent((Activity) context, G.class));
    }
    if (position == 7) {
        startActivity(new Intent((Activity) context, H.class));
    }
    if (position == 8) {
        startActivity(new Intent((Activity) context, I.class));
    }


        }
public void selectItem(int position) {

listView.setItemChecked(position, true);

}

public  void setTitle(String title)
{
getActionBar().setTitle(title);
}

class MyAdapter extends BaseAdapter {
private Context context;
String[] planets;

int[] images = { R.drawable.home, R.drawable.workout, R.drawable.week,
    R.drawable.gallery, R.drawable.statistics, R.drawable.settings};






 public MyAdapter(Context context) {
 this.context=context;
   planets=context.getResources().getStringArray(R.array.planets);
  }

@Override
  public int getCount() {
   // TODO Auto-generated method stub
return planets.length;
 }






@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
   return planets[position];
  }

@Override

 public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

  @Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

View row =null;

if(convertView==null)
{
    LayoutInflater inflater=(LayoutInflater)            
     context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    row=inflater.inflate(R.layout.custom_row, parent, false);
}

else
{
    row=convertView;

}








TextView titleTextView=(TextView) row.findViewById(R.id.textView1);
  ImageView titleImageView= (ImageView) row.findViewById(R.id.imageView1);


titleTextView.setText(planets[position]);
titleImageView.setImageResource(images[position]);
return row;





 }

   }
 }

答案 2 :(得分:1)

删除OnItemClickListenerMainActivity的实施,然后使用匿名类。将listView.setOnItemClickListener(this);替换为:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
    switch (position) {
    case 0:
        startActivity(new Intent((Activity) context, Activity0.class));
        break;
    case 1:
        startActivity(new Intent((Activity) context, Activity1.class));
        break;
    case 2:
        startActivity(new Intent((Activity) context, Activity2.class));
        break;
    // etc.
    }
});

答案 3 :(得分:1)

通过覆盖position onItemClick的{​​{1}},通过OnItemClickListener选择目标课程:

        @Override 
        public void onItemClick(View view, int position) {
            switch(position){
            case 0:
                startActivity(new Intent(MainActivity.this, A.class));
                break;
            case 1:
                startActivity(new Intent(MainActivity.this, B.class));
                break;
            default:
                break;  
            }
        }

此外,您还必须定义从positiontarget class的映射。