我开发了一个从另一个calss调用方法的方法。我创建了一个适配器,在里面我有一个覆盖getView()。我需要做的是从列表视图中获取值并显示为报告。我在同一个项目中的另一个活动中尝试了这个确切的代码。它完美地运作。但是,当我把它移动到另一个活动时,它不会给出任何错误。但是覆盖的方法不是调用。我应该怎么做才能让它运行?
public class ViewList extends ActionBarActivity {
ArrayAdapter<Units> unitsAdapterView;
ListView ViewAll;
List<Units> Unit = new ArrayList<Units>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_list);
ViewAll = (ListView) findViewById(R.id.viewList);
populateListView();
viewView();
}
private void viewView(){
Button m=(Button)findViewById(R.id.tryBtn);
m.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// startActivity(new Intent(getApplicationContext(),ViewList.class));
populateListView();
}
});
}
private void populateListView(){
unitsAdapterView =new UnitListAdaptorView();
ViewAll.setAdapter(unitsAdapterView);
// unitsAdapterView.getView();
}
private class UnitListAdaptorView extends ArrayAdapter<Units> {
public UnitListAdaptorView() {
super (ViewList.this, R.layout.list,Unit);
Log.i("","A");
// A();
}
@override
public View getView(int position, View view, ViewGroup parent) {
Log.i("","Resue connceted bulb 5");
if (view == null) {
view = getLayoutInflater().inflate(R.layout.list,parent ,false);
}
Units currentUnit = Unit.get(position);
TextView name = (TextView)view.findViewById(R.id.lblViewName);
name.setText(currentUnit.getName());
TextView bulb=(TextView) view.findViewById(R.id.lblViewBulb);
bulb.setText(currentUnit.getBulbNo());
TextView fan=(TextView) view.findViewById(R.id.lblViewFan);
fan.setText(currentUnit.getFanNo());
return view;
}
}
}
答案 0 :(得分:0)
在添加代码以从数据库获取所有现有数据后,问题得以解决。将以下代码添加到onCreate方法后,该函数现在正在运行!
DatabaseHandler dbHandler = new DatabaseHandler(getApplicationContext());
if (dbHandler.getUnitsCount() != 0)
Unit.addAll(dbHandler.getAllUnit());