简体   繁体   中英

How to get item details from Listview on another activity

I have tried many methods but still not working. almost used all methods from stackoverflow's answers. but not working.

MainActivity.java

mProductList = new ArrayList<>();
        //add sample data to list

        mProductList.add(new Product(1, "iPhone4", 200, "Apple1"));
        mProductList.add(new Product(2, "iPhone5", 2100, "Apple2"));
        mProductList.add(new Product(3, "iPhone6", 2020, "Apple3"));
        mProductList.add(new Product(4, "iPhone7", 2400, "Apple4"));
        mProductList.add(new Product(5, "iPhone8", 2050, "Apple5"));
        mProductList.add(new Product(6, "iPhone9", 7200, "Apple6"));

        //init adapter
        adapter = new ProductListAdapter(getApplicationContext(), mProductList);
        lvProduct.setAdapter(adapter);


        lvProduct.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getApplicationContext(), "Clicked product id =" + view.getTag(), Toast.LENGTH_SHORT).show();

                Product item = (Product)lvProduct.getAdapter().getItem(position);
                Log.d("abc",item.getName());

                Intent launchActivity1= new Intent(MainActivity.this,Show.class);
                launchActivity1.putExtra("item", item.getName());
                startActivity(launchActivity1);
            }
        });

and Show.java

public class Show extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show);
    }
    Intent launchActivity1 = getIntent();
    String item = launchActivity1.getStringExtra("item"); // this is line 17
}

when i click on one item it shows item name in Log with "getName()" function. but at the same time app crashes and show error in Show.java on line 17

please help me to solve this code.

this error is showing in Log

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Serializable android.content.Intent.getSerializableExtra(java.lang.String)' on a null object reference
                                                                               at com.example.sohaib.mydesign.Show.<init>(Show.java:17)

Your code is outside onCreate method!

Intent launchActivity1 = getIntent();
String item = launchActivity1.getStringExtra("item");

Put this inside onCreate. Also onCreate doesn't have override annotation.

Copy paste this instead in your Show.java

public class Show extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.show);
    Intent launchActivity1 = getIntent();
    String item = launchActivity1.getStringExtra("item"); // this is line 17
}

}

add it in oncreate block...

public class Show extends Activity {
 protected void onCreate(Bundle savedInstanceState) {
           ................
           ................


     Intent launchActivity1 = getIntent();
    String item = launchActivity1.getStringExtra("item"); // this is line 17
    }
}

Or it happens when you pass object instead of string. So use

launchActivity1.putExtra("item", ""+item.getName());

thanks..

and in onclick should be..

Product item = (Product)parent.getAdapter().getItem(position);

I have Solved the issue on my own. thank you all of you for your quick response... solution is...

MainActivity.java

lvProduct.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getApplicationContext(), "Clicked product id =" + view.getTag(), Toast.LENGTH_SHORT).show();

                Product item = (Product)lvProduct.getAdapter().getItem(position);

                Intent launchActivity1= new Intent(MainActivity.this,Show.class);
                launchActivity1.putExtra("item", item.getName());
                startActivity(launchActivity1);


            }
        });

Show.java

public class Show extends Activity {

    String passedVar = null;
    private TextView passedView= null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show);
       passedVar = getIntent().getStringExtra("item");
        passedView = (TextView) findViewById(R.id.pass);
        passedView.setText("You= " + passedVar);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM