0

Anyone know about how to fix my code? in my StudentListView.setOnItemClickListener there is putExtra which is will send the data to ActivityShow.class

StudentListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(Activity.this,ActivityShow.class);
        // Sending ListView clicked value using intent.
        intent.putExtra("ListViewValue", IdList.get(position));
        startActivity(intent);
    }
});

//send data to service
Intent serviceIntent = new 
Intent(Activity.this,ActivityService.class);
serviceIntent.putExtra("ListViewValue",  IdList.get(position));
startService(serviceIntent);

my problem is when I Try to add that putExtra directly to my service class, it give me an error that said IdList.get(position) red color for position,

I try to remove the get.(position) but it give me a null one, anyone know how to fix this?

Forumesia
  • 73
  • 1
  • 11
  • variable position cannot defined since it only define in `onItemClick` – John Joe Feb 07 '18 at 03:43
  • so, can i change the onItemClick? to add that function to send my data not on itemclick, but directly when the app is open – Forumesia Feb 07 '18 at 03:45
  • But how does the app know which item is clicked ? There are something wrong with your logic. From the code you given, ListViewValue will store the value based on the listview position you clicked. But now you want to get the data without listview click ? – John Joe Feb 07 '18 at 03:47

4 Answers4

0

The position is a local variable which has scope up to the only onItemClick(). So either define another variable position outside the method and assign it a value in onItemClick().

theanilpaudel
  • 3,348
  • 8
  • 39
  • 67
0

Try using a model class that implements Parcelable interface. This way you would be able to serialize the object in your Activity and then de-seialize it back in your Service class.

A generic example for the same:

public class MyActivity extends AppCompatActivity {
  // Your activity code here

  public void onCreate(Bundle savedInstance){
    super.onCreate(savedInstance);
    // onCreate() code
    // create and populate your list
    final ArrayAdapter<MyModel> myAdapter = new ArrayAdapter<>();
    listView.setAdapter(myAdapter);

    listView.setOnItemClickListener((parent, view, pos, id) -> {
      MyModel model = myAdapter.get(pos);
      Intent intent = new Intent(MyActivity.this, MyService.class);
      intent.putExtra("ITEM", model);
      startService(intent); // basic threading rules apply! be carefull
    });
  }

  // inner class just for demo
  static class MyModel implements Parcelable {
    // your model goes here!

    public int describeContents() {
      return 0;
    }

    public void writeToParcel(Parcel out, int flags) {
      // write your model data into parcel
    }

    public static final Parcelable.Creator<MyModel> CREATOR = new Parcelable.Creator<MyModel>() {
      public MyModel createFromParcel(Parcel in) {
        return new MyModel(in);
      }

      public MyModel[] newArray(int size) {
        return new MyModel[size];
      }
    };

    private MyParcelable(Parcel in) {
      // create model from parcel
    }
  }
}

Then in your Service you can get the model by

MyActivity.MyModel model = (MyActivity.MyModel) intent.getExtra("MODEL");

You can also use Java's Serialization but it is generally advised not to serialize Objects this way in Android. You can read more about it on this StackOverflow Answer

riyaz-ali
  • 8,677
  • 2
  • 22
  • 35
0

Define a global variable pos

then check you have clicked a item or not before sending value to service

int pos =-1;

StudentListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
pos=position;

        Intent intent = new Intent(Activity.this,ActivityShow.class);
        // Sending ListView clicked value using intent.
        intent.putExtra("ListViewValue", IdList.get(position));
        startActivity(intent);
    }
});

//send data to service
if(pos>=0){
Intent serviceIntent = new 
Intent(Activity.this,ActivityService.class);
serviceIntent.putExtra("ListViewValue",  IdList.get(pos));

startService(serviceIntent);
}
Mohit Hooda
  • 273
  • 3
  • 9
0

It's sound a little weird, but I found to fix this using shared preferences manager thanks for your help

Forumesia
  • 73
  • 1
  • 11