我有个问题。我不能修改一些代码来提升它的功能。
这里的代码:
final Item[] items = {
new Item("Email", android.R.drawable.ic_menu_add, 7),
new Item("Facebook", resId, 99),
};
ListAdapter adapter = new ArrayAdapter<Item>(
this,
android.R.layout.select_dialog_item,
android.R.id.text1,
items){
public View getView(int position, View convertView, ViewGroup parent) {
//User super class to create the View
View v = super.getView(position, convertView, parent);
TextView tv = (TextView)v.findViewById(android.R.id.text1);
//Put the image on the TextView
tv.setCompoundDrawablesWithIntrinsicBounds(items[position].icon, 0, 0, 0);
//Add margin between image and text (support various screen densities)
int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
tv.setCompoundDrawablePadding(dp5);
return v;
}
};
此代码必须创建一个带有左侧图标的菜单,并且需要一个类,这里:
public class Item {
public final String text;
public final int icon;
public final int act;
public Item(String text, Integer icon, Integer act) {
this.text = text;
this.icon = icon;
this.act = act;
}
@Override
public String toString() {
return text;
}
public int getAct() {
return act;
}
}
我想修改此类 Item 以轻松添加项目,例如,Item.addItem(str,int,int)
怎么做?