0

我已经保存了大约 6 行保存在共享首选项中,每行包含名称、描述、价格等。我有一个列表,我必须在从共享首选项中检索到的每一行中填充名称、描述、价格。如何获取行中的所有数据并填充列表?

我已经做好了

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;
    }
    }
4

1 回答 1

1

您的适配器没问题,只需从您的 Activity 类中设置数据,类似的东西 - 首先从集合中的首选项中获取数据并设置到数组列表中,然后设置到列表视图中。 编辑 像这样设置偏好 -

                    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);

设置到列表视图中的整个代码-

@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);              

            }
        });
    }
}
于 2013-09-27T13:07:51.413 回答