Skip to main content
adicionou 6 caracteres ao conteúdo; tags editadas
Fonte Link
viana
  • 27,7mil
  • 26
  • 96
  • 198

Opa galera, estou puxando uma lista do servidor e populando um recyclerViewRecyclerView com essa lista. Porém quando a lista estiver vazia, quero que apareça uma mensagem do tipo: "Ainda não existem itens cadastrados para esse produto". Mas não um ToastToast, e sim uma mensagem fixa num TextViewTextView. Alguém pode me ajudar?

Opa galera, estou puxando uma lista do servidor e populando um recyclerView com essa lista. Porém quando a lista estiver vazia, quero que apareça uma mensagem do tipo: "Ainda não existem itens cadastrados para esse produto". Mas não um Toast, e sim uma mensagem fixa num TextView. Alguém pode me ajudar?

Opa galera, estou puxando uma lista do servidor e populando um RecyclerView com essa lista. Porém quando a lista estiver vazia, quero que apareça uma mensagem do tipo: "Ainda não existem itens cadastrados para esse produto". Mas não um Toast, e sim uma mensagem fixa num TextView. Alguém pode me ajudar?

correção código
Fonte Link
public class ProductAdminActivity extends AppCompatActivity {

public static final String APP_NAME = "PanApp";
public static final String URL = "https://panapp-backend.appspot.com/_ah/api";
private RecyclerView rvProductAdmin;
private Button btnNewProduct;
private Long bakeryId;
private Bundle params;
private TextView txtAnswer;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //getLayoutInflater().inflate(R.layout.activity_product_admin, frameLayout);
    setContentView(R.layout.activity_product_admin);
    setTitle("Meus Produtos");

    Intent it = getIntent();
    params = it.getExtras();
    if (params != null) {
        bakeryId = params.getLong("bakeryId");
    } 

    txtAnswer = (TextView) findViewById(R.id.textAnswer);
    rvProductAdmin = (RecyclerView) findViewById(R.id.product_list_admin);
    btnNewProduct = (Button) findViewById(R.id.btn_new_product);
    btnNewProduct.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            params.putLong("bakeryId", bakeryId);
            Intent intentFormProduct = new Intent(ProductAdminActivity.this, FormProductActivity.class);
            intentFormProduct.putExtras(params);
            startActivity(intentFormProduct);
        }
    });
    new ProductListAsyncTask(this).execute();
}

private class ProductListAsyncTask extends AsyncTask<Void, Void, CollectionResponseProduct> {
    ProductAdminActivity context;
    private ProgressDialog pd;

    public ProductListAsyncTask(ProductAdminActivity context) {
        this.context = context;
    }

    protected void onPreExecute(){
        super.onPreExecute();
        pd = new ProgressDialog(context);
        pd.setMessage("Listando Produtos...");
        pd.show();
    }

    protected CollectionResponseProduct doInBackground(Void... unused) {
        CollectionResponseProduct collectionResponseProduct = null;
        try {
            ProductApi.Builder builder = new ProductApi.Builder(AndroidHttp.newCompatibleTransport(),
                    new AndroidJsonFactory(), null).setRootUrl(URL);
            builder.setApplicationName(APP_NAME);
            ProductApi service =  builder.build();
            collectionResponseProduct = service.list().execute();
        } catch (Exception e) {
            Log.d("Erro", e.getMessage(), e);
        }
        return collectionResponseProduct;
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    protected void onPostExecute(CollectionResponseProduct collectionResponseProduct) {
        pd.dismiss();
        if(collectionResponseProduct.size() > 0){
            List<Product> list = new ArrayList<>();
            List<Product> _list = collectionResponseProduct.getItems();
            for (Product productAux : _list) {
                Product product = new Product();
                product.setProductName(productAux.getProductName());
                product.setProductPrice(productAux.getProductPrice());
                product.setType(productAux.getType());
                product.setBakeryId(productAux.getBakeryId());
                product.setProductImage(productAux.getProductImage());
                if(Objects.equals(bakeryId, product.getBakeryId())){
                    list.add(product);
                }
            }
            rvProductAdmin.setLayoutManager(new LinearLayoutManager(ProductAdminActivity.this));
            rvProductAdmin.setAdapter(new ProductAdapter(list));
        } else {
            ToasttxtAnswer.makeTextsetText(ProductAdminActivity.this, "Não existem produtos cadastrados!", Toast.LENGTH_SHORT).show();
        }
    }
}
public class ProductAdminActivity extends AppCompatActivity {

public static final String APP_NAME = "PanApp";
public static final String URL = "https://panapp-backend.appspot.com/_ah/api";
private RecyclerView rvProductAdmin;
private Button btnNewProduct;
private Long bakeryId;
private Bundle params;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //getLayoutInflater().inflate(R.layout.activity_product_admin, frameLayout);
    setContentView(R.layout.activity_product_admin);
    setTitle("Meus Produtos");

