2012-12-13 58 views
1

每一個項目我有ListView中layout.How 16個項目,讓每一個項目跳轉到另一個activity.I嘗試這個代碼,但沒有發生:(讓ListView中跳轉到其他活動

這是GroupActivity。 java的

package com.test.listview; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ListView; 

public class GroupActivity extends Activity{ 
    private ListView listView; 
    private ListViewAdapter listViewAdapter; 
    private List<Map<String, Object>> listItems; 
    protected SharedPreferences group_sp; 

    @Override 
    public void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.group); 

     listView=(ListView)findViewById(R.id.group1_list); 
     listItems=getlistItems(); 
     listViewAdapter=new ListViewAdapter(this, listItems); 
     listView.setAdapter(listViewAdapter); 
     listView.setTextFilterEnabled(true); 

     listView.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position,long id) { 
       Log.i("mm", " onItemClick "); 
       Intent intent=new Intent(); 
       switch(position){ 
       case 0: 
        intent.setClass(getApplicationContext(), ListViewGroup.class); 
        GroupActivity.this.startActivity(intent); 
        break; 
       case 1: 
        intent.setClass(getApplicationContext(), ListViewGroup.class); 
        GroupActivity.this.startActivity(intent); 
        break; 
       case 2: 
        intent.setClass(getApplicationContext(), ListViewGroup.class); 
        GroupActivity.this.startActivity(intent); 
        break; 
       case 3: 
        intent.setClass(getApplicationContext(), ListViewGroup.class); 
        GroupActivity.this.startActivity(intent); 
        break; 
       case 4: 
        intent.setClass(getApplicationContext(), ListViewGroup.class); 
        GroupActivity.this.startActivity(intent); 
        break; 
       case 5: 
        intent.setClass(getApplicationContext(), ListViewGroup.class); 
        GroupActivity.this.startActivity(intent); 
        break; 
       case 6: 
        intent.setClass(getApplicationContext(), ListViewGroup.class); 
        GroupActivity.this.startActivity(intent); 
        break; 
       case 7: 
        intent.setClass(getApplicationContext(), ListViewGroup.class); 
        GroupActivity.this.startActivity(intent); 
        break; 
       case 8: 
        intent.setClass(getApplicationContext(), ListViewGroup.class); 
        GroupActivity.this.startActivity(intent); 
        break; 
       case 9: 
        intent.setClass(getApplicationContext(), ListViewGroup.class); 
        GroupActivity.this.startActivity(intent); 
        break; 
       case 10: 
        intent.setClass(getApplicationContext(), ListViewGroup.class); 
        GroupActivity.this.startActivity(intent); 
        break; 
       case 11: 
        intent.setClass(getApplicationContext(), ListViewGroup.class); 
        GroupActivity.this.startActivity(intent); 
        break; 
       case 12: 
        intent.setClass(getApplicationContext(), ListViewGroup.class); 
        GroupActivity.this.startActivity(intent); 
        break; 
       case 13: 
        intent.setClass(getApplicationContext(), ListViewGroup.class); 
        GroupActivity.this.startActivity(intent); 
        break; 
       case 14: 
        intent.setClass(getApplicationContext(), ListViewGroup.class); 
        GroupActivity.this.startActivity(intent); 
        break; 
       case 15: 
        intent.setClass(getApplicationContext(), ListViewGroup.class); 
        GroupActivity.this.startActivity(intent); 
        break; 
       } 

      } 

     }); 
    } 

    private List<Map<String, Object>> getlistItems() { 
     List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>(); 
     for(int i = 1; i<=16; i++) { 
      Map<String, Object> map = new HashMap<String, Object>(); 
      map.put("imageButton", R.drawable.goff1);     
      map.put("textView", "Group"+i);   
      map.put("imageView",R.drawable.arrow1);  
      listItems.add(map); 
     }  
     return listItems; 
    } 
} 

這是來自適配器代碼:

package com.test.listview; 

import java.util.List; 
import java.util.Map; 

import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageButton; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class ListViewAdapter extends BaseAdapter { 
    private Context context;       
    private List<Map<String,Object>> listItems;  //group list 
    private LayoutInflater layoutInflater;   //View container 
    protected SharedPreferences group_sp; 

    /**Custom list control set*/ 
    public final class ListItemView{ 
     public ImageButton imageButton; 
     public TextView textView; 
     public ImageView imageView; 
    } 

    public ListViewAdapter(Context context,List<Map<String,Object>> listItems){ 
     this.context=context; 
     layoutInflater=LayoutInflater.from(context);//創建視圖容器設置上下文 
     this.listItems=listItems; 
    } 

    public int getCount() { 
     return listItems.size(); 
    } 

    public Object getItem(int position) { 
     return listItems.get(position); 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    /**list settings*/ 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
      convertView=layoutInflater.inflate(R.layout.group_list, null); 
      //get space object 
      final ImageButton imageButton=(ImageButton)convertView.findViewById(R.id.goff1);  
      imageButton.setBackgroundResource(R.drawable.goff1); 
      imageButton.setOnClickListener(new View.OnClickListener() { 
       boolean flag=true; 
       public void onClick(View v) { 
        if(flag){ 
         imageButton.setBackgroundResource(R.drawable.gon1); 
         flag=false; 
        }else{ 
         imageButton.setBackgroundResource(R.drawable.goff1); 
         flag=true; 
        }  

       } 


      }); 
      TextView textView=(TextView)convertView.findViewById(R.id.title); 
      ImageView imageView=(ImageView)convertView.findViewById(R.id.right_image); 
//   imageView.setOnClickListener(new View.OnClickListener() { 
//     
//    @Override 
//    public void onClick(View v) { 
//     Intent intent=new Intent(); 
//     intent.setClass(context, ListViewGroup.class); 
//     context.startActivity(intent); 
//    } 
//   }); 

      //padding image and word 
      imageButton.setBackgroundResource((Integer)listItems.get(position).get("imageButton")); 
      textView.setText((String)listItems.get(position).get("textView")); 
      imageView.setBackgroundResource((Integer)listItems.get(position).get("imageView")); 

     //addListener(convertView); 
     return convertView; 
    } 

} 
莫非

任何人的幫助我出去了嗎?謝謝。

回答

0

,如果你想在onItemClick啓動同一活動,則沒有必要使用switch case對每一行點擊開始新的活動只是改變你的代碼爲:

listView.setOnItemClickListener(new OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view 
               , int position,long id) { 
     Log.i("mm", " onItemClick "); 
     Intent intent=new Intent(GroupActivity.this,ListViewGroup.class); 

     GroupActivity.this.startActivity(intent); 
    } 

}); 
+0

對不起,遲到回覆,謝謝 – Xoangle

+0

@Xoangle:最受歡迎的朋友!! :) 3-4天后你回到這裏吧? –

+0

不,不會回來這麼久,結果測試需要時間。:P – Xoangle

0

嘗試使用活動場景,而不是應用程序上下文..

intent.setClass(GroupActivity.this, ListViewGroup.class);