2017-05-31 83 views
-1

我想使用recyclerview CardView开发Android应用程序,从JSON文件获取数据。如何在CardView中使用JSON从特定ID获取值

我面对的错误是我在特定的帖子(如Facebook)中为所有用户获取图像和文本,而不仅仅是特定用户的数据。

这里是我的JSON代码:

{ 
    "posts": [{ 
     "post_id": "59", 
     "post_desc": "", 
     "firstname": "karan", 
     "lastname": "qq", 
     "created_at": "2017-05-30 23:17:46", 
     "files": [{ 
      "file_name": "1496166466_IMG-20170529-WA0041.jpg", 
      "file_type": "1" 
     }] 
    }, { 
     "post_id": "58", 
     "post_desc": "\ud83d\ude0d\ud83d\ude0a", 
     "firstname": "karan", 
     "lastname": "qq", 
     "created_at": "2017-05-30 12:12:36", 
     "files": [] 
    }, { 
     "post_id": "57", 
     "post_desc": "Good Morning", 
     "firstname": "karan", 
     "lastname": "qq", 
     "created_at": "2017-05-30 11:29:43", 
     "files": [] 
    }, { 
     "post_id": "56", 
     "post_desc": "", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-26 16:50:49", 
     "files": [] 
    }, { 
     "post_id": "55", 
     "post_desc": "", 
     "firstname": "jaxi", 
     "lastname": "chawda", 
     "created_at": "2017-05-26 16:50:04", 
     "files": [] 
    }, { 
     "post_id": "54", 
     "post_desc": "", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-26 16:47:24", 
     "files": [{ 
      "file_name": "1495797444_IMG_20170522_191006_842.jpg", 
      "file_type": "1" 
     }] 
    }, { 
     "post_id": "53", 
     "post_desc": "", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-26 16:45:18", 
     "files": [] 
    }, { 
     "post_id": "52", 
     "post_desc": "", 
     "firstname": "jaxi", 
     "lastname": "chawda", 
     "created_at": "2017-05-26 16:32:24", 
     "files": [] 
    }, { 
     "post_id": "51", 
     "post_desc": "", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-26 16:18:31", 
     "files": [] 
    }, { 
     "post_id": "50", 
     "post_desc": "", 
     "firstname": "karan", 
     "lastname": "qq", 
     "created_at": "2017-05-23 17:37:19", 
     "files": [{ 
      "file_name": "1495541239_VID-20170518-WA0029.mp4", 
      "file_type": "2" 
     }] 
    }, { 
     "post_id": "49", 
     "post_desc": "", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-16 16:19:13", 
     "files": [{ 
      "file_name": "1494931753_IMG_3919.mp4", 
      "file_type": "2" 
     }] 
    }, { 
     "post_id": "48", 
     "post_desc": "", 
     "firstname": "jaxi", 
     "lastname": "chawda", 
     "created_at": "2017-05-16 15:09:32", 
     "files": [{ 
      "file_name": "1494927572_Technology in the Future.mp4", 
      "file_type": "2" 
     }] 
    }, { 
     "post_id": "47", 
     "post_desc": "", 
     "firstname": "pankti", 
     "lastname": "shah", 
     "created_at": "2017-05-16 13:30:09", 
     "files": [{ 
      "file_name": "1494921609_Tom and Jerry - Little School Mouse.mp4", 
      "file_type": "2" 
     }] 
    }, { 
     "post_id": "46", 
     "post_desc": "", 
     "firstname": "pankti", 
     "lastname": "shah", 
     "created_at": "2017-05-16 13:16:34", 
     "files": [{ 
      "file_name": "1494920794_Tubelight _ Official Teaser _ Salman Khan _ Kabir Khan.mp4", 
      "file_type": "2" 
     }] 
    }, { 
     "post_id": "45", 
     "post_desc": "", 
     "firstname": "pankti", 
     "lastname": "shah", 
     "created_at": "2017-05-16 13:13:51", 
     "files": [{ 
      "file_name": "1494920631_small.mp4", 
      "file_type": "2" 
     }] 
    }, { 
     "post_id": "44", 
     "post_desc": "", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-13 16:45:08", 
     "files": [{ 
      "file_name": "1494674108_IMG_4126.JPG", 
      "file_type": "1" 
     }] 
    }, { 
     "post_id": "43", 
     "post_desc": "", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-13 16:24:30", 
     "files": [{ 
      "file_name": "1494672870_IMG_4123.JPG", 
      "file_type": "1" 
     }] 
    }, { 
     "post_id": "42", 
     "post_desc": "", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-13 16:23:25", 
     "files": [{ 
      "file_name": "1494672805_IMG_4124.JPG", 
      "file_type": "1" 
     }] 
    }, { 
     "post_id": "41", 
     "post_desc": "", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-13 15:48:39", 
     "files": [{ 
      "file_name": "1494670719_IMG_4121.JPG", 
      "file_type": "1" 
     }] 
    }, { 
     "post_id": "40", 
     "post_desc": "", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-12 22:53:24", 
     "files": [{ 
      "file_name": "1494609804_IMG_4119.JPG", 
      "file_type": "1" 
     }] 
    }, { 
     "post_id": "39", 
     "post_desc": "", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-12 18:28:28", 
     "files": [{ 
      "file_name": "1494593908_IMG_4015.JPG", 
      "file_type": "1" 
     }] 
    }, { 
     "post_id": "38", 
     "post_desc": "", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-12 14:13:20", 
     "files": [{ 
      "file_name": "1494578600_IMG_4113.PNG", 
      "file_type": "1" 
     }] 
    }, { 
     "post_id": "37", 
     "post_desc": "", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-12 14:13:18", 
     "files": [{ 
      "file_name": "1494578598_IMG_4113.PNG", 
      "file_type": "1" 
     }] 
    }, { 
     "post_id": "36", 
     "post_desc": "", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-12 14:13:15", 
     "files": [{ 
      "file_name": "1494578595_IMG_4113.PNG", 
      "file_type": "1" 
     }] 
    }, { 
     "post_id": "35", 
     "post_desc": "", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-12 14:13:12", 
     "files": [{ 
      "file_name": "1494578592_IMG_4113.PNG", 
      "file_type": "1" 
     }] 
    }, { 
     "post_id": "34", 
     "post_desc": "", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-12 14:13:09", 
     "files": [{ 
      "file_name": "1494578589_IMG_4113.PNG", 
      "file_type": "1" 
     }] 
    }, { 
     "post_id": "33", 
     "post_desc": "", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-11 23:43:42", 
     "files": [{ 
      "file_name": "1494526422_IMG_3950.JPG", 
      "file_type": "1" 
     }] 
    }, { 
     "post_id": "32", 
     "post_desc": "", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-11 23:43:10", 
     "files": [{ 
      "file_name": "1494526390_IMG_3944.JPG", 
      "file_type": "1" 
     }] 
    }, { 
     "post_id": "31", 
     "post_desc": "", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-11 23:26:24", 
     "files": [{ 
      "file_name": "1494525384_IMG_3127.JPG", 
      "file_type": "1" 
     }] 
    }, { 
     "post_id": "30", 
     "post_desc": "", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-11 23:26:20", 
     "files": [{ 
      "file_name": "1494525380_IMG_3127.JPG", 
      "file_type": "1" 
     }] 
    }, { 
     "post_id": "29", 
     "post_desc": "", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-11 23:24:12", 
     "files": [{ 
      "file_name": "1494525252_IMG_2600.JPG", 
      "file_type": "1" 
     }] 
    }, { 
     "post_id": "28", 
     "post_desc": "", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-11 23:24:06", 
     "files": [{ 
      "file_name": "1494525246_IMG_2600.JPG", 
      "file_type": "1" 
     }] 
    }, { 
     "post_id": "27", 
     "post_desc": "", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-11 23:22:04", 
     "files": [{ 
      "file_name": "1494525124_IMG_3113.JPG", 
      "file_type": "1" 
     }] 
    }, { 
     "post_id": "26", 
     "post_desc": "", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-11 23:21:11", 
     "files": [{ 
      "file_name": "1494525071_1491304854804.jpeg", 
      "file_type": "1" 
     }] 
    }, { 
     "post_id": "25", 
     "post_desc": "", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-11 23:20:26", 
     "files": [{ 
      "file_name": "1494525026_IMG_4050.JPG", 
      "file_type": "1" 
     }] 
    }, { 
     "post_id": "24", 
     "post_desc": "Ganesh Mahotsav 2016 ", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-11 23:20:01", 
     "files": [{ 
      "file_name": "1494525001_IMG_4045.PNG", 
      "file_type": "1" 
     }] 
    }, { 
     "post_id": "23", 
     "post_desc": "", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-11 23:19:03", 
     "files": [{ 
      "file_name": "1494524943_IMG_4016.JPG", 
      "file_type": "1" 
     }] 
    }, { 
     "post_id": "22", 
     "post_desc": "\ud83d\ude0d\n", 
     "firstname": "karan", 
     "lastname": "qq", 
     "created_at": "2017-05-10 14:57:16", 
     "files": [] 
    }, { 
     "post_id": "21", 
     "post_desc": "\ud83d\ude0a\n", 
     "firstname": "karan", 
     "lastname": "qq", 
     "created_at": "2017-05-04 11:06:07", 
     "files": [] 
    }, { 
     "post_id": "20", 
     "post_desc": "\ud83d\ude0a\n", 
     "firstname": "karan", 
     "lastname": "qq", 
     "created_at": "2017-05-04 11:06:03", 
     "files": [] 
    }, { 
     "post_id": "19", 
     "post_desc": "Good morning", 
     "firstname": "karan", 
     "lastname": "qq", 
     "created_at": "2017-05-04 11:05:27", 
     "files": [] 
    }, { 
     "post_id": "18", 
     "post_desc": "https:\/\/www.youtube.com\/watch?v=Qtp42J9IOUo", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-02 16:39:53", 
     "files": [] 
    }, { 
     "post_id": "17", 
     "post_desc": "\ud83d\ude1c\ud83d\ude1c\ud83d\ude1c\ud83d\ude1c\ud83d\ude1c\ud83d\ude1c\ud83d\ude1c", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-02 16:38:40", 
     "files": [] 
    }, { 
     "post_id": "16", 
     "post_desc": "hi hello", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-02 16:30:35", 
     "files": [] 
    }, { 
     "post_id": "15", 
     "post_desc": "\ud83d\ude0e\ud83d\ude34\ud83d\ude01", 
     "firstname": "Manish", 
     "lastname": "Amin", 
     "created_at": "2017-05-02 16:30:19", 
     "files": [] 
    }, { 
     "post_id": "14", 
     "post_desc": "\ud83d\ude0e\ud83d\ude34\ud83d\ude01", 
     "firstname": "Manish", 
     "lastname": "Amin", 
     "created_at": "2017-05-02 16:30:17", 
     "files": [] 
    }, { 
     "post_id": "13", 
     "post_desc": "\ud83d\ude0e\ud83d\ude34\ud83d\ude01", 
     "firstname": "Manish", 
     "lastname": "Amin", 
     "created_at": "2017-05-02 16:30:17", 
     "files": [] 
    }, { 
     "post_id": "12", 
     "post_desc": "\ud83d\ude0e\ud83d\ude34\ud83d\ude01", 
     "firstname": "Manish", 
     "lastname": "Amin", 
     "created_at": "2017-05-02 16:30:16", 
     "files": [] 
    }, { 
     "post_id": "11", 
     "post_desc": "\ud83d\ude0e\ud83d\ude34\ud83d\ude01", 
     "firstname": "Manish", 
     "lastname": "Amin", 
     "created_at": "2017-05-02 16:30:16", 
     "files": [] 
    }, { 
     "post_id": "10", 
     "post_desc": "\ud83d\ude0a\ud83d\ude0a\ud83d\ude04\ud83d\ude04\u263a\ufe0f\ud83d\ude01\ud83d\ude01", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-02 16:30:14", 
     "files": [] 
    }, { 
     "post_id": "9", 
     "post_desc": "Going to receive loads of money....", 
     "firstname": "Manish", 
     "lastname": "Amin", 
     "created_at": "2017-05-02 16:08:58", 
     "files": [] 
    }, { 
     "post_id": "8", 
     "post_desc": "\ud83d\ude0a\ud83d\ude0a\ud83d\ude0a\ud83d\ude0a\ud83d\ude0a", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-02 16:08:57", 
     "files": [] 
    }, { 
     "post_id": "7", 
     "post_desc": "\ud83d\ude0a\ud83d\ude0a\ud83d\ude0a\ud83d\ude0a\ud83d\ude0a", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-02 16:08:55", 
     "files": [] 
    }, { 
     "post_id": "6", 
     "post_desc": "\ud83d\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude02", 
     "firstname": "Abc", 
     "lastname": "Xyz", 
     "created_at": "2017-05-02 16:08:47", 
     "files": [] 
    }, { 
     "post_id": "5", 
     "post_desc": "\ud83c\udfe1\ud83c\udfeb", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-02 16:05:40", 
     "files": [] 
    }, { 
     "post_id": "4", 
     "post_desc": "\ud83d\udc4d\ud83d\udc4d\ud83d\ude0a\ud83d\ude12\ud83d\udc2e\ud83d\ude48\ud83d\ude1c\ud83d\ude09\ud83d\ude03", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-02 16:02:21", 
     "files": [] 
    }, { 
     "post_id": "3", 
     "post_desc": "\ud83d\ude12\ud83d\ude0a", 
     "firstname": "Rushin", 
     "lastname": "Patel", 
     "created_at": "2017-05-02 15:58:42", 
     "files": [] 
    }, { 
     "post_id": "2", 
     "post_desc": "\ud83d\ude33\ud83d\ude33\ud83d\ude33", 
     "firstname": "jaxi", 
     "lastname": "chawda", 
     "created_at": "2017-05-02 15:57:31", 
     "files": [] 
    }, { 
     "post_id": "1", 
     "post_desc": "hii....good afternoon...\ud83d\ude04\ud83d\ude04\ud83d\ude04", 
     "firstname": "jaxi", 
     "lastname": "chawda", 
     "created_at": "2017-05-02 15:55:49", 
     "files": [] 
    }] 
} 

