简体   繁体   中英

Launching an activity from the ListView

public class ListItem
{   
public int sname;
public int s_img;
public String sid; 
}


Class xyz extends ListActivity
{
.
.
.
protected void onListItemClick(ListView l, View v, int position, long id) 
{

       //super.onListItemClick(l, v, position, id);     
      Toast.makeText(ListPage.this,items.get(position).sid,Toast.LENGTH_SHORT).show();
      Intent intent = new Intent(v.getContext(),DisplayScheme.class);
      startActivityForResult(intent,0);
}
}

I wish to start a new Activity from the above xyz class. The Activity should start when one of the items on the list is clicked. In the next Activity, I wish to display further details of the "ListItem" object viz. s_img and sname; Is there a way by which I could pass on the CLICKED ListItem object to the next DisplayScheme activity ? coz there is no way in the next Activity to find out which item was clicked in the earlier activity. Thanks in advance.

.............. edited ...............

protected void onListItemClick(ListView l, View v, int position, long id) 
    {

    //super.onListItemClick(l, v, position, id);        
    Toast.makeText(ListPage.this, items.get(position).sid, Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(v.getContext(),DisplayScheme.class);
    intent.putExtra("positionIdentifier",v.getTag());
    startActivityForResult(intent,0);
    }

this is my edited onListItemClick. Now I am getting an error on the "intent.putextra" line which says "The method putExtra(String, boolean) in the type Intent is not applicable for the arguments (String, Object)"

................. more edits.. arrayadapter................

public class MyAdapter extends BaseAdapter
{  

LayoutInflater inflater;
List<ListItem> items;

public MyAdapter(Activity context, List<ListItem> items) 
{  
    super();

    this.items = items;
    this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override  
public int getCount() 
{  
    // TODO Auto-generated method stub  
    return items.size();  
}  

@Override  
public Object getItem(int position) 
{  
    // TODO Auto-generated method stub  
    return null;  
}  

@Override  
public long getItemId(int position) 
{  
    // TODO Auto-generated method stub  
    return 0;  
}

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

    ListItem item = items.get(position);
    View vi=convertView;

    if(convertView==null)
        vi = inflater.inflate(R.layout.list_row, null);

    ImageView imgv = (ImageView)vi.findViewById(R.id.s_name); 
    imgv.setImageResource(item.sname);                
    return vi;  
}
}

this is my MyAdapter class, where exactly do I need to make changes like "setTag()" or something like dat ?

This, mostly looks good:

Toast.makeText(ListPage.this,items.get(position).sid,Toast.LENGTH_SHORT).show();
Intent intent = new Intent(v.getContext(),DisplayScheme.class);
startActivityForResult(intent,0);

But, instead of using the v.getContext() I would suggest using this: xyz.this .

Also, if you are not expecting a result back from the DisplayScheme Activity, there is no need to use startActivityForResult() . you should use just the startActivity() and pass the intent instance to it.

If you need to send data with the Intent , a simple intent.putExtra(SOME_KEY_NAME, THE_VALUE_YOU_WANT_TO SEND); will do it for you.

You don't need to use startActivityForResult() if you want to send data to another Activity. You use it when you want to fetch some result back from another Activity.

EDIT:

Okay, so in the Activity you should also have the corresponding List<ListItem> items; to gather the data and pass to the Adapter right? You still haven't posted that code for the Activity. But see if this helps. You may have to play around to get it right.

Toast.makeText(ListPage.this,items.get(position).sid,Toast.LENGTH_SHORT).show();
Intent intent = new Intent(v.getContext(),DisplayScheme.class);
intent.putExtra("SOME_KEY_NAME", items.get(position).sname;
startActivityForResult(intent,0);

If you are using a POJO ( a setter and getter ) to handle the data before passing it to the List<ListItem> items; , posting that and as much details from the Activity as you can would help even more.

Yes. It is possible. You can use setTag() to keep an identifier for each list item in your getView() method of your adapter like setTag(position) . So you can get the tag from the view in the onClick() method and then send it to the next activity with extras like intent.putExtra("positionIdentifier",view.getTag());

Yes, you can put some data when you start activity

This part must be in OnItemClickListener

Intent intent = new Intent(CurrentClass.this, ActivityForStart.class);
intent.putExtra("name_of_variable", variable);
startActivity(intent);

in new activity

intent.getStringExtra("name_of_variable")));

it considers v.getTag() as a object. if you have tag value as a string then you have to cast in string and pass value by putting ".toString()" after v.getTag().

Note : its only for String passing. you can do the same for other values by casting.

this is my edited onListItemClick. Now I am getting an error on the "intent.putextra" line which says "The method putExtra(String, boolean) in the type Intent is not applicable for the arguments (String, Object)"

this is because the getTag() returns an object, and this is because you can put there whatever you wish.

if you've put there a boolean, just put a cast on it and you are ready to go.

if you've put there something else (like a viewHolder), decide what you wish to put and cast it instead.


here's a simple modification for your code to work:

in the adapter:

@Override  
public ListItem getItem(int position) 
  {  
  return items.get(position);
  }  

@Override  
public View getView(final int position, View convertView, ViewGroup parent) 
  {  
  ListItem item = getItem(position);
  //... same as before, but please use viewHolder
  }

outside the adapter , in the activity "xyz" (which you should rename to have an uppercase letter btw):

protected void onListItemClick(ListView l, View v, int position, long id) 
  {
  super.onListItemClick(l, v, position, id);     
  Intent intent = new Intent(this,DisplayScheme.class);
  intent.putExtra("selectedImageToShow",_adapter.getItem(position).s_img);
  startActivityForResult(intent,0);
  }

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