【问题标题】:How to place the dynamically changing arraylist of values into a adapter view in android如何将动态变化的值数组列表放入android中的适配器视图
【发布时间】:2017-06-09 16:13:44
【问题描述】:

MainActivity.java

    public static ArrayList<FeedPojo> feedLayout = new ArrayList<FeedPojo>();
            public static ArrayAdapter<String> pfAdapter;   

         for(int j=0 ; j < jsonArray1.length(); j++){
             strings[j] = jsonArray1.getString(j);
                                JSONObject promoJsonObject = new JSONObject(strings[j]);
                                Log.d(TAG,"Promojobj"+promoJsonObject);
                                FeedPojo.setType(promoJsonObject.getString("type"));
                                Log.d(TAG, "TypeFeed:" + FeedPojo.getType());
                                FeedParsePojo.setData(promoJsonObject);
                                Log.d(TAG,"FeedData"+FeedParsePojo.getData());
                                  FeedPojo feedPojoNews = new FeedPojo(promoJsonObject.getString("id"),
                                                promoJsonObject.getString("businessName"),
                                                promoJsonObject.getString("businessLogoUrl"),
                                                promoJsonObject.getString("text"),
                                                promoJsonObject.getString("photoUrl"),
                                                promoJsonObject.getString("videoUrl"),
                                                promoJsonObject.getString("isNew"));
                                        feedLayout.add(feedPojoNews);
Log.d(TAG,"ListOfValues"+feedLayout);
            }

FeedAdapter.java

public class PromotionFeedAdapter extends ArrayAdapter<String> {

    public int layout;
    Button getCal;
    WebView display;
    public static String MY_PREFS_NAME="my_prefs";
    private static final String TAG = PlatformHttp.class.getSimpleName();
    Context mContext;


    public PromotionFeedAdapter(Context context, int resource, List<String> objects) {
        super(context, resource, objects);
        layout=resource;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent){


 //ViewHolder2 mainViewHolder = null;

        mContext = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView= inflater.inflate(layout,parent,false);


        // Layout Folder For Promotion

        ViewHolder viewHolder = new ViewHolder();

        viewHolder.title = (TextView) convertView.findViewById(R.id.pf_title);
        viewHolder.category = (TextView) convertView.findViewById(R.id.pf_category);
        viewHolder.picture = (ImageView) convertView.findViewById(R.id.pf_main_pic);
        viewHolder.brand = (TextView) convertView.findViewById(R.id.pf_type);


        // Navigation listner onClick Promotion

        viewHolder.title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getContext().startActivity(new Intent(getContext(),Webview.class));
            }
        });


        convertView.setTag(viewHolder);
        TextView title = ((ViewHolder)convertView.getTag()).title;
        TextView category = ((ViewHolder) convertView.getTag()).category;
        TextView brand  = ((ViewHolder) convertView.getTag()).brand;
        title.setText(getItem(position));
        category.setText(getItem(position));
        brand.setText(getItem(position));

        String picUrl;

        picUrl=PromotionsFeedActivity.Pictures.get(position);
        convertView.setTag(viewHolder);
        URL newurl = null;
        try {
            newurl = new URL(picUrl);
            Picasso.with(getContext()).load(picUrl).into(viewHolder.picture);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return convertView;
    }

    public static class ViewHolder {
        TextView title;
        TextView brand;
        ImageView picture;
        TextView category;
    }

在 MainActivity 类中,我已将值添加到 ArrayList(feedLayout) 中。我希望将这些值放置在适配器视图中。在对象的每个数组列表中都有存储在其中的值,我希望它们被获取并显示在适配器视图上。

【问题讨论】:

    标签: android android-layout listview android-arrayadapter android-adapter


    【解决方案1】:

    您可以将对象列表传递给arrayAdapter,然后调用它

    adapter.notifyDataSetChanged();
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-24
      • 2012-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多