如何将JSON响应数据放入android中的适配器

时间:2017-06-01 19:34:10

标签: android android-layout listview android-studio android-adapterview

MainActivity.java

   JSONObject dataObject = resp.getJSONObject("data");

                        JSONArray arrJson= dataObject.getJSONArray("feed");
                        String[] arr=new String[arrJson.length()];
                        for(int i=0;i<arrJson.length();i++) {
                            arr[i] = arrJson.getString(i);
                            Log.d("asdata2",arr[i].toString());
                        }

                        //first obj
                        JSONObject jobj=new JSONObject(arr[0]);

                        Log.i("TAG2", "id: " +jobj.getString("id"));
                        Log.i("TAG2", "businessName: " + jobj.getString("businessName"));
                        Log.i("TAG2", "businessLogoUrl: " + jobj.getString("businessLogoUrl"));
                        Log.i("TAG2", "text: " + jobj.getString("text"));
                        Log.i("TAG2", "photoUrl: " + jobj.getString("photoUrl"));
                        Log.i("TAG2", "videoUrl: " + jobj.getString("videoUrl"));
                        Log.i("TAG2", "isNew: " + jobj.getString("isNew"));
                        Log.i("TAG2", "type: " + jobj.getString("type"));

                        Log.d("jobjj",jobj.toString());

                        PromotionsFeedActivity.Titles.add(jobj.getString("businessName"));
                        PromotionsFeedActivity.Pictures.add(jobj.getString("photoUrl"));
                        PromotionsFeedActivity.Categorie.add(jobj.getString("type"));
                        PromotionsFeedActivity.videoURL.add(jobj.getString("videoUrl"));
                        PromotionsFeedActivity.Id.add(jobj.getString("id"));
                        //PromotionsFeedActivity.BuyUrl.add("null");
                        PromotionsFeedActivity.PartnerId.add("null");
 context.startActivity(new Intent(context,PromotionsFeedActivity.class));

PromotionFeedAdapter.java

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 );
        final ViewHolder2 viewHolder=new ViewHolder2();

        // Layout Folder For Promotion

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

        // Navigationb listner onClick Promotion

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

        // Picking the layout based on the partnerId

        Log.i("Position",String.valueOf(position));
        switch (PromotionsFeedActivity.PartnerId.get(position)){

            case "5":layout=R.layout.livefeed_adapter;
                     break;
            case "3":layout=R.layout.livefeed_adapter_groupon;
                    break;
            case "0":layout=R.layout.livefeed_adapter_news;
                    break;
        }

        convertView.setTag(viewHolder);
        mainViewHolder=(ViewHolder2)convertView.getTag();
        mainViewHolder.title.setText(getItem(position));
//        mainViewHolder.title2.setText(getItem(position));
//        mainViewHolder.pf_type.setText(getItem(position));
        String title2,picUrl,category,type;
        title2=PromotionsFeedActivity.Title2.get(position);
        picUrl=PromotionsFeedActivity.Pictures.get(position);
        category=PromotionsFeedActivity.Categorie.get(position);
        mainViewHolder.category.setText(category);
        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 class ViewHolder2 {
        TextView title;
        TextView title2;
        ImageView picture;
        TextView category;
    }

PromotionFeedActivity.java

