DataBinding not Updating TextView when notifyPropertyChanged(BR.xx) is used - java

I have a working dataBinding for the functions being called from XML (ie."#{user.mUCL}"). But When i use notifyPropertyChanged(BR.mUCL) in any method or anywhere New values dont get updated, I have provided a sample issue below, where for-loop updated Text1 with values, but other Text2 fails to update with the same values "not-worked".
JavaFragment
public class FragmentTwo extends Fragment {
Update_observable updateObservable;
TextView fragment_quote_open_val;
public FragmentTwo() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Fragment2Binding fragmentTwoBinding = DataBindingUtil.inflate(inflater,R.layout.fragment2,null,false);
View view = fragmentTwoBinding.getRoot();
updateObservable = new Update_observable();
fragmentTwoBinding.setUser(updateObservable);
fragment_quote_open_val=(TextView)view.findViewById(R.id.fragment_quote_open_val);
bindingLoop();
return view;
}
#BindingAdapter({"mUCL"})
public static void runMe(TextView view, String message) {
if (message != null)
view.setText(message);
}
public void bindingLoop(){
final int[] k = {0};
final Update_observable ss=new Update_observable();
final Handler handler = new Handler();
final int delay = 3000; //milliseconds
handler.postDelayed(new Runnable(){
public void run(){
//do something
k[0]++;
fragment_quote_open_val.setText(String.valueOf(k[0]));
ss.setmUCL(String.valueOf(k[0]));
handler.postDelayed(this, delay);
}
}, delay);
}
}
BaseObservable
public class Update_observable extends BaseObservable {
public String mUCL= "not-worked";
#Bindable
public String getmUCL() {
return this.mUCL;
}
public void setmUCL(String mUCL) {
this.mUCL = mUCL;
notifyPropertyChanged(BR.mUCL);
}
}
XML
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="user"
type="com.journaldev.androidmvvmbasics.fragments.Update_observable"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff04"
android:orientation="vertical"
android:gravity="center"
tools:context="com.journaldev.androidmvvmbasics.fragments.FragmentTwo">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="#+id/fragment_quote_open_val"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:textColor="#000000"
android:textSize="16dp" />
<TextView
android:id="#+id/fragment_quote_ucl_val"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{user.mUCL}"
android:layout_margin="20dp"
bind:mUCL="#{user.mUCL}"
android:textColor="#000000"
android:textSize="16dp" />
</LinearLayout>
</layout>

kindly make sure you do DataBindingUtil.inflate in Base class.
Fragment1Binding fragmentOneBinding = DataBindingUtil.inflate(inflater,R.layout.fragment1,null,false);
View view = fragmentOneBinding.getRoot();
fragmentOneBinding.setUsermodel(new UserModel("XXXX","XXXX"));
or
ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this,
R.layout.activity_main);
activityMainBinding.setUser(new User());
activityMainBinding.executePendingBindings();
then in BaseObservable class do
#Bindable
public String mEmail= "....";
rebuild project, just to make sure #Bindable is done for sure
now call, as per your requirement.
notifyPropertyChanged(BR.xxx);

Related

How can i display images and videos (from app resources) repeatedly on the app without stick the UI

