我正在尝试将数据从SQLite数据库填充到列表视图,但我的列表视图没有显示任何内容。当我将项目 list_collections 填充数据时 这是我的示例代码。
public class Collection_List_Activity extends ActionBarActivity {
private DB_Nabege_helper nabege_db = new DB_Nabege_helper(this);
private ListView list_collections;
private Button btn_Go_to_add_collection;
private int[] id_tbl_collection;
private String[] name_collection_tbl_collection;
private EditText word_search_collection;
private Cursor cur_collection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_collection__list_);
// =================================================================
list_collections = (ListView) findViewById(R.id.listView_collections);
TextView textViwempty = (TextView) findViewById(R.id.textview_no_subject);
list_collections.setEmptyView(textViwempty);
// =================================================================
go_to_add_collection();
fill_listView("");
word_search_collection = (EditText) findViewById(R.id.txt_search_collection);
list_collections.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Intent goTo_SubjectsActivity = new Intent(Collection_List_Activity.this, Subjects_Activity.class);
goTo_SubjectsActivity.putExtra("id_collection", id_tbl_collection[arg2]);
startActivity(goTo_SubjectsActivity);
}
});
word_search_collection.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() > 2) {
fill_listView(word_search_collection.toString());
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
private void go_to_add_collection() {
btn_Go_to_add_collection = (Button) findViewById(R.id.btn_new_collection);
btn_Go_to_add_collection.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent goToHelpActivity = new Intent(Collection_List_Activity.this, AddCollectionActivity.class);
startActivity(goToHelpActivity);
}
});
}
private void fill_listView(String word) {
int count_tbl_collection = nabege_db.count_collection(word);
id_tbl_collection = new int[count_tbl_collection];
name_collection_tbl_collection = new String[count_tbl_collection];
nabege_db.open();
cur_collection = nabege_db.display_collection(word);
try {
if (cur_collection != null)
for (int i = 0; i < count_tbl_collection; i++) {
cur_collection.moveToPosition(i);
id_tbl_collection[i] = cur_collection.getInt(0);
name_collection_tbl_collection[i] = cur_collection.getString(1);
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
Log.d("mylog", e.toString());
}
list_collections.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, name_collection_tbl_collection));
nabege_db.close();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.collection__list_, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onResume() {
super.onResume();
fill_listView(word_search_collection.toString());
}
// end class
}
我再次尝试通过新数组填充数据。我改变了这一行
list_collections.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, name_collection_tbl_collection));
像这样:
list_collections.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,new String[]{"5","4","3","2","1"}));
列表视图显示所有数据。我不明白问题,因为logcat没有错误。请给我提示或建议。
答案 0 :(得分:1)
更改
fill_listView(word_search_collection.toString());
到
fill_listView(word_search_collection.getText().toString());
这会将正确的字符串分配给word
变量,您的程序将正常工作
感谢