我已经在我的应用程序中使用activity实现了这个功能, 参考下面的图像链接 " http://imgur.com/LuErJjY"
在第一部分中,您可以看到context=PerformanceActivity@4015
但在第二部分它是空的
我使用的代码是
活动:
viewHolder.nextReview.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int d=v.getId();
((PerformanceActivity)context).performReview(v.getId());
}
});
在碎片中
注意:PerformanceFragment pf;
viewHolder.nextReview.setId(resData.get(position).getTestID());
viewHolder.nextReview.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int d=v.getId();
((PerformanceFragment)pf).performReview_frag(v.getId());
}
});
这两种方法都是listview适配器中的方法。在活动中它只是工作正常,但不是片段。
与两个适配器的链接:
https://pastee.org/28chw - 片段的适配器https://pastee.org/nw8rr - 片段
https://pastee.org/wxepy - 活动适配器
答案 0 :(得分:0)
如果您的片段@State(Scope.Benchmark)
@BenchmarkMode(Mode.AverageTime)
public class SO30507506 {
@Param({"1", "12345", "123456789"}) int n;
int i;
String s;
@Setup public void setup() {
i = n;
s = String.valueOf(n);
}
@Benchmark public boolean parse() {
return Integer.parseInt(s) == i;
}
@Benchmark public boolean stringEquals() {
return s.equals(Integer.toString(i));
}
@Benchmark public boolean manual() {
return parseManual(s) == i;
}
@Benchmark public boolean manualAtlaste() {
return equalsIntString(i, s);
}
@Benchmark public boolean parse_unequal() {
return Integer.parseInt(s) == i * 2;
}
@Benchmark public boolean stringEquals_unequal() {
return s.equals(Integer.toString(i * 2));
}
@Benchmark public boolean manual_unequal() {
return parseManual(s) == i * 2;
}
@Benchmark public boolean manualAtlaste_unequal() {
return equalsIntString(i * 2, s);
}
private static int parseManual(String s) {
int result = 0;
int sign = s.charAt(0) == '-' ? -1 : 1;
int startIndex = (s.charAt(0) >= '0' && s.charAt(0) <= '9') ? 0 : 1;
for (int i = startIndex; i < s.length(); i++) {
result *= 10;
result += s.charAt(i) - '0';
}
return result * sign;
}
private static boolean equalsIntString(int value, String s) {
if (s.isEmpty()) return false; // This is never good.
if ((s.charAt(0) == '-' && value >= 0) || (s.charAt(0) != '-' && value < 0)) return false; // positive/negative check
// Define the limit. This is basically the end of the string to check.
int limit = 0;
if (value < 0) {
limit = 1;
value = -value;
}
for (int i = s.length() - 1; i >= limit; --i) {
char expected = (char) ('0' + (value % 10)); // the modulo will be optimized by the JIT because 10 is a constant
value /= 10; // same story.
if (s.charAt(i) != expected) return false;
}
return true;
}
}
与您的活动getActivity()
PerformanceFragment
方法
PerformanceActivity
否则,
您可以使用viewHolder.nextReview.setId(resData.get(position).getTestID());
viewHolder.nextReview.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int d=v.getId();
/* (getActivity()). no need to use content if
performReview_frag() is present in PerformanceFragment fragment*/
// this will call performReview_frag() method
performReview_frag(v.getId());
}
});
方法获取正确的上下文
答案 1 :(得分:0)
最后这对我有用 -
PerformanceFragmentAdapter adapter = new PerformanceFragmentAdapter(context,rsuData,device,this);
并将其作为
添加到适配器private PerformanceFragment pf;
public PerformanceFragmentAdapter(Context conte, ArrayList<ResultData> rData,
int device, PerformanceFragment pp) {
super();
context = conte;
resData = rData;
size = device;
pf=pp;
}
猜@ursgtm是对的。仍然会在Context c=getActivity();
和this
关键字
答案 2 :(得分:0)
在PerformanceFragmentAdapter
课程中:
PerformanceFragment pf;
您只是创建一个没有实例的对象,并且您正在使用该对象作为上下文,并且您没有为pf
分配任何内容。
而不是删除pf
,并传递从构造函数中获取的上下文:
viewHolder.nextReview.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int d=v.getId();
//Replace ((PerformanceFragment)pf) with context
context.performReview_frag(v.getId()); //you obtained context from contractor.
}
});
希望这能帮到你!