I need to do app like slideshow of images and vidoes from fiels from the device.
I have tried several projects - but no one can display videos.
What is the best way to do ot. All I need is to display, repeatedly, images and videos from the devices without the UI will stuck
There are some of the projects that i tried:
https://github.com/daimajia/AndroidImageSlider
https://github.com/marvinlabs/android-slideshow-widget
Thank you
I am showing a list of videos:
The Activity code:
public class VideoListActivity extends AppCompatActivity {
private ListView lvVideos;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_list);
lvVideos = (ListView)findViewById(R.id.lvVideos);
lvVideos.setAdapter(new VideosListAdapter(this));
}
}
The adapter code:
public class VideosListAdapter extends BaseAdapter {
private static LayoutInflater inflater = null;
private ArrayList<VideoData> listVideoData = new ArrayList<>();
private String[] creationDates = new String[0];
public VideosListAdapter(VideoListActivity videoListActivity) {
inflater = (LayoutInflater) videoListActivity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// here is a listVideoData and the creationDates initialisation code.
}
#Override
public int getCount() {
return listVideoData.size();
}
#Override
public Object getItem(int position) {
return listVideoData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (vi == null) {
vi = inflater.inflate(R.layout.row_video, null);
}
VideoData videoData = listVideoData.get(position);
VideoView videoView = (VideoView)vi.findViewById(R.id.videoView);
videoView.setVideoPath(videoData.getVideoFilePath());
TextView tvDate = (TextView) vi.findViewById(R.id.tvDate);
tvDate.setText(creationDates[position]);
TextView tvInfo = (TextView) vi.findViewById(R.id.tvPuppetInfo);
tvInfo.setText("Whatever you want" );
// must add this: -otherwise I don't know why, but is not working the click
RelativeLayout relativeLayout = (RelativeLayout)videoView.getParent();
relativeLayout.setClickable(true);
relativeLayout.setOnClickListener(rlClickListener);
return vi;
}
View.OnClickListener rlClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
VideoView videoView = (VideoView)v.findViewById(R.id.videoView);
videoView.start();
}
};
}
A code part in the row_video.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="vertical">
<!-- Yes, it is in pixel, hardcoded, because this is what is requested -->
<VideoView
android:id="#+id/videoView"
android:layout_width="1280px"
android:layout_height="720px"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/videoView"
android:layout_below="#+id/videoView"
android:layout_marginTop="5dp"
android:focusable="false"
android:text="25/12/2016 12:59:59"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<TextView
android:id="#+id/tvStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/videoView"
android:layout_below="#+id/videoView"
android:layout_marginTop="5dp"
android:drawableRight="#drawable/red_check"
android:text="whatever you want"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TextView
android:id="#+id/tvInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/videoView"
android:layout_centerHorizontal="true"
android:layout_marginTop="1dp"
android:gravity="center"
android:layout_toLeftOf="#+id/tvStatus"
android:layout_toRightOf="#+id/tvDate"
android:focusable="false"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
I hope it worth an upvote ! ( click )

Why is my RecyclerView not showing anything? / What happend if Recycler Adapter is set again after notifyDataSetChanged() called?