    Intent it = getIntent();
    params = it.getExtras();
    if (params != null) {
        bakeryId = params.getLong("bakeryId");
    }

    rvProductAdmin = (RecyclerView) findViewById(R.id.product_list_admin);
    btnNewProduct = (Button) findViewById(R.id.btn_new_product);
    btnNewProduct.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            params.putLong("bakeryId", bakeryId);
            Intent intentFormProduct = new Intent(ProductAdminActivity.this, FormProductActivity.class);
            intentFormProduct.putExtras(params);
            startActivity(intentFormProduct);
        }
    });
    new ProductListAsyncTask(this).execute();
}

private class ProductListAsyncTask extends AsyncTask<Void, Void, CollectionResponseProduct> {
    ProductAdminActivity context;
    private ProgressDialog pd;

    public ProductListAsyncTask(ProductAdminActivity context) {
        this.context = context;
    }

    protected void onPreExecute(){
        super.onPreExecute();
        pd = new ProgressDialog(context);
        pd.setMessage("Listando Produtos...");
        pd.show();
    }

    protected CollectionResponseProduct doInBackground(Void... unused) {
        CollectionResponseProduct collectionResponseProduct = null;
        try {
            ProductApi.Builder builder = new ProductApi.Builder(AndroidHttp.newCompatibleTransport(),
                    new AndroidJsonFactory(), null).setRootUrl(URL);
            builder.setApplicationName(APP_NAME);
            ProductApi service =  builder.build();
            collectionResponseProduct = service.list().execute();
        } catch (Exception e) {
            Log.d("Erro", e.getMessage(), e);
        }
        return collectionResponseProduct;
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    protected void onPostExecute(CollectionResponseProduct collectionResponseProduct) {
        pd.dismiss();
        if(collectionResponseProduct.size() > 0){
            List<Product> list = new ArrayList<>();
            List<Product> _list = collectionResponseProduct.getItems();
            for (Product productAux : _list) {
                Product product = new Product();
                product.setProductName(productAux.getProductName());
                product.setProductPrice(productAux.getProductPrice());
                product.setType(productAux.getType());
                product.setBakeryId(productAux.getBakeryId());
                product.setProductImage(productAux.getProductImage());
                if(Objects.equals(bakeryId, product.getBakeryId())){
                    list.add(product);
                }
            }
            rvProductAdmin.setLayoutManager(new LinearLayoutManager(ProductAdminActivity.this));
            rvProductAdmin.setAdapter(new ProductAdapter(list));
        } else {
            Toast.makeText(ProductAdminActivity.this, "Não existem produtos cadastrados!", Toast.LENGTH_SHORT).show();
        }
    }
}
public class ProductAdminActivity extends AppCompatActivity {

public static final String APP_NAME = "PanApp";
public static final String URL = "https://panapp-backend.appspot.com/_ah/api";
private RecyclerView rvProductAdmin;
private Button btnNewProduct;
private Long bakeryId;
private Bundle params;
private TextView txtAnswer;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //getLayoutInflater().inflate(R.layout.activity_product_admin, frameLayout);
    setContentView(R.layout.activity_product_admin);
    setTitle("Meus Produtos");

    Intent it = getIntent();
    params = it.getExtras();
    if (params != null) {
        bakeryId = params.getLong("bakeryId");
    } 

