Android -- Adding And Removing Items From A Multiline Listview With A Custom Arrayadapter
Solution 1:
I wrote a tutorial recently on how to create a similar listview. Check it out here
http://www.shubhayu.com/android/listview-with-arrayadapter-and-customized-items
This would help you in creating what you want except the deletion part. The deletion will not be difficult after that. You just need to handle it on the onClick of your delete button. I'll guide you through it once you get the list up and running.
Also, override you getCount(). That should do the trick.
Solution 2:
This worked for me, I suggest you use it. You are using two strings for maintaining the data, create a class and use that class to create arrayadapter. I'm posting an example from my current (incomplete) codes.
I use my custom ArrayAdapter as a singleton and keep modifying it through the instance. I also have a service running that maintains a backup of the data in the event Android destroys the singleton from memory. Everytime i do a notifydatasetchanged, I backup the rules in a public static variable inside that service. You can ignore this part because its only required for my case.
publicclassRuleManagerextendsArrayAdapter<Rule>
{
privatestatic RuleManager mInstance;
private final Context context;
privateRuleManager(Context context, List<Rule> r)
{
super(context,R.layout.main_menu_options_list_item);
if(r==null)r=new ArrayList<Rule>();
this.context=context;
}
publicstatic synchronized RuleManager getInstance(Context context,List<Rule> r)
{
if (mInstance == null)
{
if(r==null)
r=CallMonitor.ruleBackup; //A service that contains copy of entire data (as a backup if android destroys stuffs)
mInstance = new RuleManager(context, r);
}
return mInstance;
}
publicvoidaddRule(Rule rule)
{
add(rule);
notifyDataSetChanged();
backupRules();
}
privatevoidbackupRules()
{
CallMonitor.ruleBackup=null;
CallMonitor.ruleBackup=new ArrayList<Rule>();
for(int i=0;i<getCount();i++)
CallMonitor.ruleBackup.add(getItem(i));
}
publicvoideditRule(int position,Rule rule)
{
getItem(position).setAbc(rule.getAbc);
getItem(position).setDef(rule.getDef());
getItem(position).setGhi(rule.getGhi());
getItem(position).setJkl(rule.getJkl());
notifyDataSetChanged();
backupRules();
}
public boolean deleteRule(int position)
{
try
{
remove(getItem(position));
notifyDataSetChanged();
backupRules();
returntrue;
}
catch(Exception e)
{
returnfalse;
}
}
}
Solution 3:
super(MainActivity.this, R.layout.row, songs);
Why do you have this in the adapter constructor passing list of songs
.
I'm not sure, as i haven't implemented listview with multiple data resources(2 lists in ur case).
But the ideal way of doing it would be to create a new class ( in your case it would just have two strings : Songs , artists). such that Single object of that class represents a single row information in list view. And thus passing that to your custom adapter, and in getView() using data from that class objects for list view.
In your case , notifydatasetchanged() might not be working because you have set songs
to be the objects to represent in the ListView
in the constructor of adapter and that is not changing ever... thus list is not refreshing at all...
Post a Comment for "Android -- Adding And Removing Items From A Multiline Listview With A Custom Arrayadapter"