I am new to Android Programming and I've been trying to make a fragment which displays a list of items using RecyclerView. Everything seems fine and there is no error when I run the app.
Here is my code:
FriendsFragment.java
public class FriendsFragment extends android.support.v4.app.Fragment {
#Bind(R.id.friendList)
RecyclerView friendList;
RecyclerView.LayoutManager friendsManager;
FriendListAdapter friendListAdapter;
public FriendsFragment() {
// Required empty public constructor
}
#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_friends, container, false);
ButterKnife.bind(this, view);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
friendList.setHasFixedSize(true);
friendsManager = new LinearLayoutManager(view.getContext());
friendList.setLayoutManager(friendsManager);
friendListAdapter = new FriendListAdapter(new ArrayList<Friends>());
friendList.setAdapter(friendListAdapter);
test();
friendListAdapter.notifyDataSetChanged();
friendList.setAdapter(friendListAdapter);
super.onViewCreated(view, savedInstanceState);
}
private void test() {
Friends friend = new Friends();
friend.setName("Junjie Saitamaria");
friendListAdapter.addNewFriend(friend);
}
}
FriendListAdapter.java
public class FriendListAdapter extends RecyclerView.Adapter<FriendListAdapter.ViewHolderFriend> {
private List<Friends> friendList;
public FriendListAdapter(List<Friends> friends) {
if(friends != null) {
friendList = friends;
}
}
public void addNewFriend(Friends friend) {
friendList.add(0, friend);
}
#Override
public ViewHolderFriend onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_friend, parent, false);
ViewHolderFriend viewHolder = new ViewHolderFriend(view);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolderFriend holder, int position) {
Friends currentFriend = friendList.get(position);
holder.friendName.setText(currentFriend.getName());
}
#Override
public int getItemCount() {
return friendList.size();
}
static class ViewHolderFriend extends RecyclerView.ViewHolder {
#Bind(R.id.friend_avatar)
public ImageView friendThumbnail;
#Bind(R.id.friend_name)
public TextView friendName;
Context currentContext = null;
public ViewHolderFriend(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
this.currentContext = itemView.getContext();
}
}
}
fragment_friends.xml
<RelativeLayout 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="match_parent"
android:layout_margin="10dp"
tools:context="com.cebuinstituteoftechnology_university.citumessenger.FriendsFragment">
<android.support.v7.widget.RecyclerView
android:id="#+id/friendList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" >
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
list_item_friend.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="80dp"
android:id="#+id/friends_card"
android:elevation="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- icon -->
<ImageView
android:id="#+id/friend_avatar"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:contentDescription="icon"
android:src="#drawable/avatar"
/>
<!-- title -->
<TextView
android:id="#+id/friend_name"
android:layout_width= "wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_toRightOf="#+id/friend_avatar"
android:layout_alignBaseline="#+id/friend_avatar"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="Name" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Looks everything fine except this extra line friendList.setAdapter(friendListAdapter); in below code:
friendList.setAdapter(friendListAdapter);
test();
friendListAdapter.notifyDataSetChanged();
friendList.setAdapter(friendListAdapter);
Replace above 3 lines with this and see :
friendList.setAdapter(friendListAdapter);
test();
friendListAdapter.notifyDataSetChanged();
Remove following line from onViewCreated() Method
friendList.setAdapter(friendListAdapter);
other code is fine.

Android: Replacing activity view with fragment view