这里是我的Android代码:

String filename = ""; 
      try { 
       JSONObject jsonObject = new JSONObject(s); 
       JSONArray posts = jsonObject.getJSONArray("posts"); 
       if(posts != null && posts.length() > 0) { 
        for (int i = 0; i < posts.length(); i++) { 
         JSONObject fileObj = posts.getJSONObject(i); 

         String fName = fileObj.getString("firstname"); 
         String created_at = fileObj.getString("created_at"); 
         String post_desc = fileObj.getString("post_desc"); 


         Log.e("Details", fName + "" + created_at + "" + post_desc); 

         JSONArray files = fileObj.getJSONArray("files"); 
         if(files != null && files.length() > 0) { 

          for (int j = 0; j < files.length(); j++) { 
           JSONObject Jsonfilename = files.getJSONObject(j); 
           filename = Jsonfilename.getString("file_name"); 
           filename = "www.sample.com/home_webservice/compress/"+filename; 
           Log.e("Files", "" + filename); 
          } 
         } 

         Data_SAerver item = new Data_SAerver(fName,created_at,post_desc,filename); 
         data_sAervers.add(item); 
        } 
       } 

      adapter = new MyAdapter(data_sAervers,getApplicationContext()); 

       recyclerView.setAdapter(adapter); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      progressDialog.dismiss(); 

      Toast.makeText(getApplicationContext(), error.getMessage(),Toast.LENGTH_LONG).show(); 

     } 
    }); 

    RequestQueue requestQueue = Volley.newRequestQueue(this); 
    requestQueue.add(stringRequest); 
} 
} 

