android-actionbar in a ListActivity - android

I'm trying to make the android-actionbar plugin working in my ListActivity,
Here's my Class :
public class DisciplinesController extends ListActivity {
private DisciplinesModel dbHelper;
private Cursor cursor;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_action);
ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
dbHelper = new DisciplinesModel(this);
dbHelper.open();
if (dbHelper.fetchDisciplineCount() == 0) {
dbHelper.fetch();
}
cursor = dbHelper.fetchAllDisciplines();
startManagingCursor(cursor);
ListAdapter adapter = new SimpleCursorAdapter(this, R.id.list,
cursor, new String[] { "name" }, new int[] { R.id.discipline_name});
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);
}
}
and my XML layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.markupartist.android.widget.ActionBar
android:id="#+id/actionbar" style="#style/ActionBar" />
<LinearLayout android:id="#+id/list" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/discipline_name"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical" android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight" />
</LinearLayout>
</LinearLayout>
I always have this exception :
Your content must have a ListView whose id attribute is 'android.R.id.list'
Do you have an idea ??
Thanks !

In your XML layout you must include a ListView element with the #android:id/list id.
<ListView
android:id="#android:id/list"
...
/>
Basically, you need to rip out the section LinearLayout into a new file (e.g. list_item.xml). Replace this with a listview in your list_action.xml
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
andrdoi:layout_height="wrap_content" />
Then change R.id.list in your SimpleCursorAdapter constructor to R.layout.list_item.

Related

How to use onitemClickListener with Fragments?

I am trying to implement onItemClickListener in Fragment class but unfortunately it is not working properly... here is the source code.. please let me know what is the error??
NetworkDetailsFragment.java(My Fragment class)
public class NetworkDetailsFragment extends Fragment implements AdapterView.OnItemClickListener{
private ListView listView;
private View networkDetailsView;
private QOSNetworkDetailsAdapter qosNetworkDetailsAdapter;
private QOSNetworkDetailsDatabaseHelper qosNetworkDetailsDatabaseHelper;
private SimpleCursorAdapter simpleCursorAdapter;
private String LOG_TAG = NetworkDetailsFragment.class.getSimpleName();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(LOG_TAG, "Inside onCreateView() method");
networkDetailsView = inflater.inflate(R.layout.network_details, container, false);
listView = (ListView) networkDetailsView.findViewById(R.id.list_view);
qosNetworkDetailsDatabaseHelper = new QOSNetworkDetailsDatabaseHelper(getActivity());
Cursor cursor = qosNetworkDetailsDatabaseHelper.getRecord();
String[] columns = {QOSNetworkDetailsDatabaseConstants.COLUMN_NETWORK_TYPE,
QOSNetworkDetailsDatabaseConstants.COLUMN_NETWORK_STATUS,
QOSNetworkDetailsDatabaseConstants.COLUMN_LATITUDE,
QOSNetworkDetailsDatabaseConstants.COLUMN_LONGITUDE,
QOSNetworkDetailsDatabaseConstants.COLUMN_TIME};
int[] to = new int[]{
R.id.networkType,
R.id.networkStatus,
R.id.latitudeAndLongitude,
R.id.date
};
/*SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(
this, R.layout.activity_network_info,
cursor,
columns,
to,
0);*/
simpleCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.activity_network_info, cursor, columns, to, 0);
qosNetworkDetailsAdapter = new QOSNetworkDetailsAdapter(getActivity().getApplicationContext(), qosNetworkDetailsDatabaseHelper.getRecord(), false);
listView.setAdapter(qosNetworkDetailsAdapter);
return networkDetailsView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listView.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Cursor cursor = (Cursor) parent.getItemAtPosition(position);
String countryCode =
cursor.getString(cursor.getColumnIndexOrThrow("code"));
Toast.makeText(getActivity().getApplicationContext(),
countryCode, Toast.LENGTH_SHORT).show();
}
}
network_details.xml(i.e list_view.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:contentDescription="#string/network_details_fragment_string"
android:descendantFocusability="blocksDescendants" >
<ListView
android:id="#+id/list_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
activity_network_info.xml(list items)
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:descendantFocusability="blocksDescendants">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/black"
>
<TextView
android:id="#+id/networkStatus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#drawable/white"
android:textSize="15sp" />
<TextView
android:id="#+id/networkType"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#drawable/white"
android:textSize="15sp" />
<TextView
android:id="#+id/latitudeAndLongitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#drawable/lightblue"
android:textSize="15sp" />
<TextView
android:id="#+id/date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#drawable/lightblue"
android:textSize="15sp" />
</LinearLayout>
</ScrollView>
List is getting populated to the screen... but onItemClickListener is not working in this code... please let me know what is the error.. Thanks
Try to extend FragmentActivity instead of Fragment...
If you use a ListFragment, your ListView Id in layout must be android.R.id.list
like that:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="#android:id/list" />

The ListView does not scroll. How do I fix it?

I have a ListView inside a Linearlayout.
The listview has a custom cursoradapter.
Everything works fine, except the ListView does not scroll.
Any suggestion more than welcome!! Thanks. maurizio
Here is the XML of the MainActivity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Aggiungi" />
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Here is the relevant piece of java code.
public class MainActivity extends Activity {
private DatabaseHelper db=null;
private Cursor tabellaCursor=null;
private ListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int[] to = new int[] {R.id.name_entry};
db = new DatabaseHelper(this);
tabellaCursor=db.getWritableDatabase().rawQuery("SELECT _id, colonna1, colo nna2, colonna3 FROM tabella ORDER BY _id", null);
ListAdapter adapter=new MyAdapter(this, R.layout.list_example_entry, tabe llaCursor, new String[]{"colonna1"},to);
ListView lt = (ListView)findViewById(R.id.list);
lt.setAdapter(adapter);
Button addbtn=(Button)findViewById(R.id.buttonAdd);
addbtn.setOnClickListener(new OnClickListener()
{public void onClick(View v){add(); }
});
}
Your ListViews Layout param is "wrap_content". it will expand as you add new items to litview. therefore it won't scroll. If you set it to "match_parent" it will sure start scrolling.And dont forget that a view does scroll only if its contents(childs view what ever)
size is bigger than it.
Add more list items that exceeds height. then you can scroll.
Please use below code, it will solve your problem.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Aggiungi" />
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/buttonAdd"/>
</RelativeLayout>

unable to get values in list view by using simple cursor adapter android?

In my android app i want to display the list which is retrieving the values from DB .I m using simple cursor adapter to do so,but list view is empty.Here is my code.my list view have two rows. How can i use simple cursor adapter in app.what i m doing wrong.Please suggest.
Thanks in advance.
//list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:layout_width="fill_parent" android:id="#android:id/android:list"
android:layout_height="fill_parent" android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"></ListView>
</RelativeLayout>
// textview.xml-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:weightSum="1">
<TextView android:id="#+id/column1" android:background="#drawable/mid"
android:layout_height="30dp"
android:layout_width="fill_parent" android:layout_weight="0.03"></TextView>
<TextView android:layout_height="wrap_content"
android:text="TextView" android:id="#+id/column2" android:background="#drawable/mid"
android:layout_weight="0.03" android:layout_width="fill_parent"></TextView>
</LinearLayout>
public class AddFood extends ListActivity{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.listxml);
DataBaseHelper db = null;
try {
db = new DataBaseHelper(this);
} catch (IOException e) {
e.printStackTrace();
}
SQLiteDatabase database= db.getReadableDatabase();
Cursor c=database.query("food_list", new String[]{"_id","food_items"}, null, null, null,null,null);
startManagingCursor(c);
String[] displayfield=new String[]{"_id","food_items"};
int[] displayViews = new int[] {R.id.column1,R.id.column2};
SimpleCursorAdapter mAdapter=new SimpleCursorAdapter(getApplicationContext(),R.layout.textview, c,displayfield , displayViews);
this.setListAdapter(mAdapter);
c.close();
}
}
For list.xml use:
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></ListView>
In class file:
do not use c.close();
and use:
SimpleCursorAdapter mAdapter=new SimpleCursorAdapter(this,R.layout.textview, c,displayfield , displayViews);
setListAdapter(mAdapter);