I've been searching for hours and hours on trying to understand the relationship between Fragments and Activities and I still cant get it right with my code.
I have a mainScreen class that loads actvity_main.xml. This main screen has a layout with a graph and sidebar etc.
I have a button that is on the side bar that is supposed to launch a sidebar fragment that has a checkbox listview in it so I can select data to show on my graph etc. But that fragment is not showing no matter what I do. It shows the "Bing~!" Toast but no other view is launched.
Perhaps I shouldn't be using a fragment? I don't want to start a new activity as the checkbox is supposed to interact with the graph and another fragment with a dynamic table in it and it will interact back and forth and so on.
I'm not too sure what to do here. I have about 3 weeks of Android experience so I'm not exactly knowledgeable about the whole shebang of it just yet. Really appreciate any ideas or help I can get. I am completely stumped and hence had to post my own question.
I really really appreciate any kind of help! Thanks!
mainScreen.java
public class mainScreen extends Activity {
private TextView text_display;
private Button button_list;
private Button button_table;
private String[] dataHolder;
public boolean listClicked = false;
public void loadData () {
//loaddata stuff here
}
public void createGraph () {
//graph create stuff here
}
public void buttonClick () {
button_list.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
sideFragment sf = new sideFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.side_fragment, sf);
ft.commit();
Toast.makeText(mainScreen.this, "Bing~!", Toast.LENGTH_SHORT).show();
}
}
);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = getIntent();
dataHolder = i.getStringArrayExtra("dataHolder");
button_list = (Button)findViewById(R.id.button_list);
loadData();
createGraph();
buttonClick();
}
}
activity_main.xml
<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"
tools:context=".mainScreen"
android:id="#+id/mainlayout"
android:orientation="horizontal"
android:weightSum="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="85dp"
android:layout_height="match_parent">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="85dp"
android:layout_height="85dp"
android:text="Select"
android:id="#+id/button_list" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="85dp"
android:layout_height="85dp"
android:text="Table"
android:id="#+id/button_table" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text=" "
android:id="#+id/text_display"
android:textSize="26dp"
android:layout_margin="5dp" />
<com.jjoe64.graphview.GraphView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/graph" />
</LinearLayout>
<fragment
android:name="xabre.mobileicip.sideFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/side_fragment" />
</LinearLayout>
sideFragment.java
public class sideFragment extends Fragment implements android.widget.CompoundButton.OnCheckedChangeListener {
ListView listviewFrag;
ArrayList<sideFrag> sideFragList;
sideFragAdapter sfAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.side_fragment, container, false);
listviewFrag = (ListView) view.findViewById(R.id.side_listview);
//displayList();
return view;
}
private void displayList() {
sideFragList = new ArrayList<sideFrag>();
sideFragList.add(new sideFrag("SPO2"));
sideFragList.add(new sideFrag("O2 Flow Rate"));
sideFragList.add(new sideFrag("Resp."));
sideFragList.add(new sideFrag("Cardiac Output"));
sideFragList.add(new sideFrag("Cardiac Index"));
sideFragList.add(new sideFrag("SVR"));
sideFragList.add(new sideFrag("Wedge Pressure"));
listviewFrag.setAdapter(sfAdapter);
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int pos = listviewFrag.getPositionForView(buttonView);
if (pos != ListView.INVALID_POSITION) {
sideFrag sf = sideFragList.get(pos);
sf.setSelected(isChecked);
Toast.makeText(getActivity(),"" + sf.getName(), Toast.LENGTH_SHORT).show();
//Toast.makeText(sideFragment.this, "Clicked on sideFrag: " + sf.getName() + ". State: is " + isChecked, Toast.LENGTH_SHORT).show();
}
}
}
side_fragment.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".sideFragment"
android:id="#id/side_fragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="HELLO WORLD"
android:id="#+id/helloTester"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp">
<ListView
android:id="#+id/side_listview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
</LinearLayout>
side_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<CheckBox android:id="#+id/check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCheckboxClicked"
android:layout_marginBottom="15dp" />
<TextView
android:id="#+id/check_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/check_box"
android:textStyle="bold"/>
</RelativeLayout>
sideFragAdapter.java
package xabre.mobileicip;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import java.util.List;
class sideFrag {
String name;
boolean selected = false;
public sideFrag(String name) {
super();
this.name = name;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class sideFragAdapter extends ArrayAdapter<sideFrag> implements CompoundButton.OnCheckedChangeListener {
private List<sideFrag> sideList;
private Context context;
public sideFragAdapter (List<sideFrag> sideList, Context context) {
super(context, R.layout.side_list, sideList);
this.sideList = sideList;
this.context = context;
}
private static class sideHolder {
public CheckBox check_box;
public TextView check_name;
/* public CheckBox check_O2FR;
public CheckBox check_Resp;
public CheckBox check_Carout;
public CheckBox check_Carind;
public CheckBox check_svr;
public CheckBox check_resprate;
public CheckBox check_Peep;
public CheckBox check_O2AF;
public CheckBox check_FIO2;
public CheckBox check_PO2;
public CheckBox check_HCO3;
public CheckBox check_Urea;
public CheckBox check_Potassium;
public CheckBox check_Sodium;
public CheckBox check_Creatinine;
public CheckBox check_FluidIn;
public CheckBox check_FluidOut;
*/
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
sideHolder holder = new sideHolder();
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.side_list, parent, false);
holder.check_box = (CheckBox) v.findViewById(R.id.checkbox);
holder.check_name = (TextView) v.findViewById(R.id.check_name);
holder.check_box.setOnCheckedChangeListener(this);
} else {
holder = (sideHolder) v.getTag();
}
sideFrag p = sideList.get(position);
holder.check_name.setText(p.getName());
holder.check_box.setChecked(p.isSelected());
holder.check_box.setTag(p);
return v;
}
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
}
}
You can only replace elements added from code. side_fragment is added in XML and as such is part of, let's say "read only" structure. You need to remove <fragment> element it from XML, and add it from code if you want to replace it later.

