TestAdapter:
public class TestAdapter extends Activity
{
ArrayList<Horario> array = new ArrayList<Horario>();
protected static final String TAG = "DataAdapter";
private final Context mContext;
private SQLiteDatabase mDb;
private DataBaseHelper mDbHelper;
public TestAdapter(Context context)
{
this.mContext = context;
mDbHelper = new DataBaseHelper(mContext);
}
public TestAdapter createDatabase() throws SQLException
{
try
{
mDbHelper.createDataBase();
}
catch (IOException mIOException)
{
Log.e(TAG, mIOException.toString() + " UnableToCreateDatabase");
throw new Error("UnableToCreateDatabase");
}
return this;
}
public TestAdapter open() throws SQLException
{
try
{
mDbHelper.openDataBase();
mDbHelper.close();
mDb = mDbHelper.getReadableDatabase();
}
catch (SQLException mSQLException)
{
Log.e(TAG, "open >>"+ mSQLException.toString());
throw mSQLException;
}
return this;
}
public void close()
{
mDbHelper.close();
}
ArrayList<Horario> getorigem(){
String query = "select origem, destino From horarios";
Cursor cursor = mDb.rawQuery(query, new String[]{});
ArrayList<Horario> array = new ArrayList<Horario>();
while (cursor.moveToNext()) {
String _origem = cursor.getString(cursor.getColumnIndex("origem"));
String _destino = cursor.getString(cursor.getColumnIndex("destino"));
array.add(new Horario(_origem, _destino));
}
return array;
}
}
MainActivity(和MyListAdapter):
public class MainActivity extends Activity {
ArrayList<Horario> array = new ArrayList<Horario>();
SQLiteDatabase mDb;
Button bt_search;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_search = (Button) findViewById(R.id.btnsearch);
TestAdapter mDbHelper = new TestAdapter(getBaseContext());
mDbHelper.createDatabase();
bt_search.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
search(v);
}
});
}
public void search(View v) {
setContentView(R.layout.list_view);
TestAdapter mDbHelper = new TestAdapter(getBaseContext());
mDbHelper.open();
mDbHelper.getorigem();
ArrayAdapter<Horario> adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.list_view);
list.setAdapter(adapter);
mDbHelper.close();
}
public void populateListView() {
// TODO Auto-generated method stub
MyListAdapter adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.list_view);
list.setAdapter(adapter);
}
private class MyListAdapter extends ArrayAdapter<Horario>{
public MyListAdapter() {
super(MainActivity.this, R.layout.item_view, array);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//Make sure we have view to work with
View itemView = convertView;
if(itemView == null){
itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);
}
//Find the car to work with.
Horario currentHorario = array.get(position);
//Fill the view
// Origem:
TextView makeText = (TextView) itemView.findViewById(R.id.view_origem);
makeText.setText(currentHorario.getOrigem());
// Destino:
TextView ConditionText = (TextView) itemView.findViewById(R.id.view_destino);
ConditionText.setText(currentHorario.getDestino());
return itemView;
}
}
}
我尝试使用item_view.xml中的布局填充名为list_view的ListView。 当我点击按钮搜索以获得结果时,不显示任何错误,但list_view显示为空
答案 0 :(得分:0)
试试这个
public class MyListAdapter extends BaseAdapter{
public MyListAdapter(yourActivity, yourlayout, yourArray) {
//initialize here
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//Make sure we have view to work with
View itemView = convertView;
if(itemView == null){
itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);
}
//Find the car to work with.
Horario currentHorario = array.get(position);
//Fill the view
// Origem:
TextView makeText = (TextView) itemView.findViewById(R.id.view_origem);
makeText.setText(currentHorario.getOrigem());
// Destino:
TextView ConditionText = (TextView) itemView.findViewById(R.id.view_destino);
ConditionText.setText(currentHorario.getDestino());
return itemView;
}
@Override
public int getCount() {
return yourArray.size();
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
}
请确保您拥有:
@Override
public int getCount() {
return yourArray.size();
}
这部分代码会返回列表中的单元格数
答案 1 :(得分:0)
onCreate(Bundle b)
MainActivity
的位置
在MainActivity
onCreate(Bundle b)
方法中,您必须在populateListView()
之后致电setContentView(R.layout.ur_layout)
而不了解TestAdapter
为什么会延伸Activity
1}} 强>
编辑:你array
为空你忘记分配值
array = mDbHelper.getorigem();
你不需要这个populateListView()
方法作为你already referred the ListView in search(View v)
public void populateListView() {
// TODO Auto-generated method stub
MyListAdapter adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.list_view);
list.setAdapter(adapter);
}
MyListAdapter adapter = new MyListAdapter();
在search(View v)
尝试使用ViewHolder Pattern进行平滑滚动
答案 2 :(得分:0)
每次更新arraylist时,都必须告诉适配器数据已更改。 在列表中设置数据后,在适配器上调用 notifyDataSetChanged()。
详细信息http://developer.android.com/reference/android/widget/ArrayAdapter.html#notifyDataSetChanged()