public static ArrayList<String> Id=new ArrayList<String> ();

    public static ArrayList<String> Titles = new ArrayList<String>();
    public static ArrayList<String> Title2 = new ArrayList<String>();
    public static ArrayList<String> Categorie = new ArrayList<String>();
    public static ArrayList<String> videoURL=new ArrayList<String> ();
    public static ArrayList<String> Pictures=new ArrayList <String> ();
    public static ArrayList<String> PartnerId=new ArrayList<String>();

    public static ArrayAdapter<String> pfAdapter;
    ListView show;
    VideoView videoView;
    ImageView btnWatchVideo;
    private TextView mTextMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_promotions_feed);

        btnWatchVideo = (ImageView) findViewById(R.id.btnWatchVideo);
        btnWatchVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(PromotionsFeedActivity.this,MediaActivity.class));
            }
        });

        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);


        Log.i("PartnerSize",String.valueOf(PartnerId.size()));

        show= (ListView) findViewById(R.id.lv_pf);
        pfAdapter = new PromotionFeedAdapter(PromotionsFeedActivity.this, R.layout.livefeed_adapter, Title2);
        pfAdapter = new PromotionFeedAdapter(PromotionsFeedActivity.this,R.layout.livefeed_adapter,Titles);
        showData(pfAdapter);
 public void showData(ArrayAdapter<String> adapter){
        adapter.notifyDataSetChanged();
        show.setAdapter(adapter);
    }

activity_promotionfeed.xml

   <ListView
            android:layout_marginTop="80dp"
            android:layout_width="match_parent"
            android:layout_height="435dp"
            android:id="@+id/lv_pf">

        </ListView>

activity_livefeed.xml


<LinearLayout
        android:layout_width="380dp"
        android:layout_height="50dp"
        android:layout_marginLeft="2dp"
        android:src="@drawable/img_frame_1"
        android:orientation="horizontal"
        android:background="@drawable/boarder">

        <ImageView
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@drawable/special_icon"
            android:layout_marginTop="5dp"
            android:layout_marginLeft="5dp" />
        <TextView
            android:id="@+id/pf_title"
            android:layout_width="330dp"
            android:layout_height="wrap_content"
            android:text="$80 Toward Gourmet Stackhouse"
            android:layout_marginTop="16dp"
            android:textStyle="bold"
            android:textSize="16sp" />

    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="200dp">
        <ImageView
            android:id="@+id/pf_main_pic"
            android:layout_width="370dp"
            android:layout_height="220dp"
            android:src="@color/colorPrimaryDark"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />
        <ImageView
            android:layout_width="65dp"
            android:layout_height="65dp"
            android:src="@drawable/special_tag"
            android:layout_alignRight="@+id/pf_main_pic"
            android:layout_alignEnd="@+id/pf_main_pic" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">

        <TextView
            android:id="@+id/pf_category"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Artistic Expert Picture Framing"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/pf_type"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Retail"
            android:textStyle="normal"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"/>

    </RelativeLayout>

我想在列表视图中设置适配器上的数据。但是当我运行应用程序时数据没有显示。任何人都可以帮助我。每件事看起来都很好。但是当我运行应用程序时它没有显示在列表视图。

1 个答案:

答案 0 :(得分:0)

1。而不是使用static中的PromotionsFeedActivity列表,在ArrayList中声明MainActivity

2。获取JOSN回复并将string添加到ArrayList

3。最后使用Intent将ArrayList传递给PromotionsFeedActivity

4。PromotionsFeedActivity中,从ArrayList获取Intent并将其传递给Adapter以填充ListView上的数据。

更新您的MainActivityPromotionsFeedActivity,如下所示:

<强> MainActivity:

ArrayList<String> Titles = new ArrayList<String>();

..........
....................

// Get response and store it into list
Titles.add(jobj.getString("businessName"));

// Send Titles to PromotionsFeedActivity
Intent intent = new Intent(MainActivity.this, PromotionsFeedActivity.class);
intent.putStringArrayListExtra("LIST", Titles);
startActivity(intent);

<强> PromotionsFeedActivity:

public ArrayList<String> Titles;
public static ArrayAdapter<String> pfAdapter;
ListView show;

........
...............

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_promotions_feed);

    show = (ListView) findViewById(R.id.lv_pf);

    // Get Titles from Intent send from MainActivity
    Titles = getIntent().getStringArrayListExtra("LIST");
    // Adapter 
    pfAdapter = new PromotionFeedAdapter(PromotionsFeedActivity.this, R.layout.livefeed_adapter, Titles);

    // Set adapter
    show.setAdapter(adapter);
}