数据服务器类代码

public class Data_SAerver { 

    private String firstname; 
    private String created_at; 
    private String post_desc; 


    private String filepath; /*= "http://cleanvadodara.esy.es/images/";*/ 

    public Data_SAerver(String firstname, String created_at, String post_desc,String filepath) 
    { this.firstname = firstname; 
     this.created_at = created_at; 
     this.post_desc = post_desc; 
     this.filepath = filepath; 
    } 
    public String getfilepath() 
    { return filepath; } 

    public Data_SAerver(String firstname, String created_at, String post_desc) { 
     this.firstname = firstname; 
     this.created_at = created_at; 
     this.post_desc = post_desc; 

    } 


    public String getFirstname() { 
     return firstname; 
    } 

    public String getCreated_at() { 
     return created_at; 
    } 

    public String getPost_desc() { 
     return post_desc; 
    } 


} 

MY适配器类

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { 
    private List<Data_SAerver> data_sAervers; 

    private Context context; 
    public MyAdapter(List<Data_SAerver> data_sAervers, Context context) { 
     this.data_sAervers = data_sAervers; 
     this.context = context; 
    } 
    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.sample, parent, false); 
     return new ViewHolder(v); 
    } 
    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 

     Data_SAerver data_sAerver = data_sAervers.get(position); 
     holder.firstname.setText(data_sAerver.getFirstname()); 
     holder.created_at.setText(data_sAerver.getCreated_at()); 
     holder.post_desc.setText(data_sAerver.getPost_desc()); 
     Picasso.with(context).load(data_sAerver.getfilepath()).resize(736,1128).onlyScaleDown().into(holder.files); 
    } 


    @Override 
    public int getItemCount() { 
     return data_sAervers.size(); 
    } 
    public class ViewHolder extends RecyclerView.ViewHolder{ 

     public TextView firstname; 
     public TextView created_at; 
     public TextView post_desc; 
     public ImageView files; 
     boolean isImageFitToScreen; 

     public ViewHolder(View itemView) { 
      super(itemView); 

      firstname = (TextView) itemView.findViewById(R.id.firstname); 
      created_at = (TextView) itemView.findViewById(R.id.created_at); 
      post_desc= (TextView)itemView.findViewById(R.id.post_desc); 
      files = (ImageView) itemView.findViewById(R.id.image_files); 


     // ImageViewPopUpHelper.enablePopUpOnClick(ViewHolder.this, files); 

      /* files.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        if(isImageFitToScreen) { 
         isImageFitToScreen=true; 
         files.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
         files.setAdjustViewBounds(true); 
        }else{ 
         isImageFitToScreen=false; 
         files.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
         files.setScaleType(ImageView.ScaleType.FIT_XY); 
        } 
       } 
      });*/ 

     } 
    } 

} 

