主要活动.java
spring xml
ProductAdapter.java
ProgressDialog pd;
public static final String ROOT_URL="http://www.tontosworld.com/ws/";
public static final String PODAY_URL="poday-json.php";
public static final String PRODUCTS_DOODLES_URL="products-doodles-json.php";
public static final String PRODUCTS_GREETING_CARDS_URL="products-greetingcards-json.php";
public static final String PRODUCTS_HANDICRAFTS_URL="products-handicrafts-json.php";
public static final String PRODUCTS_MODERNART_URL="products-modernart-json.php";
public static final String PRODUCTS_OTHERS_URL="products-others-json.php";
public static final String PRODUCTS_PAINTINGS_URL="products-paintings-json.php";
public static final String PRODUCTS_PHOTOGRAPHS_URL="products-photographs-json.php";
public static final String PRODUCTS_POSTERS_URL="products-posters-json.php";
public static final String PRODUCTS_SKETCHES_URL="products-sketches-json.php";
public static final String PODAY_ID="products.id";
public static final String PODAY_PRIMARY_IMAGE="products.p_img";
public static final String PODAY_PNAME="products.p_name";
public static final String PODAY_PRICE="products.price";
public static final String PODAY_OFFPRICE="products.off_price";
public static final String JSON_ARRAY="result";
public static final String PRODUCT_ID="id";
public static final String PRODUCT_NAME="p_name";
public static final String PRODUCT_DESC="desc";
public static final String PRODUCT_ARTIST_ID="artist_id";
public static final String PRODUCT_PRICE="price";
public static final String PRODUCT_CATEGORY="category";
public static final String PRODUCT_CITY1="city1";
public static final String PRODUCT_CITY2="city2";
public static final String PRODUCT_IMG="p_img";
public static final String PRODUCT_S1_IMG="s1_img";
public static final String PRODUCT_S2_IMG="s2_img";
public JSONArray _poday,_doodles,_greetingCards,_handicrafts,_modernArt,_others,_paintings,_photographs,_posters,_sketches;
String poday,doodles,greetingCards,handicrafts,modernArt,others,paintings,photographs,posters,sketches;
ArrayList<String> parsedPODAY_ID=new ArrayList<>();
ArrayList<String> parsedPODAY_P_IMG=new ArrayList<>();
ArrayList<String> parsedPODAY_NAME=new ArrayList<>();
ArrayList<String> parsedPODAY_PRICE=new ArrayList<>();
ArrayList<String> parsedPODAY_OFFPRICE=new ArrayList<>();
int i;
Bitmap bitmap;
RecyclerView rvPoday;
ProductAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
NavigationDrawerFragment drawerFragment=(NavigationDrawerFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
pd=ProgressDialog.show(MainActivity.this, "Please Wait...", null, true, true);
callPoday(PODAY_URL);
}
public List<Product> getDataPoday(){
List<Product> data=new ArrayList<>();
for(i=0;i<parsedPODAY_ID.size();i++){
Product current=new Product();
current.productName=parsedPODAY_NAME.get(i);
current.price=parsedPODAY_OFFPRICE.get(i);
current.pImage=getImage("http://www.tontosworld.com/img/productimages/" + parsedPODAY_P_IMG.get(i));
data.add(current);
}
return data;
}
public Bitmap getImage(String s) {
LoadImageTask task=new LoadImageTask();
task.execute(s);
return bitmap;
}
private void callPoday(String podayUrl) {
CallJsonPodayTask task=new CallJsonPodayTask();
task.execute(ROOT_URL + "" + podayUrl);
}
class LoadImageTask extends AsyncTask<String,String,Bitmap>{
@Override
protected Bitmap doInBackground(String... strings) {
try {
bitmap= BitmapFactory.decodeStream((InputStream) new URL(strings[0]).getContent());
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if(bitmap==null){
Toast.makeText(MainActivity.this,"Error in loading image",Toast.LENGTH_LONG).show();
}
}
}
class CallJsonPodayTask extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... strings) {
String uri = strings[0];
BufferedReader bufferedReader = null;
try {
URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String json;
while ((json = bufferedReader.readLine()) != null) {
sb.append(json + "\n");
}
return sb.toString().trim();
} catch (Exception e) {
return null;
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
poday=s;
parsePoday(poday);
}
}
private void parsePoday(String poday) {
_poday=null;
try {
JSONObject jsonObject = new JSONObject(poday);
_poday = jsonObject.getJSONArray(JSON_ARRAY);
} catch (JSONException e) {
e.printStackTrace();
}
try {
for (int i = 0; i < _poday.length(); i++) {
JSONObject jj = _poday.getJSONObject(i);
parsedPODAY_ID.add(jj.getString(PODAY_ID));
parsedPODAY_NAME.add(jj.getString(PODAY_PNAME));
parsedPODAY_P_IMG.add(jj.getString(PODAY_PRIMARY_IMAGE));
parsedPODAY_PRICE.add(jj.getString(PODAY_PRICE));
parsedPODAY_OFFPRICE.add(jj.getString(PODAY_OFFPRICE));
//Toast.makeText(this, "ID:" + parsedPODAY_ID.get(i) + "\nNAME:" + parsedPODAY_NAME.get(i) + "\nPIMG:" + parsedPODAY_P_IMG.get(i) + "\nPRICE" + parsedPODAY_PRICE.get(i) + "\nOFFPRICE" + parsedPODAY_OFFPRICE.get(i), Toast.LENGTH_LONG).show();
}
}
catch (JSONException e) {
e.printStackTrace();
Toast.makeText(this,"Error",Toast.LENGTH_LONG).show();
}
finally {
rvPoday=(RecyclerView)findViewById(R.id.rvProductOfTheDay);
adapter=new ProductAdapter(MainActivity.this,getDataPoday());
Toast.makeText(this,""+adapter.getItemCount(),Toast.LENGTH_LONG).show();
rvPoday.setAdapter(adapter);
LinearLayoutManager llm=new LinearLayoutManager(MainActivity.this);
llm.setOrientation(LinearLayoutManager.HORIZONTAL);
rvPoday.setLayoutManager(llm);
}
}
recycler_view_single.xml
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.MyViewHolder> {
public LayoutInflater inflater;
List<Product> data= Collections.emptyList();
public ProductAdapter(Context context,List<Product> data){
inflater=LayoutInflater.from(context);
this.data=data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v=inflater.inflate(R.layout.recycler_view_single,parent,false);
MyViewHolder holder=new MyViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Product current=data.get(position);
holder.name.setText(current.productName);
holder.price.setText(current.price);
holder.image.setImageBitmap(current.pImage);
}
@Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView name,price;
ImageView image;
public MyViewHolder(View itemView) {
super(itemView);
name=(TextView)itemView.findViewById(R.id.rvItemName);
price=(TextView)itemView.findViewById(R.id.rvItemPrice);
image=(ImageView)itemView.findViewById(R.id.rvItemImage);
}
}
}
Product.java
public class Product {
String productName,price;
Bitmap pImage;
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="?android:selectableItemBackground">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/rvItemImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/rvItemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
<TextView
android:id="@+id/rvItemPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
我从json调用了数据,在解析数据之后我附加了回收器视图。但它没有显示回收器视图。
我在最后一个parsePoday()方法块中附加了回收站视图
答案 0 :(得分:0)
您将recycleviewview放在您的activity_main.xml中的scrollview中,因此请从scrollview中取出recyclerview,或者只是删除scrollview并再试一次
答案 1 :(得分:0)
parsePoday()
方法仅在AsyncTask完成时调用,因此当AsyncTask运行时,RecyclerView仍然没有连接适配器。