    txtAnswer = (TextView) findViewById(R.id.textAnswer);
    rvProductAdmin = (RecyclerView) findViewById(R.id.product_list_admin);
    btnNewProduct = (Button) findViewById(R.id.btn_new_product);
    btnNewProduct.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            params.putLong("bakeryId", bakeryId);
            Intent intentFormProduct = new Intent(ProductAdminActivity.this, FormProductActivity.class);
            intentFormProduct.putExtras(params);
            startActivity(intentFormProduct);
        }
    });
    new ProductListAsyncTask(this).execute();
}

private class ProductListAsyncTask extends AsyncTask<Void, Void, CollectionResponseProduct> {
    ProductAdminActivity context;
    private ProgressDialog pd;

    public ProductListAsyncTask(ProductAdminActivity context) {
        this.context = context;
    }

    protected void onPreExecute(){
        super.onPreExecute();
        pd = new ProgressDialog(context);
        pd.setMessage("Listando Produtos...");
        pd.show();
    }

    protected CollectionResponseProduct doInBackground(Void... unused) {
        CollectionResponseProduct collectionResponseProduct = null;
        try {
            ProductApi.Builder builder = new ProductApi.Builder(AndroidHttp.newCompatibleTransport(),
                    new AndroidJsonFactory(), null).setRootUrl(URL);
            builder.setApplicationName(APP_NAME);
            ProductApi service =  builder.build();
            collectionResponseProduct = service.list().execute();
        } catch (Exception e) {
            Log.d("Erro", e.getMessage(), e);
        }
        return collectionResponseProduct;
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    protected void onPostExecute(CollectionResponseProduct collectionResponseProduct) {
        pd.dismiss();
        if(collectionResponseProduct.size() > 0){
            List<Product> list = new ArrayList<>();
            List<Product> _list = collectionResponseProduct.getItems();
            for (Product productAux : _list) {
                Product product = new Product();
                product.setProductName(productAux.getProductName());
                product.setProductPrice(productAux.getProductPrice());
                product.setType(productAux.getType());
                product.setBakeryId(productAux.getBakeryId());
                product.setProductImage(productAux.getProductImage());
                if(Objects.equals(bakeryId, product.getBakeryId())){
                    list.add(product);
                }
            }
            rvProductAdmin.setLayoutManager(new LinearLayoutManager(ProductAdminActivity.this));
            rvProductAdmin.setAdapter(new ProductAdapter(list));
        } else {
            txtAnswer.setText("Não existem produtos cadastrados!");
        }
    }
}
adição exception
Fonte Link

Exception:

FATAL EXCEPTION: main
                                                                  Process: com.gregmachado.panapp, PID: 8730
                                                                  java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference
                                                                      at com.gregmachado.panapp.activity.ProductAdminActivity$ProductListAsyncTask.onPostExecute(ProductAdminActivity.java:106)
                                                                      at com.gregmachado.panapp.activity.ProductAdminActivity$ProductListAsyncTask.onPostExecute(ProductAdminActivity.java:70)
                                                                      at android.os.AsyncTask.finish(AsyncTask.java:651)
                                                                      at android.os.AsyncTask.-wrap1(AsyncTask.java)
                                                                      at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                      at android.os.Looper.loop(Looper.java:148)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

Exception:

FATAL EXCEPTION: main
                                                                  Process: com.gregmachado.panapp, PID: 8730
                                                                  java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference
                                                                      at com.gregmachado.panapp.activity.ProductAdminActivity$ProductListAsyncTask.onPostExecute(ProductAdminActivity.java:106)
                                                                      at com.gregmachado.panapp.activity.ProductAdminActivity$ProductListAsyncTask.onPostExecute(ProductAdminActivity.java:70)
                                                                      at android.os.AsyncTask.finish(AsyncTask.java:651)
                                                                      at android.os.AsyncTask.-wrap1(AsyncTask.java)
                                                                      at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                      at android.os.Looper.loop(Looper.java:148)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
adicionou 5452 caracteres ao conteúdo
Fonte Link
Carregando
Fonte Link
Carregando