-3

I am trying open an other activity on click of the list item of list view, and display the data of the clicked list item onto the new activity . But I am able to get the data of the list item onclick but when i'm starting new activity using startAcitvity(intent) method , then only a blank activity is opening , and on debugging i'm getting the info that startActivity() is undefined . Please help me to resolve this issue . My code is here:

public class MainActivity extends AppCompatActivity {
ListView lvDetail;
Intent intent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lvDetail = (ListView) findViewById(R.id.list);
    new ProgressTask(MainActivity.this).execute();

    lvDetail.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            try {
                intent = new Intent(MainActivity.this, DataDisplayer.class);
                Employee e = (Employee) parent.getItemAtPosition(position);
                ArrayList<String> el = new ArrayList<String>();
                el.add(String.valueOf(e.getId()));
                el.add(e.getName());
                el.add(String.valueOf(e.getAge()));
                el.add(String.valueOf(e.getSalary()));
                Bundle bundle = new Bundle();
                bundle.putStringArrayList("emp", el);
                intent.putExtras(bundle);
                //intent.putStringArrayListExtra("emp", el);
                startActivityForResult(intent, 187);
            } catch (Exception er) {
                er.printStackTrace();
            }
        }
    });
Anushka Khare
  • 363
  • 3
  • 11

2 Answers2

2

Actually I caught the issue my self actually the issue was is in DataDisplayer class. So this was the only issue. There is no problem in MainActivity. I was using the onCreate method as follows :

public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) { super.onCreate(savedInstanceState, persistentState); }

Anushka Khare
  • 363
  • 3
  • 11
  • While I should use the following onCreate instead of above method : – Anushka Khare Oct 26 '15 at 10:21
  • protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } – Anushka Khare Oct 26 '15 at 10:22
  • protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } is Android Activity life cycle method, in this method your view will design. That is why this method is useful for it. @Anushka Khare – Pratik Dasa Sep 02 '16 at 12:57
0

You are in an Inner class so you have to call startactivity as below :

MainActivity.this.startActivityforresult(intent,reqcode);
Sherif
  • 11,786
  • 3
  • 32
  • 57
srinivas
  • 72
  • 1
  • 9
  • Sorry, this in not true. startActivityForResult is not overriden by AdapterView.OnItemClickListener, so you can call it like in the code of the topic description. – Christopher Oct 26 '15 at 08:12
  • There is another way of doing this also override the method onListItemClick() and start your activity from that method, instead of this OnclickListner() interface if the later is not going to work:) – srinivas Oct 26 '15 at 08:41