简体   繁体   English

从ListView启动活动

[英]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. 我希望从上面的xyz类开始一个新的Activity。 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. 在下一个活动中,我希望显示“ ListItem”对象的更多详细信息。 s_img and sname; s_img和sname; Is there a way by which I could pass on the CLICKED ListItem object to the next DisplayScheme activity ? 有没有一种方法可以将CLICKED ListItem对象传递给下一个DisplayScheme活动? 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. 这是我编辑的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)" 现在,我在“ intent.putextra”行上收到一条错误消息,该行显示“ Intent类型的方法putExtra(String,boolean)不适用于参数(String,Object)”

................. more edits.. arrayadapter................ .................更多编辑.. 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 ? 这是我的MyAdapter类,我到底需要在哪里进行诸如“ setTag()”或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 . 但是,建议不要使用v.getContext()xyz.this

Also, if you are not expecting a result back from the DisplayScheme Activity, there is no need to use startActivityForResult() . 另外,如果您不期望从DisplayScheme活动返回结果,则无需使用startActivityForResult() you should use just the startActivity() and pass the intent instance to it. 您应该只使用startActivity()并将intent实例传递给它。

If you need to send data with the Intent , a simple intent.putExtra(SOME_KEY_NAME, THE_VALUE_YOU_WANT_TO SEND); 如果您需要使用Intent发送数据,则可以使用一个简单的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. 如果要将数据发送到另一个Activity,则无需使用startActivityForResult() 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; 好的,因此在“活动”中还应该具有相应的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; 如果使用POJO( setter和getter )处理数据,然后再将其传递给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) . 您可以使用setTag()在适配器的getView()方法(例如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()); 因此,您可以使用onClick()方法从视图中获取标记,然后将其发送到具有intent.putExtra("positionIdentifier",view.getTag());类的其他内容的下一个活动intent.putExtra("positionIdentifier",view.getTag());

Yes, you can put some data when you start activity 是的,您可以在开始活动时放入一些数据

This part must be in OnItemClickListener 这部分必须在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. 它将v.getTag()视为对象。 if you have tag value as a string then you have to cast in string and pass value by putting ".toString()" after v.getTag(). 如果您将标记值作为字符串,则必须强制转换字符串并通过在v.getTag()之后放置“ .toString()”来传递值。

Note : its only for String passing. 注意:它仅适用于String传递。 you can do the same for other values by casting. 您可以通过强制转换对其他值执行相同的操作。

this is my edited onListItemClick. 这是我编辑的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)" 现在,我在“ intent.putextra”行上收到一条错误消息,该行显示“ Intent类型的方法putExtra(String,boolean)不适用于参数(String,Object)”

this is because the getTag() returns an object, and this is because you can put there whatever you wish. 这是因为getTag()返回了一个对象,这是因为您可以在其中放置任何所需的对象。

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. 如果您在其中放置了其他内容(例如viewHolder),请决定要放置的内容并进行投射。


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): 在适配器外部,在活动“ xyz”中(应将其重命名为大写字母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);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM