0

我是 android 的菜鸟,我正在使用 arrayilst 和自定义适配器填充列表视图。我想设置我的列表视图 onClickListener 以根据填充列表视图的项目执行命令。列表视图动态填充来自 arraylist 的项目。我已经尝试了 position 和 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;}}
4

3 回答 3

1

假设您的投资组合记录对象具有以下内容: 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; 
                 }
    }
    });
于 2012-09-04T22:58:54.623 回答
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;             
        }}
于 2012-09-05T02:14:16.180 回答
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;               
        }
    }
});
于 2012-09-04T21:20:47.767 回答