2013-11-01 80 views
0

我已經使用baseadapter創建了listview。 我想在不同的項目上點擊不同的項目,但無法獲得項目價值。所以任何人都可以幫助我如何去做。 例如點擊項目x我想活動1 點擊項目說z,我想去avctivity2 謝謝。Listview和baseadapter

這裏是代碼:

public class homeScreen extends Activity{ 
ListView list1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.homescreen); 

     list1=(ListView)findViewById(R.id.spacelist); 
     list1.setAdapter(new MySimpleAdapter(this)); 
     list1.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View v, int position,long id) { 
       // TODO Auto-generated method stub  
       Intent i=new Intent(homeScreen.this, privateSpaceList.class); 
       startActivity(i); 
      } 
     }); 
    } 
} 

class SingleRow{ 

    String title; 
    int image; 
    public SingleRow(String title,int image) { 
     // TODO Auto-generated constructor stub 
     this.title=title; 
     this.image=image; 
    } 
} 
class MySimpleAdapter extends BaseAdapter{ 

    ArrayList<SingleRow> list; 
    Context context; 
    public MySimpleAdapter(Context c) { 
     // TODO Auto-generated constructor stub 
     context=c; 
     list=new ArrayList<SingleRow>(); 
     //putting actual values in array 
     Resources res=c.getResources(); 
     String[] titles=res.getStringArray(R.array.titles); 
     int[] images={R.drawable.error,R.drawable.ic_launcher,R.drawable.ic_launcher}; 

     //putting single row in arraylist 
     for(int i=0;i<3;i++) 
     { 
      list.add(new SingleRow(titles[i], images[i])); 
     } 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return list.size(); 
    } 
    @Override 
    public Object getItem(int i) { 
     // TODO Auto-generated method stub 
     return list.get(i); 
    } 
    @Override 
    public long getItemId(int i) { 
     // TODO Auto-generated method stub 
     return i; 
    } 

    //called when want to display row 
    @Override 
    public View getView(int i, View view, ViewGroup viewgroup) { 
     // TODO Auto-generated method stub 
     LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View row=inflater.inflate(R.layout.single_row,viewgroup,false); 
     TextView title=(TextView)row.findViewById(R.id.label); 
     ImageView image=(ImageView)row.findViewById(R.id.imageView); 
     SingleRow temp=list.get(i); 
     title.setText(temp.title); 
     image.setImageResource(temp.image); 
     return row; 
    } 
} 
+0

設置行佈局可點擊 –

+0

您可以在onItemClickListener中開啓位置值。 – Jumpa

回答

0

在您的項目點擊更改您的代碼以

list1.setOnItemClickListener(new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View v, int position,long id) { 
     // TODO Auto-generated method stub 
     String[] titles=res.getStringArray(R.array.titles); 
     if((titles[position]).equalsIgnoreCase("Itemx")) 
     { 
       Intent i=new Intent(homeScreen.this, privateSpaceList.class); 
       startActivity(i); 
     } 
     else if((titles[position]).equalsIgnoreCase.equals("Itemy")) 
     { 
       Intent i=new Intent(homeScreen.this, activity2.class); 
       startActivity(i); 
     } 
     else if((titles[position]).equalsIgnoreCase.equals("Itemz")) 
     { 
       Intent i=new Intent(homeScreen.this, activity3.class); 
       startActivity(i); 
     } 
    } 
}); 
+0

謝謝你解決了我的問題 – bhagyashree

1

我想對實施ClickListener getView在適配器類簡單的方法。或者,你的情況,你需要存儲名單< ..>,也該列表添加到適配器,在這一刻:

​​

你得到你的對象。

或像這樣:

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

    switch(position) { 

     case 1: 
      Intent intent = new Intent(homeScreen.this, privateSpaceList.class); 
      startActivity(intent); 
      break; 

     } 

    } 
+0

謝謝你的幫助 – bhagyashree

0

試試這個..

list1.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View v, int position,long id) { 
      // TODO Auto-generated method stub 
      TextView title=(TextView)v.findViewById(R.id.label);  
      if(title.getText().toString().trim().equals("Item x")) 
      { 
        Intent i=new Intent(homeScreen.this, activity1.class); 
        startActivity(i); 
      } 
      else if(title.getText().toString().trim().equals("Item y")) 
      { 
        Intent i=new Intent(homeScreen.this, activity2.class); 
        startActivity(i); 
      } 
      else if(title.getText().toString().trim().equals("Item z")) 
      { 
        Intent i=new Intent(homeScreen.this, activity3.class); 
        startActivity(i); 
      } 
     } 
    }); 
+0

謝謝你的幫助 – bhagyashree

0

嘗試這樣,

它非常簡單沒有太多條件的方式。

定義所有Activity類名稱的數組。 但是所有的活動應該在同一個包中。

String[] Classtitles=new String[]{"Activity1","Activity2","Activity3"}; 

獲取包名稱

final String packageName = this.getClass().getPackage().getName(); 

而onItemClick(內改變)

list1.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View v, int position,long id) { 
       try { 
         Class c = Class.forName(packageName + "." + Classtitles[position]); 
         startActivity(new Intent(homeScreen.this, c)); 
        } catch (ClassNotFoundException e) { 
         Toast.makeText(homeScreen.this, String.valueOf(e), 5000).show(); 
        } 
      } 
     }); 

希望這會幫助你。

+0

謝謝你的幫助 – bhagyashree