点击列表视图项目go->新活动?

时间:2014-04-22 04:38:06

标签: android listview

你好,我有一个Listview活动

public class fragment_main extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dersler);
        String[] myArray= getResources().getStringArray(R.array.Dersler);
        ArrayAdapter<String> aad= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, myArray);
        setListAdapter(aad);
    }

    private void setListAdapter(ArrayAdapter<String> aad) {
        // TODO Auto-generated method stub  
    }
}

xml代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
   android:id="@+id/listder"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" 
    android:entries="@array/Dersler">

</ListView>

我希望当我按下listview (string Matematik1)时按下string Matematik1它会打开class.gnrl,就像去其他可以帮助我的活动一样感谢所有人!

3 个答案:

答案 0 :(得分:0)

试试如下:

public class fragment_main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dersler);
     ListView list=(ListView)findViewById(R.id.listder);

      String[] myArray= getResources().getStringArray(R.array.Dersler);
ArrayAdapter<String> aad= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, myArray);

      list.setAdapter(aad);

       //Add the item click listener as below.
       list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
              Toast.makeText(YourListActivity.this,
                 "Item in position " + position + " clicked"+ myArray[position].toString(),Toast.LENGTH_LONG).show();

             Intent intent=new Intent(this,<yourclassname>.class);
              startActivity(intent);
          }
        });
}

private void setListAdapter(ArrayAdapter<String> aad) {
    // TODO Auto-generated method stub

}
}

答案 1 :(得分:0)

ListView listview=(ListView)findViewById(R.id.listder);


listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
 if (position == 0) {
        Intent int0 = new Intent(getApplicationContext(), gnrl.class);
        startActivity(int0);
    }

    else if (position == 1) {
        Intent int1 = new Intent(getApplicationContext(), asf.class);
        startActivity(int1);
    }
    else if (position == 2) {
        Intent int2 = new Intent(getApplicationContext(), ThirdActivity.class);
        startActivity(int2);
    }

    else if (position == 3) {
        Intent int3 = new Intent(getApplicationContext(),
                FourthActivity.class);
        startActivity(int3);
    }


          }
        });

答案 2 :(得分:0)

 l1.setOnItemClickListener(new OnItemClickListener() 
    {
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
        {                       
           String name=(String)parent.getItemAtPosition(position);  
       if(position==1)
       {                
           Intent i = new Intent(getBaseContext(),1st_class.class);
           i.putExtra("PROJECT", name);
           startActivity(i);
       }
       else if(position==2)
       {                
           Intent i = new Intent(getBaseContext(),2nd_class.class);
           i.putExtra("PROJECT", name);
           startActivity(i);
       }
       else if(position==2)
       {                
           Intent i = new Intent(getBaseContext(),3rd_class.class);
           i.putExtra("PROJECT", name);
           startActivity(i);
       }
        }
    });

另一项活动

Intent intent = getIntent();
String receivedName = (String) intent.getSerializableExtra("PROJECT");