Android 点击项的 ListView

35
我是一名新手程序员,也是Android的新手。我正在使用这个示例,并且它非常好用。
现在我想让项目(Dell,Samsung Galaxy S3等)调用一个函数来打开一个新的活动,并显示不同的信息。
例如:
如果我点击Dell,则必须显示一个新的Activity,向我显示有关Dell的信息。如果我点击三星,则相同的事情。
我在谷歌上搜索,但找不到任何有用的提示。我认为这很基础,但我是新手,所以我真的不知道从哪里开始。
9个回答

81
在您定义列表视图的活动中,您写下了以下内容。
listview.setOnItemClickListener(new OnItemClickListener(){   
    @Override
    public void onItemClick(AdapterView<?>adapter,View v, int position){
        ItemClicked item = adapter.getItemAtPosition(position);

        Intent intent = new Intent(Activity.this,destinationActivity.class);
        //based on item add info to intent
        startActivity(intent);
    }
});

在您的适配器的getItem方法中,您编写

public ItemClicked getItem(int position){
    return items.get(position);
}

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

        Intent i = new Intent(getActivity(), DiscussAddValu.class);
        startActivity(i);
    }
});

3
此答案出现在低质量审核队列中,可能是因为您没有对代码进行任何解释。如果此代码回答了问题,请考虑在答案中添加一些解释以便更容易被理解。这样,您就更有可能获得更多的赞同,并帮助提问者学到新知识。 - lmo

5

你可以使用Intent来启动新的活动。向Intent传递数据的一种方法是在Intent中传递一个实现Parcelable接口的类。请注意,你传递的是该类的副本。

http://developer.android.com/reference/android/os/Parcelable.html

这里有一个onItemClick。我创建了一个意图并将整个类放入意图中。我发送的类已经实现了parcelable。提示:您只需要最小限度地实现可解析内容来重新创建类。例如,可能是文件名或简单的字符串,可以用于创建类的构造函数。新活动稍后可以获取Extras,并使用其构造方法创建类的副本。
这里当我在listview中接收到onclick时,启动我的应用程序的kmlreader类。
注意:下面的摘要是我传递的类的列表,因此get(position)实际上返回该类,它是填充listview的相同列表。
List<KmlSummary> summary = null;
...

public final static String EXTRA_KMLSUMMARY = "com.gosylvester.bestrides.util.KmlSummary";

...

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    lastshownitem = position;
    Intent intent = new Intent(context, KmlReader.class);
    intent.putExtra(ImageTextListViewActivity.EXTRA_KMLSUMMARY,
            summary.get(position));
    startActivity(intent);
}

稍后在新的活动中,我使用可解析类进行提取。
kmlSummary = intent.getExtras().getParcelable(
                ImageTextListViewActivity.EXTRA_KMLSUMMARY);

//note:
//KmlSummary implements parcelable.
//there is a constructor method for parcel in
// and a overridden writetoparcel method
// these are really easy to setup.

public KmlSummary(Parcel in) {
    this._id = in.readInt();
    this._description = in.readString();
    this._name = in.readString();
    this.set_bounds(in.readDouble(), in.readDouble(), in.readDouble(),
    in.readDouble());
    this._resrawid = in.readInt();
    this._resdrawableid = in.readInt();
     this._pathstring = in.readString();
    String s = in.readString();
    this.set_isThumbCreated(Boolean.parseBoolean(s));
}

@Override
public void writeToParcel(Parcel arg0, int arg1) {
    arg0.writeInt(this._id);
    arg0.writeString(this._description);
    arg0.writeString(this._name);
    arg0.writeDouble(this.get_bounds().southwest.latitude);
    arg0.writeDouble(this.get_bounds().southwest.longitude);
    arg0.writeDouble(this.get_bounds().northeast.latitude);
    arg0.writeDouble(this.get_bounds().northeast.longitude);
    arg0.writeInt(this._resrawid);
    arg0.writeInt(this._resdrawableid);
    arg0.writeString(this.get_pathstring());
    String s = Boolean.toString(this.isThumbCreated());
    arg0.writeString(s);
}

祝你好运Danny117


4

你肯定需要扩展你的ArrayListAdapter 并且在你的getView() 方法中实现它。如果第二个参数 (一个View) 的值是null,就应该将其设置为已充气状态并在充气后立即设置onClickListener()

假设你的第二个getView() 参数被称为 convertView

convertView.setOnClickListener(new View.OnClickListener() {
  public void onClick(final View v) {
    if (isSamsung) {
      final Intent intent = new Intent(this, SamsungInfo.class);
      startActivity(intent);
    }
    else if (...) {
      ...
    }
  }
}

如果你想了解如何扩展ArrayListAdapter,我推荐这个链接

4
在你的onitemClick中,你会发送所选的值,比如 deal,并在打开新活动时将其发送到意图中,在你的新活动中获取已发送的数据,并显示与所选项相关的数据。
要从list中获取名称。
String item = yourData.get(position).getName(); 

在Intent中设置数据

intent.putExtra("Key", item);

在第二个活动中获取数据

getIntent().getExtras().getString("Key")

1

为什么要实现Parcelable?

他将String[]传递给适配器,因此:

  • 在位置处获取item(String)
  • 创建intent
  • 将其作为额外内容放入intent中
  • 启动activity
  • 在activity中获取额外内容

要存储产品列表,您可以在此处使用HashMap(例如作为静态对象)。

描述产品的示例类:

public class Product {
    private String _name;
    private String _description;
    private int _id

    public Product(String name, String description,int id) {
        _name = name;
        _desctription = description;
        _id = id;
    }

    public String getName() {
        return _name;
    }

    public String getDescription() {
        return _description;
    }
}

Product dell = new Product("dell","this is dell",1);

HashMap<String,Product> _hashMap = new HashMap<>();
_hashMap.put(dell.getName(),dell);

然后您将一组键传递给适配器,如下所示:

String[] productNames =  _hashMap.keySet().toArray(new String[_hashMap.size()]);

当在适配器中返回视图时,您可以像以下示例一样设置监听器: ```java ```
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {

     Context context = parent.getContext(); 

     String itemName = getItem(position)

     someView.setOnClikListener(new MyOnClickListener(context, itemName));

 }


 private class MyOnClickListener implements View.OnClickListener { 

     private  String _itemName; 
     private  Context _context

     public MyOnClickListener(Context context, String itemName) {
         _context = context;
         _itemName = itemName; 
     }

     @Override 
     public void onClick(View view) {
         //------listener onClick example method body ------
         Intent intent = new Intent(_context, SomeClassToHandleData.class);
         intent.putExtra(key_to_product_name,_itemName);
         _context.startActivity(intent);
     }
 }

然后在其他活动中:
@Override 
public void onCreate(Bundle) {

    String productName = getIntent().getExtra(key_to_product_name);
    Product product = _hashMap.get(productName);

}

*key_to_product_name是一个公共静态字符串,用作额外的密钥。
注:抱歉有打字错误,我很匆忙 :) 注2:这应该能让你明白如何做 注3:当我有更多时间时,我会添加详细描述。 我的评论:
  • 不要使用任何switch语句
  • 不要为每个产品创建单独的活动(您只需要一个)

1
listview.setOnItemClickListener(new OnItemClickListener(){

//setting onclick to items in the listview.

@Override
public void onItemClick(AdapterView<?>adapter,View v, int position){
Intent intent;
switch(position){

// case 0 is the first item in the listView.

  case 0:
    intent = new Intent(Activity.this,firstActivity.class);
    break;
//case 1 is the second item in the listView.

  case 1:
    intent = new Intent(Activity.this,secondActivity.class);
    break;
 case 2:
    intent = new Intent(Activity.this,thirdActivity.class);
    break;
//add more if you have more items in listView
startActivity(intent);
}

});

0
listview.setOnItemClickListener(new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?>adapter,View v, int position){
    Intent intent;
    switch(position){
      case 0:
        intent = new Intent(Activity.this,firstActivity.class);
        break;
      case 1:
        intent = new Intent(Activity.this,secondActivity.class);
        break;
     case 2:
        intent = new Intent(Activity.this,thirdActivity.class);
        break;
    //add more if you have more items in listview
   //0 is the first item 1 second and so on...
    }
    startActivity(intent);
  }

});

0

我通过将上下文引用从 thisContext.this 替换为 getapplicationcontext 来避开整个问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接