请帮忙解决这个错误。谢谢。

+0

添加错误报告。使其易于帮助。 – RameshJaga

+0

你的问题不清楚。您发送的代码是简单的JSON解析。请将您的错误日志和代码发送给您发生此错误的地方。 –

+0

actully我为所有用户获取相同的图像,所以我只想要那些发布图像使用POST_ID @Target –

回答

1

您的Android代码

String filename = ""; 
    String filetype = ""; 
      try { 
       JSONObject jsonObject = new JSONObject(s); 
       JSONArray posts = jsonObject.getJSONArray("posts"); 
       if(posts != null && posts.length() > 0) { 
        for (int i = 0; i < posts.length(); i++) { 
         JSONObject fileObj = posts.getJSONObject(i); 

         String fName = fileObj.getString("firstname"); 
         String created_at = fileObj.getString("created_at"); 
         String post_desc = fileObj.getString("post_desc"); 


         Log.e("Details", fName + "" + created_at + "" + post_desc); 

         JSONArray files = fileObj.getJSONArray("files"); 
         if(files != null && files.length() > 0) 
         { 

          for (int j = 0; j < files.length(); j++) 
          { 
           JSONObject Jsonfilename = files.getJSONObject(j); 
           filename = Jsonfilename.getString("file_name"); 
           filetype=jsonfilename.getString("file_type") 
           filename = "http://uat.ziplife.in/img/Post/compress/"+filename; 
           Log.e("Files", "" + filename); 
          } 
         } 
         else 
         { 
          filname=""; 
          filetype=""; 
         } 

         Data_SAerver item = new Data_SAerver(fName,created_at,post_desc,filename,file_type); 
         data_sAervers.add(item); 
        } 
       } 

      adapter = new MyAdapter(data_sAervers,getApplicationContext()); 

       recyclerView.setAdapter(adapter); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      progressDialog.dismiss(); 

      Toast.makeText(getApplicationContext(), error.getMessage(),Toast.LENGTH_LONG).show(); 

     } 
    }); 

    RequestQueue requestQueue = Volley.newRequestQueue(this); 
    requestQueue.add(stringRequest); 
} 
} 

