即时通讯到Android的菜鸟我正在用arrayilst和自定义适配器填充listview。我想设置我的listview onClickListener来执行基于填充listview的项目的命令。 listview是使用arraylist中的项目动态填充的。我已经尝试过position和id参数而没有运气。非常感谢任何帮助。
如何实例化listview,arraylist和adapter
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添加到我的arraylist中。我该如何识别这些物品?我已经尝试了下面没有运气的代码
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;}}
答案 0 :(得分: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;
}
}
});
答案 1 :(得分: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;
}}
答案 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;
}
}
});