简体   繁体   中英

How can i gain access to the selected list item in another class in android

here are my classes, when i run the application it crashes on selection and gives me a null error. I'm designing a revision app and am trying to do it without having to make a class for every topic covered any help would be much appreciated.

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import org.w3c.dom.Text;

public class TheoryMain extends Activity {
    TheoryTopicList ttl;
    TextView tv1;
    String choice;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.theory_layout);
        ttl = new TheoryTopicList();
        ttl.getChoice();
        tv1 = (TextView) findViewById(R.id.theory_tv);
        switch(choice){
            case("Photo-Electric effect"):
                tv1.setText(getString(R.string.photo_electric));
                break;
            case("Photons and Electrons"):
                tv1.setText(getString(R.string.photons_electrons));
                break;
            case("de Broglie wavelength"):
                tv1.setText(getString(R.string.de_broglie));
                break;
            case("Types of particles"):
                tv1.setText(getString(R.string.particles));
                break;
            case("Interactions"):
                tv1.setText(getString(R.string.interactions));
                break;
            case("Radiation"):
                tv1.setText(getString(R.string.radiation));
                break;
            case("Voltage, Current and Resistance(Ohms law)"):
                tv1.setText(getString(R.string.ohms_law));
                break;
            case("Circuits"):
                tv1.setText(getString(R.string.circuits));
                break;
            case("Power and Efficiency"):
                tv1.setText(getString(R.string.power_efficiency));
                break;
            case("Alternating Current and oscilloscope graphs"):
                tv1.setText(getString(R.string.ac_graphs));
                break;
            case("E.M.F and internal Resistance"):
                tv1.setText(getString(R.string.emf_resistance));
                break;
        }
    }
}






import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class TheoryTopicList extends ListActivity {
    String[] display = {"Photo-Electric effect", "Photons and Electrons", "de Broglie wavelength", "Types of particles",
                        "Interactions", "Radiation", "Voltage, Current and Resistance(Ohms law)", "Circuits",
                        "Power and Efficiency","Alternating Current and oscilloscope graphs", "E.M.F and internal Resistance"};
    String choice;
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {

        super.onListItemClick(l, v, position, id);
        choice = display[position];
        Intent intent = new Intent("com.revisonapp.alec.alevelphysics.THEORYMAIN");
        startActivity(intent);
    }

    public String getChoice(){
        return choice;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(TheoryTopicList.this,android.R.layout.simple_list_item_1,display));
    }
}

Try to do like this

Intent intent=new Intent(this,AnotherClass.class);
intent.putExtra("yourStringVal",yourStringVal);
startActivity(intent);
and in your another class try 2 get these items by doing

  Bundle extras = getIntent().getExtras();
        if(extras!=null){
            String yourStringVal=extras.getString("yourStringVal");

        }

Your understanding of the way you work with Activities is a little wrong.

Typically, you never instance an Activity yourself.

You need to investigate how to use Intents , startActivity and onActivityResult .

Firstly, you create an Intent to TheoryTopicList , then you set the result data and finish that Activity . Then you read the selected option from onActivityResult .

In your TheoryTopicList Activity when you fire the Intent to call the TheoryMain activity, you can send data along with that intent. This data can be received by the TheoryMain class and used to do an appropriate operation.

Add data to your intent in the TheoryTopicList activity.

Intent intent = new Intent("com.revisonapp.alec.alevelphysics.THEORYMAIN");
intent.putExtra("choice_selected", choice);

// The first parameter is the key and the next is the value. // key in order to retrieve the value in the next activity.

startActivity(intent);

Remove the activity instantiation lines from TheoryMain,

ttl = new TheoryTopicList(); // Not needed and not good
ttl.getChoice(); // Not needed and not good

Now retrieve the data that the previous activity gave you. In your onCreate of TheoryMain add,

...

String choice = getIntent().getExtra("choice_selected"); // Using the key
switch(choice){

...
}

replace this code :

Intent intent = new Intent("com.revisonapp.alec.alevelphysics.THEORYMAIN");

startActivity(intent);

with

Intent intent = new Intent(TheoryTopicList .this,TheoryMain .class);

intent.putExtra("choice",choice);

startActivity(intent);

and In TheoryMain activity's on create method.

replace this

ttl = new TheoryTopicList();
ttl.getChoice();

with

if(getIntent().hasExtra("choice"))
 {
     choice = getIntent().getStringExtra("choice");
 }
else{
      choice ="";
}

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