模型

public class Data_SAerver { 

    private String firstname; 
    private String created_at; 
    private String post_desc; 


    private String filepath; 
    private String filetype;/*= "http://cleanvadodara.esy.es/images/";*/ 

    public Data_SAerver(String firstname, String created_at, String post_desc,String filepath,String filetype) 
    { this.firstname = firstname; 
     this.created_at = created_at; 
     this.post_desc = post_desc; 
     this.filepath = filepath; 
     this.filetype = filetype; 
    } 
    public String getfilepath() 
    { return filepath; } 

    public Data_SAerver(String firstname, String created_at, String post_desc) { 
     this.firstname = firstname; 
     this.created_at = created_at; 
     this.post_desc = post_desc; 

    } 


    public String getFirstname() { 
     return firstname; 
    } 

    public String getCreated_at() { 
     return created_at; 
    } 

    public String getPost_desc() { 
     return post_desc; 
    } 
    public String getFiletype() { 
     return filetype; 
    } 



} 

你我的适配器

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { 
    private List<Data_SAerver> data_sAervers; 

    private Context context; 
    public MyAdapter(List<Data_SAerver> data_sAervers, Context context) { 
     this.data_sAervers = data_sAervers; 
     this.context = context; 
    } 
    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.sample, parent, false); 
     return new ViewHolder(v,context); 
    } 
    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) 
    { 

     Data_SAerver data_sAerver = data_sAervers.get(position); 
     holder.firstname.setText(data_sAerver.getFirstname()); 
     holder.created_at.setText(data_sAerver.getCreated_at()); 
     holder.post_desc.setText(data_sAerver.getPost_desc()); 

     holder.filepathurl.setText(data_sAerver.getfilepath()); 
     if(data_sAerver.getFiletype().equals("1")) 
     { 
      holder.files.setVisibility(View.VISIBLE); 
      Picasso.with(context).load(data_sAerver.getfilepath()).resize(736,1128).onlyScaleDown().into(holder.files); 
      holder.playvideo.setVisibility(View.GONE); 
     } 
     else 
     { 
      if(data_sAerver.getFiletype().equals("2")) 
      { 
       holder.playvideo.setVisibility(View.VISIBLE); 
       holder.files.setVisibility(View.GONE); 
      } 

     } 
    } 


    @Override 
    public int getItemCount() { 
     return data_sAervers.size(); 
    } 
    public class ViewHolder extends RecyclerView.ViewHolder 
    { 

     public TextView firstname; 
     public TextView created_at; 
     public TextView post_desc; 
     public ImageView files; 
     boolean isImageFitToScreen; 
     public TextView filepathurl; 
     Public Textview playvideo; 
     Context con; 

     public ViewHolder(View itemView,Context context) 
     { 
      super(itemView); 
      filepathurl = (TextView) itemView.findViewById(R.id.filepathurl); 
      filepathurl.setVisibility(View.GONE); 
      playvideo = (TextView) itemView.findViewById(R.id.playvideo); 
      firstname = (TextView) itemView.findViewById(R.id.firstname); 
      created_at = (TextView) itemView.findViewById(R.id.created_at); 
      post_desc= (TextView)itemView.findViewById(R.id.post_desc); 
      files = (ImageView) itemView.findViewById(R.id.image_files); 
      con=context; 

     // ImageViewPopUpHelper.enablePopUpOnClick(ViewHolder.this, files); 

      /* files.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        if(isImageFitToScreen) { 
         isImageFitToScreen=true; 
         files.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
         files.setAdjustViewBounds(true); 
        }else{ 
         isImageFitToScreen=false; 
         files.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
         files.setScaleType(ImageView.ScaleType.FIT_XY); 
        } 
       } 
      });*/ 
      playvideo.setOnClickListener(new View.OnClickListener() 
      { 
       @Override 
       public void onClick(View v) 
       { 
        Intent play=new Intent(con,PLayVideo.java); 
        play.putExtra("url",filepathurl.getText().toString()); 
        startActivity(play); 
       } 
      } 
     } 
    } 

} 

