【问题标题】:How to place JSON response data into adapter in android如何将JSON响应数据放入android中的适配器
【发布时间】:2017-11-03 02:02:09
【问题描述】:

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>

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

【问题讨论】:

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


    【解决方案1】:

    1. 不要使用来自PromotionsFeedActivitystatic 列表,而是在MainActivity 中声明ArrayList

    2. 获取 JOSN 响应并将string 添加到ArrayList

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

    4.PromotionsFeedActivity 中,从Intent 获取ArrayList 并将其传递给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);
    }
    

    【讨论】:

    • 致命异常:java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.arunmannuru.arun/com.example.arunmannuru.arun.PromotionsFeedActivity}:java.lang.NullPointerException:尝试在空对象引用上调用接口方法'int java.util.List.size()' ....它显示此错误并且应用程序正在崩溃。但它显示在适配器@FAT
    • 发布您更新的 MainActivity 和 Promotio‌​nsFeedActivity
    • //MainActiviy.java ArrayList Titles = new ArrayList(); Titles.add(jobj.getString("businessName")); Intent intent = new Intent(MainActivity.this,PromotionsFeedActivity.class); intent.putStringArrayListExtra("LIST",Titles);开始活动(意图);
    • PromotionFeedActivity.java show= (ListView) findViewById(R.id.lv_pf);标题 = getIntent().getStringArrayListExtra("LIST"); //pfAdapter = new PromotionFeedAdapter(PromotionsFeedActivity.this, R.layout.livefeed_adapter, Title2); pfAdapter = new PromotionFeedAdapter(PromotionsFeedActivity.this,R.layout.livefeed_adapter,Titles); show.setAdapter(pfAdapter);
    • 您的 Titles ArrayList 为空。检查标题的大小
    猜你喜欢
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 2021-08-01
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多