How to customize timeline by using Fabric? - android

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>

Related

TextView doesn't display inside LinearLayout with RecycleView

I just started with Android and I have a problem. I have Recycler View with training list and in this view, I want to make header which will be in a static place regardless of the scrolling. However, the header or text view is not visible in the linear layout which is over the recycler view.
I have no idea how to solve my problem, anybody have any solutions or idea?
Below I present the individual elements of the code.
My recycler view:
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/training_recycler"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
My example linear layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".TrainingFragment">
<TextView
android:id="#+id/anything"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40sp"
android:text="#string/monia"/>
</LinearLayout>
Inside RecyclerView I have card view which looks:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="15dp"
card_view:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="#+id/info_text"
android:layout_marginLeft="15dp"
android:layout_marginBottom="15dp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textSize="20sp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
When I look to design window I see my textView with text but when I turn on emulator TextView wasn't display.
Thanks for any suggestions.
Edit:
TrainingMaterialFragment.java
public class TrainingMaterialFragment extends Fragment {
RecyclerView trainingRecycler;
public TrainingMaterialFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RecyclerView trainingRecycler = (RecyclerView)inflater.inflate(
R.layout.fragment_training_material, container, false);
int userNo = 1;
String[] nameArray = new String[0];
int[] idArray= new int[0];
try {
SQLiteOpenHelper myFitnessAppDatabaseHelper = new MyFitnessAppDatabaseHelper(inflater.getContext());
View view = inflater.inflate(R.layout.fragment_training, container, false);
SQLiteDatabase db = myFitnessAppDatabaseHelper.getReadableDatabase();
Cursor cursor = db.query("TRAINING_DIARY",
new String[]{"TRAINING_NAME", "DATE",
"DISTANCE","_id"},
"USER = ?",
new String[]{Integer.toString(userNo)},
null, null, null);
int count = cursor.getCount();
nameArray = new String[count];
idArray=new int[count];
if (cursor.moveToFirst()) {
String nameText = cursor.getString(0);
TextView name = (TextView) view.findViewById(R.id.training_name);
name.setText(nameText);
nameArray[0] = nameText;
idArray[0]=cursor.getInt(3);
int i = 1;
while(i < count) {
cursor.moveToNext();
nameText = cursor.getString(0);
nameArray[i] = nameText;
idArray[i]=cursor.getInt(3);
i++;
}
}
cursor.close();
db.close();
} catch (SQLiteException e) {
Toast toast = Toast.makeText(inflater.getContext(), "Baza danych jest niedostępna", Toast.LENGTH_SHORT);
toast.show();
}
TrainingAdapter adapter =
new TrainingAdapter(nameArray);
trainingRecycler.setAdapter(adapter);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
trainingRecycler.setLayoutManager(layoutManager);
adapter.setListener(new TrainingAdapter.Listener() {
public void onClick(int position) {
Intent intent = new Intent(getActivity(), TrainingDetailActivity.class);
intent.putExtra(TrainingDetailActivity.EXTRA_TRAINING, position);
getActivity().startActivity(intent);
}
});
return trainingRecycler;
}
}
Adapter
public class TrainingAdapter extends RecyclerView.Adapter<TrainingAdapter.ViewHolder> {
private String[] captions;
private Listener listener;
public TrainingAdapter(String[] captions){
this.captions = captions;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private CardView cardView;
public ViewHolder(CardView v) {
super(v);
cardView = v;
}
}
#Override
public TrainingAdapter.ViewHolder onCreateViewHolder(
ViewGroup parent, int viewType){
CardView cv = (CardView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.training_card_view, parent, false);
return new ViewHolder(cv);
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position){
CardView cardView = holder.cardView;
TextView textView = (TextView)cardView.findViewById(R.id.info_text);
textView.setText(captions[position]);
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (listener != null) {
listener.onClick(position);
}
}
});
}
#Override
public int getItemCount(){
return captions.length;
}
public static interface Listener {
public void onClick(int position);
}
public void setListener(Listener listener) {
this.listener = listener;
}
}
Try this way.
As you said LinearLayout in fragment_training.xml so put recylerview inside linearlayout.
Your fragment_training.xml code become like below
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".TrainingFragment">
<TextView
android:id="#+id/anything"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40sp"
android:text="#string/monia"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/training_recycler"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout/>
Then finally changes in java class.
Change this line
RecyclerView trainingRecycler = (RecyclerView)inflater.inflate(
R.layout.fragment_training_material, container, false);
To
View rootview = inflater.inflate(
R.layout.fragment_training, container, false);
.....
.....
return rootview;
Then you can find recylerview using rootview.
trainingRecycler = rootview.findViewById(R.id.training_recycler);
and make sure return rootview added..
Your OnCreateView method look like below
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview = inflater.inflate(
R.layout.fragment_training, container, false);
trainingRecycler = rootview.findViewById(R.id.findViewById);
.....
...
..
return rootview;
Correct me if i am wrong but you are not putting your Recycler inside the LinearLayout.Your LinearLayout should look like this and then you will be able to get what you want:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".TrainingFragment">
<TextView
android:id="#+id/anything"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40sp"
android:text="#string/monia"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/training_recycler"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout/>
I did this same thing in this project.
This is the link to the XML file
https://github.com/ShowYoungg/JournalApp/blob/master/app/src/main/res/layout/activity_main.xml. and the link to the java file;
https://github.com/ShowYoungg/JournalApp/blob/master/app/src/main/java/com/example/android/journalapp/MainActivity.java.
You can download the entire app and run to see that I put not just TexView but also button and others on the RecyclerView

CursorAdapter not displaying elements in ListView

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>

NullPointerException error in my ListView

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

ListView not showing items

I am trying to display a basic array of titles in a list view using fragments. And just not able to get the List items to show up. Wondering if anyone can shed some light. Also are there any good resources that help you understand Layouts well?
Here is my list_fragment.xml and the listfragment source file
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="#id/android:list"
android:drawSelectorOnTop="false"
android:layout_weight="1"
android:layout_height="0dp"
android:layout_width="fill_parent">
</ListView>
</LinearLayout>
public class PublicTradeListFragment extends ListFragment
{
private String trades[];
public PublicTradeListFragment()
{
trades = new String [] {
"Trade ?",
"Give up ?",
"Trade again?"
};
}
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
ListAdapter listAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,trades);
setListAdapter(listAdapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.list_fragment,container,false);
}
#Override
public void onListItemClick(ListView list, View v, int position, long id) {
Toast.makeText(getActivity(), getListView().getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
}
}
You can try this in "on Create View" then
"return super.onCreateView(inflater, container, savedInstanceState);"
Check this link: http://wptrafficanalyzer.in/blog/a-listfragment-application-in-android/

Cannot see list of contacts in ListFragment with LoaderManager

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"/>

Categories

Resources