在示例XML增加两个领域的一个filepathurl TextView的,第二增加一个TextView的与文本Playvideo和setvisibility去和去设置文件ImageView的可见性。

你的playVideo活动

public class PlayVideo extends Activity 
    { 

     // Declare variables 
     ProgressDialog pDialog; 
     VideoView videoview; 


     String VideoURL = ""; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      // Get the layout from video_main.xml 
      setContentView(R.layout.video_main); 

      VideoURL=getIntent().getStringExtra("url"); 
      // Find your VideoView in your video_main.xml layout 
      videoview = (VideoView) findViewById(R.id.VideoView); 
      // Execute StreamVideo AsyncTask 

      // Create a progressbar 
      pDialog = new ProgressDialog(PlayVideo.this); 
      // Set progressbar title 
      pDialog.setTitle("Android Video Streaming Tutorial"); 
      // Set progressbar message 
      pDialog.setMessage("Buffering..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(false); 
      // Show progressbar 
      pDialog.show(); 

      try 
      { 
       // Start the MediaController 
       MediaController mediacontroller = new MediaController(
         PlayVideo.this); 
       mediacontroller.setAnchorView(videoview); 
       // Get the URL from String VideoURL 
       Log.i("VideoURL",""+VideoURL); 
       Uri video = Uri.parse(VideoURL); 
       videoview.setMediaController(mediacontroller); 
       videoview.setVideoURI(video); 

      } 
      catch (Exception e) 
      { 
       Log.e("Error", e.getMessage()); 
       e.printStackTrace(); 
      } 

      videoview.requestFocus(); 
      videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
       // Close the progress bar and play the video 
       public void onPrepared(MediaPlayer mp) { 
        pDialog.dismiss(); 
        videoview.start(); 
       } 
      }); 

     } 

} 

