2

I am trying to fill data from SQLite database to list view but my list view does not show anything.When i bedug the project list_collections varible fill of data Here is my sample code.

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

}

Once again I am trying to fill data By new array .I change this line

list_collections.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, name_collection_tbl_collection));

like this:

list_collections.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,new String[]{"5","4","3","2","1"}));

List view show all data. I don't understand problem because no error in logcat.Please give me hint or suggestion.

1 Answer 1

1

Change

fill_listView(word_search_collection.toString());

to

fill_listView(word_search_collection.getText().toString());

This will assign correct String to word variable and your program will work Thanks

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.