سؤال

Below is the code for a listview with an adapter

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_online_users);
    final ListView listview = (ListView) findViewById(R.id.listviewusers);
    //onlineUsers.setAdapter()

    //final ListView listview = (ListView) findViewById(R.id.listview);

    Intent intent = getIntent();

    String username = intent.getStringExtra(connection.SERVER_ONLINE_USERS);

    String[] values = username.split(",");

    final ArrayList<String> list = new ArrayList<String>();
    for (int i = 0; i < values.length; ++i) {
      list.add(values[i]);
    }
    final StableArrayAdapter adapter = new StableArrayAdapter(this,
        android.R.layout.simple_list_item_1, list);
    listview.setAdapter(adapter);


    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> parent, final View view,
          int position, long id) {
        final String item = (String) parent.getItemAtPosition(position);

       // view.setAlpha(1);

      }


    });

  }

What i want to do is get the String item which will be clicked on in the listview, start a new intent and pass the string item to it,

something similar to:

Intent i = new Intent(this, activity.class);
sign.putExtra(KEY, value);
startActivity(i);

But when I use the above Intent code in my onItemClickListener() I get an error.

"The Constructor Intent(New AdapterView.OnItemClickListener(){}, Class<activity>) is undefined"

The suggested way to fix it is to remove the arguments from the intent and have it like this

Intent i = new Intent(); 
هل كانت مفيدة؟

المحلول

I'm confused on the problem because you seem to know how to do it. So let's put it together

 @Override
  public void onItemClick(AdapterView<?> parent, final View view,
      int position, long id) {
    final String item = (String) parent.getItemAtPosition(position);

   // view.setAlpha(1);

   Intent i = new Intent(view.getContext(), activity.class);  // get a valid context
   i.putExtra("someKey", item);  //I don't know where sign came from
   startActivity(i);

  }

then get it in your Activity such as in onCreate() with

Intent itent = getIntent();
String value = intent.getStringExtra("someKey");
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top