سؤال

I've looked and searched at other examples which seem to say the following.

In the Array Adapter make sure the Context is declared

Context context;

    public ListViewJobAdapter(Context context, ArrayList jobItemsArrayList) {

        super(context, R.layout.tab3_list_row, jobItemsArrayList);
        this.context = context;
    }

I override GetView of the ListView Adapter. Within this I have a textView onClick action which starts a new activity with extras.

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

        textView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Intent intentEditJob = new Intent(context,ActivityJobEdit.class);
                intentEditJob.putExtra("JOBID", "TEST");
                context.startActivity(intentEditJob);

Now the Actvitiy starts but the Extras are not passed.

    Intent intentExtras = getIntent();
    String editJobID = intentExtras.getStringExtra("JOBID");

Within the customer ListView Adapter, if I call the Intent function outside of getView, it then works and the Extra is passed. Not sure why this doesn't work within the onClick of GetView as the context is referenced? For now I will use the workaround of calling it outside GetView.

public void callEditActivity(){
    Intent intentEditJob = new Intent(context,ActivityJobEdit.class);
    intentEditJob.putExtra("TEST", "JOBID");
    context.startActivity(intentEditJob);
}
هل كانت مفيدة؟

المحلول

It looks like you were inconsistent with the key used to store and retrieve the value.

The putExtra method accepts the key as the first parameter and the value as the second. From what you wrote I assume that "JOBID" is the key yet in callEditActivity you use "TEST" as the key.

Make sure you are consistent with this and everything will align into place.

For setting the extra intent parameter use:

intentEditJob.putExtra("JOBID", "TEST");

For retrieving the extra intent parameter use:

String editJobID = intentExtras.getStringExtra("JOBID");   

نصائح أخرى

Start by creating a static final string for your intent value:

public static final string result = "result";

public void callEditActivity() {
    Intent intentEditJob = new Intent(context,ActivityJobEdit.class);
    intentEditJob.putExtra(result, "JOBID");
    context.startActivity(intentEditJob);
}

Then call it:

Intent intentExtras = getIntent();
String editJobID = intentExtras.getStringExtra(previousClass.result);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top