我正在尝试使用Django框架构建一个英语和保加利亚语的网站.我的想法是用户应该点击一个按钮,页面将重新加载,语言将被更改.这就是我试图这样做的方式:
在我的HTML中,我有一个按钮标签 <button id='btn' onclick="changeLanguage();" type="button"> ... </button>
cookies.js的摘录:
function changeLanguage() {
if (getCookie('language') == 'EN') {
document.getElementById('btn').innerHTML = getCookie('language');
setCookie("language", 'BG');
} else {
document.getElementById('btn').innerHTML = getCookie('language');
setCookie("language", 'EN');
}
}
function setCookie(sName, sValue, oExpires, sPath, sDomain, bSecure) {
var sCookie = sName + "=" + encodeURIComponent(sValue);
if (oExpires) {
sCookie += "; expires=" + oExpires.toGMTString();
}
if (sPath) {
sCookie += "; path=" + sPath;
}
if (sDomain) {
sCookie += "; domain=" + sDomain;
}
if (bSecure) …Run Code Online (Sandbox Code Playgroud) 我已就此事完成了所有研究.我知道Google认为这是毫无意义的,开发人员知道事实并非如此.我也知道没有已知的解决方法,但我知道我接近制作一个.用户DougW发布了此代码:
public class Utility {
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
}
Run Code Online (Sandbox Code Playgroud)
几乎完成了我的工作.但是当我尝试它时,我在listItem.measure(0,0)行得到一个NullPointer异常.listItem本身已初始化,但该方法仍会抛出异常.请告诉我如何解决这个问题.
这是我的代码:
public class ExpenseReportsActivity extends Activity {
private ListView lvReports;
private ExpenseReportListAdapter adapter; …Run Code Online (Sandbox Code Playgroud)