Custom adapter for listview using pasrequery

I am trying to create a demo app where the users enter a search and destination, the data will be queried from parse.com and then the matching result will be displayed in the next screen's listview. I have followed a few tutorials but for sure haven't been able to grasp the concept concretely enough. When I enter a search query, the progress bar shows up and then the application goes back into the previous activity. I can't understand where exactly things are going wrong.
This is my mainactivity class.
public class CarpoolingActivitySearch extends ActionBarActivity {
ListView listView;
ArrayList<Travellers> travellers;
CarpoolingAdapter adapter;
protected ProgressDialog proDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_carpooling_activity_search);
final EditText mSrc = (EditText)findViewById(R.id.carpooling_source);
final EditText mDst = (EditText)findViewById(R.id.destination);
Button mSubmitButton = (Button)findViewById(R.id.carpooling_submit);
mSubmitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String source = mSrc.getEditableText().toString();
String destination = mDst.getEditableText().toString();
listView = (ListView) findViewById(R.id.list);
ParseQuery<ParseObject> query = ParseQuery.getQuery("Carpooling");
query.whereEqualTo("Source", source); //assume you have a DonAcc column in your Country table
query.whereEqualTo("Destination", destination); //assume you have a DonAcc column in your Country table
startLoading();
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> parseObjects, ParseException e) {
if(e==null)
{
for(int i=0;i<parseObjects.size();i++)
{
Travellers travellers1 = new Travellers();
travellers1.setSource("Source");
travellers1.setDestination("Destination");
travellers.add(travellers1);
if(travellers.size() > 0)
{
adapter = new CarpoolingAdapter(getApplicationContext(),R.layout.activity_carpooling_activity_search,travellers);
listView.setAdapter(adapter);
} else {
AlertDialog.Builder popup = new AlertDialog.Builder(CarpoolingActivitySearch.this);
popup.setMessage("Seems our servers are busy. Try again in some time.");
popup.setPositiveButton("Back",null);
AlertDialog dialog = popup.create();
dialog.show();
}
}
stopLoading();
finish();
}
else {
stopLoading();
AlertDialog.Builder popup = new AlertDialog.Builder(CarpoolingActivitySearch.this);
popup.setMessage(e.getMessage());
popup.setPositiveButton("Back",null);
AlertDialog dialog = popup.create();
dialog.show();
}
}
});
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_carpooling_activity_search, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
protected void startLoading() {
proDialog = new ProgressDialog(this);
proDialog.setMessage("loading...");
proDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
proDialog.setCancelable(false);
proDialog.show();
}
protected void stopLoading() {
proDialog.dismiss();
proDialog = null;
}
}
I have created a custom adapter for this purpose.
public class CarpoolingAdapter extends ArrayAdapter<Travellers> {
ArrayList<Travellers> travellersArrayList;
LayoutInflater vi;
int Resource;
ViewHolder holder;
Context context;
public CarpoolingAdapter(Context context, int resource, ArrayList<Travellers> objects) {
super(context, resource, objects);
vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
travellersArrayList = objects;
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView== null) {
convertView = vi.inflate(Resource,null);
holder = new ViewHolder();
holder.mSource = (TextView)convertView.findViewById(R.id.textView);
holder.mDestination = (TextView)convertView.findViewById(R.id.textView2);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.mSource.setText(travellersArrayList.get(position).getSource());
holder.mDestination.setText(travellersArrayList.get(position).getDestination());
return convertView;
}
static class ViewHolder {
public TextView mSource;
public TextView mDestination;
}
}
And the travellers class with a default constructor and getter/setter methods.
public class Travellers {
private String Source;
private String Destination;
public Travellers() {
}
public Travellers(String source, String destination) {
super();
Source = source;
Destination = destination;
}
public String getSource() {
return Source;
}
public void setSource(String source) {
Source = source;
}
public String getDestination() {
return Destination;
}
public void setDestination(String destination) {
Destination = destination;
}
}
The main layout file named activity_carpooling_activity_search.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="com.example.android.myapplication.Carpooling.Carpooling">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/carpooling_source"
android:hint="Source"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/destination"
android:hint="Destination"
android:layout_below="#+id/carpooling_source"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="86dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="#+id/carpooling_submit"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/scrollView"
android:layout_toEndOf="#+id/carpooling_submit"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_below="#+id/carpooling_submit">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/list" />
</ScrollView>
</RelativeLayout>
And finally my list_item_deatil.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="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:layout_width="174dp"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView"
android:layout_weight="0.13" />
<TextView
android:layout_width="245dp"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView2"
android:layout_weight="0.13" />
</LinearLayout>
What should I modify to make it work appropriately? I am not getting any errors as such.
First of all, you have posted a very long code blocks where indentation is not good enough to read you code.
So what I understood, as your ParseException e is null that means there is no exception. Inside that if block, after the for loop you are calling finish() which is causing your current activity instance to be destroyed thus going to previous activity.
I hope this helps you.

