我在共享首选项中保存了大约6行,每行包含名称,描述,价格等。我有List,其中我必须填充从共享首选项中检索的每一行中的名称,描述,价格。如何获取行中的所有数据并填充列表?
我已经完成了
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater=null;
private ArrayList<HashMap<String, Object>> data;
//public ImageLoader imageLoader;
int i=0;
public LazyAdapter(Activity a) {
activity = a;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.order_list, null);
TextView orderDishDescription = (TextView)vi.findViewById(R.id.orderDishDescription);
TextView OrderDishName = (TextView)vi.findViewById(R.id.OrderDishName);
TextView OrderDishPrice = (TextView)vi.findViewById(R.id.OrderDishPrice);
ImageView imageview=(ImageView) vi.findViewById(R.id.list_image_order);
//ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); /
//HashMap<String, Object> song = new HashMap<String, Object>();
// song = data.get(position);
Log.i("iiiiii "," " +i++);
// Log.i("objj.Get_O_Id() ",objj.Get_O_Id());
// Log.i("objj.GetProductName() ",objj.GetProductName());
// Log.i("objj.GetDescription() ",objj.GetDescription());
//
OrderDishPrice.setText(OrderSharedPrefences.getDish_Price(getApplicationContext()));
OrderDishName.setText(OrderSharedPrefences.getUserName(getApplicationContext()));
orderDishDescription.setText(OrderSharedPrefences.getDish_Description(getApplicationContext()));
//imageview.setImageBitmap(objj.GetImage());
return vi;
}
}
答案 0 :(得分:1)
你的适配器好了,只需从你的Activity类中创建设置数据,就像那样 - 首先从set中的首选项中获取数据并将其设置为array-list,然后设置为list-view。 的修改 设置prefrence就像 -
SharedPreferences.Editor sEdit = sPrefs
.edit();
sEdit.putString("NAME", content);
sEdit.putStringSet("args", listArraySet);
sEdit.commit();
得到任何类似的地方 -
sPrefs=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
listArraySet=new HashSet<String>();
listArraySet=sPrefs.getStringSet(name1,new HashSet<String>());
list=new ArrayList<String>(listArraySet);
ArrayAdapter<String> ao=new ArrayAdapter<String>(this, R.layout.songs_list_item, R.id.songTitle, list);
lv.setAdapter(ao);
设置为listview的整个代码 -
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.songs_list);
list = new ArrayList<String>();
lv=(ListView)findViewById(R.id.listView1);
sPrefs=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
listArraySet=new HashSet<String>();
listArraySet=sPrefs.getStringSet(name1,new HashSet<String>());
list=new ArrayList<String>(listArraySet);
ArrayAdapter<String> ao=new ArrayAdapter<String>(this, R.layout.songs_list_item, R.id.songTitle, list);
lv.setAdapter(ao);
//-------------------------CLICK ON LIST ITEM_-----------------------
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int index = parent.getPositionForView(view);
String filename = list.get(index);
}
});
}
}