你VideoView XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <VideoView 
     android:id="@+id/VideoView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" /> 

</RelativeLayout> 
+2

评论是不适用于扩展的讨论;这个对话已经[转移到聊天](http://chat.stackoverflow.com/rooms/147652/discussion-on-answer-by-magenative-how-to-get-values-from-a-particular-id-使用)。 – Matt

0

这是我的活动代码

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:cardview="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal" 
    android:padding="5dp"> 




    <LinearLayout 
     android:layout_width="match_parent" 
     android:orientation="vertical" 
     android:layout_height="wrap_content"> 



      <android.support.v7.widget.CardView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:padding="10dp"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal"> 

       <ImageView 
        android:id="@+id/iconone" 
        android:layout_width="50dp" 
        android:layout_height="50dp" 
        android:layout_marginBottom="5dp" 
        android:layout_marginLeft="5dp" 
        android:src="@drawable/logo" /> 

       <LinearLayout 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" 
        android:orientation="vertical"> 


        <TextView 
         android:id="@+id/firstname" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_marginLeft="5dp" 
         android:gravity="start" 
         android:text="Karan Brahmaxatriya" 
         android:textAppearance="?android:attr/textAppearanceMedium" 
         android:textColor="#2196fe" /> 


        <TextView 

         android:id="@+id/created_at" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_marginLeft="5dp" 
         android:gravity="start" 
         android:text="23/05/2017 6:05 PM" 
         android:textAppearance="?android:attr/textAppearanceMedium" 
         android:textColor="#333333" 
         android:textSize="16sp" /> 


       </LinearLayout> 

      </LinearLayout> 


      <ImageView 
       android:id="@+id/image_files" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:adjustViewBounds="true" 
       /> 
      <TextView 
       android:id="@+id/filepathurl" 
       android:visibility="gone" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="filepathurl"/> 
      <TextView 
       android:id="@+id/playvideo" 
       android:visibility="gone" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="playvideo"/> 
      <VideoView 
       android:id="@+id/videoView" 
       android:visibility="gone" 
       android:layout_width="fill_parent" 
       android:layout_height="190dp" /> 
      <TextView 
       android:id="@+id/post_desc" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="start" 
       android:layout_marginLeft="5dp" 
       android:text="ccvvzv" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:textColor="#333333" /> 


      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1"> 

       <Button 
        android:id="@+id/comment_btn" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text="Comment" /> 

       <Button 
        android:id="@+id/repost_btn" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text="Repost" /> 

       <Button 
        android:id="@+id/like_btn" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text="Like" /> 
      </LinearLayout> 
     </LinearLayout> 


    </android.support.v7.widget.CardView> 
</LinearLayout> 
</LinearLayout> 
相关问题