Extending ActionBarActivity than ListActivity

I have a list that is populated from a database. I want to extend ActionBarActivity than ListActivity (but I still need it) in the following code because I have some functionality that uses Action Bar.
Java class:

public class ChooseHelp extends ListActivity {
ListView listHelp;
private final ArrayList<String> results = new ArrayList<>();
public final static String ID_HELP = "com.module.help._ID";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_help);

    listHelp = (ListView) findViewById(android.R.id.list);
    listHelp.setOnItemClickListener(onListClick);

    openAndQueryDatabase();
    displayResultList();
}

private void displayResultList() {
    setListAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, results));
    getListView().setTextFilterEnabled(true);
}

private final AdapterView.OnItemClickListener onListClick = new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent i = new Intent(ChooseHelp.this, HelpContent.class);

        i.putExtra(ID_HELP, String.valueOf(id));
        startActivity(i);
    }
};

private void openAndQueryDatabase() {
    try {
        DBHelper dbHelper = new DBHelper(this.getApplicationContext());
        SQLiteDatabase newDB = dbHelper.getWritableDatabase();
        String tableName = "tbl_Help";
        Cursor c = newDB.rawQuery("SELECT help FROM " +
                tableName +
                " LIMIT 18", null);

        if (c != null) {
            if (c.moveToFirst()) {
                do {
                    String HelpTopic = c.getString(c.getColumnIndex("help_topic"));
                    results.add(HelpTopic);
                } while (c.moveToNext());
            }
        }
    } catch (SQLiteException se) {
        Log.e(getClass().getSimpleName(), "Could not create or Open the database");
    }
}
}

      

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.module.example.ChooseHelp">

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:entries="@array/allTopics" />

</RelativeLayout>

      

I'm trying to do like this , but it doesn't fit my design

Please help me. Thank you very much.

+3


source to share


1 answer


You can create a layout with a listview like this:

activity_help.xml

<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_height="match_parent"
   android:layout_width="match_parent">
   <ListView
      android:id="@+id/listview"
      android:layout_height="match_parent"
      android:layout_width="match_parent">
</LinearLayout>

      



then go into your ActionBarActivity:

public class ChooseHelp extends ActionBarActivity {

private ArrayList<String> results;
ListView listView;
ArrayAdapter listAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_help);

    results = new ArrayList<>();
    openAndQueryDatabase();

    listView = (ListView)findViewById(R.id.listview);
    listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, results);
    listView.setAdapter(listAdapter);
    listView.setOnItemClickListener(onListClick);
}

      

+1


source







All Articles