Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • My listview not returning me to my DetailActivity.class when I'm clicking an item on listview

    • 0
    • 0
    • 0
    • 2
    • 0
    • 0
    • 0
    • 420
    Answer it

    I am having trouble with my ListView. It always sends me back to my login page whenever I press an item on the list. It supposed to go to the details page. 

    Home Activity

    package com.example.kun.carcarkila;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ImageView;
    import android.widget.ListView;
    
    import com.amigold.fundapter.BindDictionary;
    import com.amigold.fundapter.FunDapter;
    import com.amigold.fundapter.extractors.StringExtractor;
    import com.amigold.fundapter.interfaces.DynamicImageLoader;
    import com.kosalgeek.android.json.JsonConverter;
    import com.kosalgeek.genasync12.AsyncResponse;
    import com.kosalgeek.genasync12.PostResponseAsyncTask;
    import com.nostra13.universalimageloader.core.ImageLoader;
    
    import java.io.Serializable;
    import java.util.ArrayList;
    
    public class HomeActivity extends AppCompatActivity implements AsyncResponse, AdapterView.OnItemClickListener {
    final String LOG = "HomeActivity";
    
    private ArrayList<Product> productList;
    private ListView lvProduct;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    
    
    
        ImageLoader.getInstance().init(UILConfig.config(HomeActivity.this)); // Do it on Application start
    
        PostResponseAsyncTask taskRead = new PostResponseAsyncTask(HomeActivity.this, this);
        taskRead.execute("http://10.0.2.2/carkila/product.php");
    
    
    }
    
    @Override
    public void processFinish(String s) {
    
                productList = new JsonConverter<Product>().toArrayList(s, Product.class);
    
        BindDictionary<Product> dict = new BindDictionary<Product>();
    
        dict.addDynamicImageField(R.id.ivImg, new StringExtractor<Product>() {
            @Override
            public String getStringValue(Product item, int position) {
                return item.img;
            }
        }, new DynamicImageLoader() {
            @Override
            public void loadImage(String url, ImageView imageView) {
    
                ImageLoader.getInstance().displayImage(url, imageView); // Default options will be used
            }
        });
        dict.addStringField(R.id.tvCarModel, new StringExtractor<Product>() {
            @Override
            public String getStringValue(Product item, int position) {
                return "Car Model: " + item.Car_Model;
            }
        });
        dict.addStringField(R.id.tvCarType, new StringExtractor<Product>() {
            @Override
            public String getStringValue(Product item, int position) {
                return "Car Type: " +  item.Car_Type;
            }
        });
        dict.addStringField(R.id.tvCapacity, new StringExtractor<Product>() {
            @Override
            public String getStringValue(Product item, int position) {
                return "Capacity: " + item.Capacity;
            }
        });
    
        FunDapter<Product> adapter = new FunDapter<>(
                HomeActivity.this, productList, R.layout.layout_list, dict);
        lvProduct = (ListView)findViewById(R.id.lvProduct);
        lvProduct.setAdapter(adapter);
        lvProduct.setOnItemClickListener(this);
    }
    
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Product selectedProduct = productList.get(position);
        Intent in = new Intent(HomeActivity.this, DetailActivity.class);
        in.putExtra("product", (Serializable) selectedProduct);
        startActivity(in);
    }
    }

     

    Detail Activity

    package com.example.kun.carcarkila;
    
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.View;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import com.nostra13.universalimageloader.core.ImageLoader;
    
    public class DetailActivity extends AppCompatActivity {
    TextView tvCarModel, tvCarType, tvCapacity;
    ImageView ivImage;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        ImageLoader.getInstance().init(UILConfig.config(DetailActivity.this));
    
        Product product = (Product) getIntent().getSerializableExtra("product");
    
        tvCarModel = (TextView)findViewById(R.id.tvCarModel);
        tvCarType = (TextView)findViewById(R.id.tvCarType);
        tvCapacity = (TextView)findViewById(R.id.tvCapacity);
        ivImage = (ImageView)findViewById(R.id.ivImage);
    
        if(product != null){
            tvCarModel.setText(product.Car_Model);
            tvCarType.setText(product.Car_Type);
            tvCapacity.setText(product.Capacity);
            ImageLoader.getInstance().displayImage(product.img, ivImage);
        }
    }
    
    }

     

    Product Class

    package com.example.kun.carcarkila;
    
    import com.google.gson.annotations.SerializedName;
    
    public class Product {
    
    @SerializedName("Car_No")
    public int Car_No;
    @SerializedName("Car_Model")
    public String Car_Model;
    @SerializedName("Car_Type")
    public String Car_Type;
    @SerializedName("Capacity")
    public int Capacity;
    @SerializedName("img")
    public String img;
    }

    Error is java.lang.ClassCastException: com.example.kun.carcarkila.Product cannot be cast to java.io.Serializable

 2 Answer(s)

  • Your current activity is crashed because your Product class is not Serializable so thats why last activity i.e Login Activity appear.

    You need to implement Serializable interface on Product class.

    first line of you Product class will look like.

    public class Product implements Serializable 
    
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: