为ListFragment设置clicklistener

时间:2014-09-08 11:05:21

标签: android onclicklistener android-listfragment

我有一个“modell”类,它定义了我的列表的结构和值,称为Application。现在在我的ListFragment类中,我试图获取已在listrow中单击的标题。按照我目前的代码:

应用程序类:

public class Application {
    private String title;
    private long totalDl;
    private String rating;
    private String icon;


    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public long getTotalDl() {
        return totalDl;
    }
    public void setTotalDl(long totalDl) {
        this.totalDl = totalDl;
    }
    public String getRating() {
        return rating;
    }
    public void setRating(String rating) {
        this.rating = rating;
    }
    public String getIcon() {
        return icon;
    }
    public void setIcon(String icon) {
        this.icon = icon;

    }
}

带有clicklistener部分的ListFragment类:

public class NewUploadsFragment extends ListFragment {

private Application app;
...

@Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        Toast.makeText(getActivity(),
                "Item " + app.getTitle() + " was clicked", Toast.LENGTH_SHORT)
                .show();

    }
}

但它不起作用,我会收到错误。所以我的问题是,如果有人知道如何显示listrow 1的点击“标题”。使用Application类 2。没有Application类?

谢谢你们!

2 个答案:

答案 0 :(得分:0)

您可以使用 onItemClickListener ,如

public void onItemClickListener(AdapterView<?> obj,View v, int pos, long arg3) { 
Application app= (Application )obj.getItemAtPosition(pos);
Toast.makeText(getActivity(),"Item " + app.getTitle() + " was clicked", Toast.LENGTH_SHORT).show();
}

也来自 ArrayList ,如

ArrayList<Application> listofObjects;
// add values to listofObjects
// set listAdapter
 .......
 .......
public void onItemClickListener(AdapterView<?> obj,View v, int pos, long arg3) 
{ 
  Application app= listofObjects.get(pos);
  Toast.makeText(getActivity(),"Item " + app.getTitle() + " was clicked", Toast.LENGTH_SHORT).show();
}

答案 1 :(得分:0)

您可以使用View。

从ListRow获取String
// listening to single list item on click
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
      //in this example "name" is the item from row you want to display
      TextView ptitle = (TextView) view.findViewById(R.id.name);
      String Title = ptitle.getText().toString();
      Toast.makeText(getActivity(),"Item " + Title + " was clicked", Toast.LENGTH_SHORT).show();
}
});

将R.id.name更改为R.id。“您要显示的视图”