2012-09-04 59 views
0

即時通訊android的一個noob和我填充陣列和自定義適配器的列表視圖。我想設置我的列表視圖onClickListener執行基於填充列表視圖的項目的命令。該列表視圖會動態地填入來自數組列表的項目。我已經試過位置和id參數,沒有運氣。任何幫助是極大的讚賞。我如何確定填充listview的項目?

我如何實例列表視圖,ArrayList中,和適配器

portfoliolist = (ListView) findViewById(R.id.listViewPortfolios); 
    users = new ArrayList<PortfolioRecord>(); 
    portfoliolist.setAdapter(new UserItemAdapter(this, R.layout.simplerow, users)); 
    portfoliolist.setOnItemClickListener(this);  

我的自定義適配器。

public class UserItemAdapter extends ArrayAdapter<PortfolioRecord> { 
    private ArrayList<PortfolioRecord> users; 

    public UserItemAdapter(Context context, int textViewResourceId, ArrayList<PortfolioRecord> users) { 
     super(context, textViewResourceId, users); 
     this.users = users; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     if (v == null) { 
      LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.simplerow, null); 
     } 

     PortfolioRecord user = users.get(position);   
     if (user != null) { 
      TextView portfolioname = (TextView) v.findViewById(R.id.portfolioname); 
      TextView currentvalue = (TextView) v.findViewById(R.id.currentvalue); 

      if (portfolioname != null) { 
       portfolioname.setText(user.portfolioname); 
      } 

      if(currentvalue != null) { 
       currentvalue.setText("Current Value: " + user.currentvalue); 
      } 
     } 
     return v; 
    } 
} 

public class PortfolioRecord { 
    public String portfolioname; 
    public String currentvalue; 

    public PortfolioRecord(String portfolioname, String currentvalue) { 
     this.portfolioname = portfolioname; 
     this.currentvalue = currentvalue;  
    } 
} 

我如何將項目添加到ArrayList中:

  user1 = new PortfolioRecord(pn10, denomination10+portfoliovalue10);   
     users.add(user1); 

我加入Usernull,user1和user2我的數組列表。我如何識別這些項目?我已經試過了如下因素代碼,沒有運氣

public void onItemClick(AdapterView parent, View itemClicked, int position, 
     long id) { 
    TODO Auto-generated method stub 
    switch(parent.getId()){ 
    case R.id.listViewPortfolios: 
     if(portfoliolist.getSelectedItem()==usernull){ 
      openCustomDialog(); 
     }else if(portfoliolist.getSelectedItem()==user1){ 
      whichportfolio=1;    
     }else if(portfoliolist.getSelectedItem()==user2){ 
      whichportfolio=2;    
     } 
    break;}} 

回答

1

不需要實現OnItemSelectedListener。只需使用現有的OnItemClick方法:

public void onItemClick(AdapterView parent, View itemClicked, int position, 
     long id) { 
    // TODO Auto-generated method stub 
    PortfolioRecord user = users.get(position); 

     // I'm not certain what this code block does... 
     if(user.equals(usernull)){ 
      openCustomDialog(); 
     }else if(user.equals(user1)){ 
      whichportfolio=1;    
     }else if(user.equals(user2)){ 
      whichportfolio=2;    
     }} 
0

我建議改變到OnItemSelectedListener,然後你可以簡單地獲取PortfolioRecord有:

listView.setOnItemSelectedListener(new OnItemSelectedListener() { 
    public void onItemSelected(AdapterView parent, View view, int position, long id) { 
     // Get the user (PortfolioRecord) that was selected 
     PortfolioRecord user = users.get(position); 

     // I'm not certain what this code block does... 
     if(user.equals(usernull)){ 
      openCustomDialog(); 
     }else if(user.equals(user1)){ 
      whichportfolio=1;    
     }else if(user.equals(user2)){ 
      whichportfolio=2;    
     } 
    } 
}); 
+0

感謝您的建議,但我仍然沒有任何運氣。 –

+0

您是否在LogCat中遇到任何錯誤? – Sam

+0

不,錯誤。我在onCreate中實現了你的代碼,並且listview沒有響應。然後我嘗試在OnItemclick方法中實現它,但沒有結果。 –

1

假設你portfolioRecord對象有以下幾點:getTypeofUser( )

portfoliolist.setOnItemClickListener(new OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position,long id) { 
     String typeofUser =users.get(position).getTypeofUser(); 
       if(typeofUser == user1){ 
        whichportfolio=1; 
       } 
    } 
    });