Java 标签慢/滞后
所以我有3个标签,我注意到我的标签很慢。如何提高选项卡的性能。这些选项卡具有列表视图 提高选项卡性能的方法有哪些 我认为问题是因为我有一个带有数据库的列表视图,它不断调用onActivityCreated,并且列表正在更新(我不确定) 这是我的代码,这个片段导致了问题Java 标签慢/滞后,java,android,Java,Android,所以我有3个标签,我注意到我的标签很慢。如何提高选项卡的性能。这些选项卡具有列表视图 提高选项卡性能的方法有哪些 我认为问题是因为我有一个带有数据库的列表视图,它不断调用onActivityCreated,并且列表正在更新(我不确定) 这是我的代码,这个片段导致了问题 public class Jornal extends Fragment { DataBasaJurnal dbJ; ListView listView; public static myAdapter myA; ImageBu
public class Jornal extends Fragment {
DataBasaJurnal dbJ;
ListView listView;
public static myAdapter myA;
ImageButton AddButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater
.inflate(R.layout.fragment_jornal, container, false);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
AddButton = (ImageButton) getActivity().findViewById(R.id.buttonAddJornal);
AddButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getActivity(), AddToJornal.class));
}
});
listView = (ListView) getView().findViewById(R.id.listViewJurnal);
dbJ = new DataBasaJurnal(getActivity());
dbJ.open();
new Handler().post(new Runnable() {
@Override
public void run() {
myA = new myAdapter(getActivity(), dbJ.getAllRowre());
listView.setAdapter(myA);
}
});
listView.setAdapter(myA);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
int mySuperInt = dbJ.countCases() - position;
Intent myIntent = new Intent(getActivity(),
JornalListViewClick.class);
myIntent.putExtra("intVariableName", mySuperInt);
listView.setEnabled(false);
startActivity(myIntent);
}
});
}
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
onActivityCreated(null);
System.out.println("ok");
listView.setEnabled(true);
}
class myAdapter extends CursorAdapter {
public myAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.single_row_jornal, parent,
false);
return retView;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView textViewMonth = (TextView) view
.findViewById(R.id.textViewDateWeek);
textViewMonth.setText(cursor.getString(cursor.getColumnIndex(cursor
.getColumnName(1))));
TextView textViewMonthDay = (TextView) view
.findViewById(R.id.textViewDateNum);
textViewMonthDay.setText(cursor.getString(cursor.getColumnIndex(cursor
.getColumnName(2))));
TextView textViewUserInput = (TextView) view
.findViewById(R.id.textViewUserInput);
textViewUserInput.setText(cursor.getString(cursor.getColumnIndex(cursor
.getColumnName(5))));
TextView textViewUserTitle = (TextView) view
.findViewById(R.id.textViewTitelFromDB);
textViewUserTitle.setText(cursor.getString(cursor.getColumnIndex(cursor
.getColumnName(6))));
TextView textViewLucid = (TextView) view
.findViewById(R.id.textViewhasLucid);
}
公共类Jornal扩展片段{
数据库法人数据库;
列表视图列表视图;
公共静态myA适配器;
图像按钮添加按钮;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
视图=充气机
.充气(R.layout.fragment_jornal,容器,假);
返回视图;
}
@凌驾
ActivityCreated上的公共无效(@Nullable Bundle savedinStateCState){
//TODO自动生成的方法存根
super.onActivityCreated(savedInstanceState);
AddButton=(ImageButton)getActivity().findViewById(R.id.ButtonAdJornal);
AddButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
startActivity(新的意图(getActivity(),AddToJornal.class));
}
});
listView=(listView)getView().findViewById(R.id.listViewJurnal);
dbJ=新的DatabaseJurnal(getActivity());
dbJ.open();
new Handler().post(new Runnable()){
@凌驾
公开募捐{
myA=新的myAdapter(getActivity(),dbJ.getAllRowre());
setAdapter(myA);
}
});
setAdapter(myA);
setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
//TODO自动生成的方法存根
int mySuperInt=dbJ.countCases()-位置;
Intent myIntent=新的Intent(getActivity(),
JornAlistViewClick.class);
myIntent.putExtra(“intVariableName”,mySuperInt);
setEnabled(false);
星触觉(myIntent);
}
});
}
@凌驾
public void onStart(){
//TODO自动生成的方法存根
super.onStart();
onActivityCreated(空);
System.out.println(“ok”);
setEnabled(true);
}
类myAdapter扩展了CursorAdapter{
公共myAdapter(上下文,游标c){
超级(上下文,c);
//TODO自动生成的构造函数存根
}
@凌驾
公共视图newView(上下文上下文、光标、视图组父对象){
LayoutInflater充气器=LayoutInflater.from(parent.getContext());
视图retView=充气机。充气(右布局。单列,主,
假);
返回视图;
}
@凌驾
公共void bindView(视图、上下文上下文、光标){
TextView textViewMonth=(TextView)视图
.findViewById(R.id.textViewDateWeek);
textViewMonth.setText(cursor.getString(cursor.getColumnIndex)(cursor
.getColumnName(1));
TextView textViewMonthDay=(TextView)视图
.findViewById(R.id.textViewDateNum);
textViewMonthDay.setText(cursor.getString(cursor.getColumnIndex(cursor
.getColumnName(2));
TextView textViewUserInput=(TextView)视图
.findViewById(R.id.textViewUserInput);
textViewUserInput.setText(cursor.getString(cursor.getColumnIndex(cursor
.getColumnName(5));
TextView textViewUserTitle=(TextView)视图
.findViewById(R.id.textViewTitelFromDB);
textViewUserTitle.setText(cursor.getString(cursor.getColumnIndex(cursor
.getColumnName(6));
TextView textViewLucid=(TextView)视图
.findViewById(R.id.textViewhasLucid);
}
}}使用Traceview并准确确定问题所在。我已经做了一些参考资料,我认为问题是因为我有一个带有数据库的列表视图,它不断地调用onActivityCreated(),
onActivityCreated()
是一个活动生命周期函数,不应该手动调用。用你自己的函数包装你的代码,然后在你想要的地方调用它。哦,我没想到。没有解决我的问题,不过还是谢谢你