SimpleCursorAdapter with ContentProvider; ListView empty

I am using SimpleCursorAdapter trying to display data which comes from a content provider. But the ListView stays empty. Have been searching for an answer for 3 days.
Here is my code:
IssuesFragment.java
public class IssuesFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
private SimpleCursorAdapter issuesAdapter;
private static final int ISSUES_LOADER = 0;
private static final String[] ISSUES_COLUMNS = {
DbContract.issues._ID,
DbContract.issues.ID,
DbContract.issues.THUMBNAIL,
DbContract.issues.TEXT
};
public static final int COL_ISSUES__ID = 0;
public static final int COL_ISSUES_ID = 1;
public static final int COL_ISSUES_THUMBNAIL = 2;
public static final int COL_ISSUES_TEXT = 3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public IssuesFragment() {
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
getLoaderManager().initLoader(ISSUES_LOADER, null, this);
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_issues, container, false);
ListView listView = (ListView) rootView.findViewById(R.id.issues_listview);
getLoaderManager().initLoader(ISSUES_LOADER, null, this);
issuesAdapter = new SimpleCursorAdapter (
getActivity(),
R.layout.issues_list_item,
null,
ISSUES_COLUMNS,
new int[]{
R.id.issues_list__id,
R.id.issues_list_id,
R.id.issues_list_thumbnail,
R.id.issues_list_text,
},
0
);
listView.setAdapter(issuesAdapter);
return rootView;
}
#Override
public void onResume() {
super.onResume();
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Uri issuesEntry = DbContract.issues.buildIssuesUri();
return new CursorLoader(
getActivity(),
issuesEntry,
ISSUES_COLUMNS,
null,
null,
null
);
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
issuesAdapter.swapCursor(data);
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
issuesAdapter.swapCursor(null);
}
}
Here are the corresponding Layout files:
issues_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="horizontal"
android:padding="16dp">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/issues_list__id" />
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/issues_list_id" />
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/issues_list_thumbnail" />
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/issues_list_text" />
</LinearLayout>
fragment_issues.xml
<LinearLayout
....
/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/issues_listview"
android:layout_weight="1" />
<TextView
android:id="#+id/emptyListElem"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Keine Ausgaben vorhanden." />
</LinearLayout>
My cursor gets filled with data. I verified that by printing DatabaseUtils.dumpCursorToString(cursor) within the onLoadFinished Method. But the data doesn't get from there to the UI.
I found out what the problem was. The element emptyListElem Textview from fragment_issues.xml was replacing the list element, even though i had
View empty = getActivity().findViewById(R.id.emptyListElem);
listView.setEmptyView(empty);
in my onCreateView method which AFAIK should make the emptyListElem show up only if there is no content to fill the list. Seems like i was mistaken. Or maybe i had the code in the wrong place.
edit: i removed the Element emptyListElem and it works now.

Categories