I am trying to make a contacts list view fragment. Here is the code:
public class ContactsListFragment extends RoboListFragment implements
LoaderManager.LoaderCallbacks<Cursor> {
private static final int CONTACTS_LIST_LOADER = 0x01;
CursorAdapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("onCreate", "called");
String[] fromColumns = {ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.ADDRESS};
int[] toViews = { R.id.contact_list_item_id,
R.id.contact_list_item_name,
R.id.contact_list_item_email};
getLoaderManager().initLoader(CONTACTS_LIST_LOADER, null, this);
adapter = new SimpleCursorAdapter(
getActivity().getApplicationContext(), R.layout.contacts_list_item,
null, fromColumns, toViews,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
setListAdapter(adapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("onCreateView", "called");
View rootView = inflater.inflate(R.layout.contacts_list_fragment, container, false);
return rootView;
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Log.d("onCreateLoader", "called");
return new CursorLoader(getActivity(),
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, null, null, null);
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
Log.d("onLoadFinished", "called");
adapter.swapCursor(cursor);
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
Log.d("onLoaderReset", "called");
adapter.swapCursor(null);
}
}
but although all of the logs are printed, and there are no Exceptions, I see nothing on the screen.
The fragment layout:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.Fragments.ContactsListFragment">
<ListView
android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/contact_list_empty"/>
</RelativeLayout>
The list item layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/contact_list_item_id"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/contact_list_item_name"
android:layout_below="#id/contact_list_item_id"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/contact_list_item_email"
android:layout_below="#id/contact_list_item_name"/>
</RelativeLayout>
I am at a bit of a loss here. My parent activity includes the fragment thus:
<fragment
android:id="#+id/contact_list_fragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="com.example.Fragments.ContactsListFragment"
tools:layout="#layout/contacts_list_fragment"
android:layout_below="#id/fakeView"/>
Related
I am trying to get a column of SQLITE database to a listview using a SimpleCursorColumn. I am getting the accurate number of rows in the list view, but the text is not appearing in the list. I printed out the content of the cursor and it contains the correct elements.
Why is the text not showing up in the list? Surely it must be easy that I am being stupid about.
public class Tab3Fragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
private static final int LOADER_ID = 42;
private CursorAdapter _adapter;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab3_frag, container, false);
ListView lv = view.findViewById(R.id.student_view);
_adapter = new SimpleCursorAdapter(getContext(),
R.layout.container_list_item_view, null,
new String[] { Contract.Student_Table.STUDENTS_COLUMN_NAME},
new int[] { R.id.list_item });
lv.setAdapter(_adapter);
getLoaderManager().initLoader(LOADER_ID, null, this);
return view;
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
if (id != LOADER_ID) {
return null;
}
return new CursorLoader(getContext(),
Contract.Student_Table.CONTENT_URI,
new String[] { Contract.Student_Table.STUDENTS_COLUMN_ID, Contract.Student_Table.STUDENTS_COLUMN_NAME}, null, null,
null);
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
_adapter.swapCursor(data);
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
_adapter.swapCursor(null);
}
}
tab3_frag.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="#c2f6ff"
android:orientation="vertical">
<ListView
android:id="#+id/simple_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="#+id/student_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#android:color/black" />
</LinearLayout>
Better use loader. Try below example:
public class LanguageListActivity extends ListActivity implements
LoaderCallbacks<Cursor> {
private static final int LOADER_ID = 42;
private CursorAdapter _adapter;
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.container_list);
_adapter = new SimpleCursorAdapter(this,
R.layout.container_list_item_view, null,
new String[] { DatabaseConstants.COL_LANG_NAME },
new int[] { R.id.list_item });
setListAdapter(_adapter);
getLoaderManager().initLoader(LOADER_ID, null, this);
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
if (id != LOADER_ID) {
return null;
}
return new CursorLoader(LanguageListActivity.this,
LanguageContentProvider.CONTENT_URI,
new String[] { DatabaseConstants.COL_LANG_ID, DatabaseConstants.COL_LANG_NAME }, null, null,
null);
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
_adapter.swapCursor(data);
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
_adapter.swapCursor(null);
}
}
container_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
container_list_item_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="fill_parent"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="2dip" >
<TextView
android:id="#+id/list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dip" />
</LinearLayout>
I'm trying to develop a twitter client app by using Fabric.I have embedded home and user timelines successfully. But i want to customize them according to the my needs such as showing status on cardview.How can i do that ?
final TweetViewFetchAdapter adapter =
new TweetViewFetchAdapter<CompactTweetView>(
getActivity());
public TimelineFragment(){}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_timeline, container, false);
final ListView lv = (ListView)view.findViewById(R.id.hometimeline_list);
StatusesService statusesService = Twitter.getApiClient().getStatusesService();
statusesService.homeTimeline(200, null, null, null, null, null, null, new Callback<List<Tweet>>() {
#Override
public void success(Result<List<Tweet>> result) {
adapter.setTweets(result.data);
lv.setAdapter(adapter);
}
#Override
public void failure(TwitterException e) {
}
});
return view;
}
hometimeline.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"/>
<ListView android:id="#+id/hometimeline_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</LinearLayout>
I've been creating News application. I used this tutorial to create good-looking Navigation Drawer. It uses fragments for every category in Navigation List. I will be using provided API to get news (in the JSON format), and i need customized List View to show the list of news. When you click list item, it will open new view to show details of news. I've found several tutorials, but none of them worked for fragment. Please, tell me way to do it.
I'm new to Android Programming. This is my first project, though it is too hard for being first project. But i have to do it. Please, help!
Thanks beforehand!
Assuming you know how to create an adapter and a custom row layout from a tutorial that works for activities. Minor modifications will help you using fragments.
Your fragment should look like this:
public class NewFragment extends Fragment{
CustomAdapter listAdapter;
ListView listView;
ArrayList<Data> yourData; // Should be filled with data from your JSONParser;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
/*
* Use your custom adapter. in this example the adapter need
* the context and an array that contains your data
*/
listAdapter = new CustomAdapter(getActivity(),yourData);
...
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.new_fragment, container, false);
listView = (LsitView)view.findViewById(R.id.your_list);
listView.setAdapter(listAdapter);
...
}
}
You can use for this ListFragment:
Here is a fully customized example:
activity_main.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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/frameLayout"/>
</RelativeLayout>
mylist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>
myitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:id="#+id/txt_mytext" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment fr = mListFragment.newInstance();
FragmentManager fm = getFragmentManager();
fm.beginTransaction().add(R.id.frameLayout, fr, "tag").commit();
}
}
mListFragment.java
public class mListFragment extends ListFragment {
public static mListFragment newInstance() {
mListFragment f = new mListFragment();
//Bundle args = new Bundle();
//f.setArguments(args);
return f;
}
public mListFragment() {}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
String[] strings = {"a", "b", "c"};
ArrayAdapter<String> adapter = new LstAdapter(
getActivity(), R.layout.myitem,
strings);
setListAdapter(adapter);
return inflater.inflate(R.layout.mylist, null);
}
public class LstAdapter extends ArrayAdapter<String> {
private String[] mArray;
public LstAdapter(Context context, int textViewResourceId, String[] mList) {
super(context, textViewResourceId, mList);
mArray = mList;
}
#Override
public int getCount() {
return mArray.length;
}
#Override
public String getItem(int position) {
return mArray[position];
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.myitem, null);
}
TextView tvName = (TextView) v.findViewById(R.id.txt_mytext);
tvName.setText(mArray[position]);
return v;
}
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.d("123", position + "");
super.onListItemClick(l, v, position, id);
}
}
I have this simple program that populates data using Content Provider and using Custom cursor adapter. My problem is that the item of the listfragment is not clickable even though the item has a "android:textIsSelectable="true". I have done a debugging and i could see that onListItemClick is not called.
These are my XML
activity_item_list.xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dream , Believe and Achieve!"
android:id="#+id/achieveButton"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/believe"
/>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="#+id/item_list"
android:name="com.toksis.lawofattraction.ItemListFragment" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_marginLeft="16dp"
android:layout_marginRight="16dp" tools:context=".ItemListActivity"
tools:layout="#layout/fragment_item_detail"
android:descendantFocusability="blocksDescendants"
/>
</LinearLayout>
Fragment_item_detail.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="#+id/item_detail"
style="?android:attr/textAppearanceLarge" android:layout_width="match_parent"
android:layout_height="match_parent" android:padding="16dp" android:textIsSelectable="true"
tools:context=".ItemDetailFragment"
/>
ItemlistFragment Oncreate snippet.
// Create an array to specify the fields to display in the list
// (only TITLE)
String[] from = new String[] { WishContentProvider.COLUMN_WISH };
// and an array of the fields to bind those fields to (in this
// case, just text1)
int[] to = new int[] { R.id.item_detail };
// Now create a simple cursor adapter and set it to display
/* mAdapter = new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_1, null, from, to, 0); */
mAdapter = new WishCursorAdaptor(getActivity(),
getActivity().getContentResolver()
.query(WishContentProvider.CONTENT_URI, from, null, null, null),
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
setHasOptionsMenu(true);
setListAdapter(mAdapter);
getLoaderManager().initLoader(0, null, this);
Cursor adapter :WishCursorAdapter
public class WishCursorAdaptor extends CursorAdapter {
private LayoutInflater inflater;
public WishCursorAdaptor(Context context, Cursor c,int autoRequry) {
super(context, c,autoRequry);
inflater = LayoutInflater.from(context);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(R.layout.fragment_item_detail,parent,false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView wish = (TextView)view.findViewById(R.id.item_detail);
wish.setText(cursor.getString(cursor.getColumnIndex("wish")));
}
}
This is how.
public class ItemListFragment extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.hello, container, false);
return v;
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
//Do your stuff..
}
}
Reference
I have a fragment which I want to display a ListView
here's a portion of my code
public class ExpensesDaily extends Fragment{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
lvExpenses = (ListView) getActivity().findViewById(android.R.id.list);
Cursor expensesListCursor;
DbControl dbc = new DbControl(getActivity());
try {
dbc.open();
expensesListCursor = dbc.listExpenses();
String[] from = new String[] {"description", "cost" };
ListAdapter adapter = new SimpleCursorAdapter(getActivity(), R.layout.list_expenses, expensesListCursor, from, new int[] { R.id.tvDescription, R.id.tvCost },0);
lvExpenses.setAdapter(adapter);
}catch (SQLException e){
}
dbc.close();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.expenses_daily, container, false);
return rootView;
}
}
** DbControl class**
public class DbControl {
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
public DbControl(Context context) {
dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public Cursor listExpenses(){
Cursor cursor;
cursor = database.query(MySQLiteHelper.TABLE_EXPENSES, new String[] {"id _id", "description, cost"}, "forDate='" + date + "'", null, null, null, "id asc");
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
expenses_daily.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/ll_expensesDaily"
android:baselineAligned="false">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
</LinearLayout>
list_expenses.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="7dip">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:id="#+id/tvDescription" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:id="#+id/tvCost" />
</LinearLayout>
</LinearLayout>
but everytime I load the fragment, it will display this error
FATAL EXCEPTION: main java.lang.NullPointerException at com.code.imin.mymoneymanaged.ExpensesDaily.onCreate(ExpensesDaily.java:60)
which points to the line
lvExpenses.setAdapter(adapter) in class ExpensesDaily.
lvExpenses = (ListView) getActivity().findViewById(android.R.id.list);
You can't call getActivity().findViewById on onCreate because the view was not created yet. Change that logic to onActivityCreated