Reputation: 931
im a noob to android and i am populating a listview with an arrayilst and custom adapter. I want setup my listview onClickListener to execute commands based to the the items populating the listview. the listview is dynamically populated with items from the arraylist. I have tried the position and id parameters with no luck. Any help is greatly appreciated.
How I instantiate listview, arraylist, and adapter
portfoliolist = (ListView) findViewById(R.id.listViewPortfolios);
users = new ArrayList<PortfolioRecord>();
portfoliolist.setAdapter(new UserItemAdapter(this, R.layout.simplerow, users));
portfoliolist.setOnItemClickListener(this);
My Custom Adapter.
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;
}
}
How i add items to arraylist:
user1 = new PortfolioRecord(pn10, denomination10+portfoliovalue10);
users.add(user1);
I have added Usernull, user1 and user2 to my arraylist. How do i identify these items? I've tried the folowing code with no luck
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;}}
Upvotes: 0
Views: 151
Reputation: 348
No need to implement OnItemSelectedListener. Just use this within the existing OnItemClick method:
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;
}}
Upvotes: 1
Reputation: 867
supposing your portfolioRecord object has the following: 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;
}
}
});
Upvotes: 1
Reputation: 86948
I recommend changing to a OnItemSelectedListener and then you could simply fetch the PortfolioRecord with:
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;
}
}
});
Upvotes: 0