Item and SubItem in ListView

I want to show Item and corresponding subitem in list view using Arrayadapter and android.R.layout.simple_list_item_2. I am able to list items with following code
XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingLeft="8dp" >
<ListView android:id="#android:id/list" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView android:id="#android:id/empty" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Empty set" />
java file:
public class Lists extends ListActivity {
String[] listItems = {"Item1", "Item2 ", "Item3", "Item4"};
String[] SubItems = {"SubItem1", "SubItem2", "SubItem3", "SubItem4"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListAdapter adp = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
setListAdapter(adp);
}
}

Trying to use a listview in .xml with a cursor but without a ListActivity

I'm stumped as to how to properly implement an adapter that can pull a column of data from my sqlite database and add it to a listview (based on the android-provided multiple checkbox). All of the existing examples use the following:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] myList = new String[] {"Hello","World","Foo","Bar"};
ListView lv = new ListView(this);
lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,myList));
setContentView(lv);
}
However, I don't want a string. Here's my code:
public class ListGroups extends Activity {
/** Called when the activity is first created. */
private ExpensesDbAdapter mDBHelper;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
ListView lv = (ListView)findViewById(R.id.list1);
mDBHelper = new ExpensesDbAdapter(ListGroups.this);
mDBHelper.open();
Cursor c = mDBHelper.fetchAllGroups();
startManagingCursor(c);
String[] from = new String[] {ExpensesDbAdapter.KEY_GROUPNAME};
int[] to = new int[] {R.id.groupname};
ListAdapter ladapter = new SimpleCursorAdapter(ListGroups.this, R.layout.grouprow, c, from, to);
lv.setAdapter(ladapter);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
}
Here's the view.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<TextView android:id="#+id/header01"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Select Groups"/>
<ListView
android:id="#+id/list1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/footerbutton01"
android:text="Get Expense Reports" />
</LinearLayout>
And, because the SimpleCursorAdapter asks for it, here's the grouprow.xml:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/groupname"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="6dip"
android:paddingRight="6dip"/>
If I use a ListActivity and don't invoke the layout on Creation, I can get it to work, but I need the wrappers around that ListView, and I can't make the .addHeader and .addFooter methods work for me. Any help on creating the proper adapter would be appeciated.
Answered it with some help. Several things were wrong, mostly in the list.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<TextView android:id="#+id/header01"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Select Groups"/>
<ListView
android:id="#android:id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_around"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/footerbutton01"
android:text="Get Expense Reports" />
</LinearLayout>
This allows you to tie the ListView to that element in the xml. You don't have a name for it, but you can use getListView() in the onCreate method to access it.

Categories

Resources