TextView not appearing. Blank CardViews - java

I'm currently working on a basic shopping cart project where I select items from a RecyclerView and add them to a custom ArrayList which should then be seen in EstimateActivity in a new Recycleview. I'm able to add custom objects to the "cartList" and pass it through intent(through parseable). When I click the button to go to the Estimate Activity all that appears in the new RecyclerView are an appropriate amount of CardViews without any of the TextViews(code and name). Am I wrong to believe that the intent is being passed correctly due to the fact that the correct amount of CardViews are found in the EstimateActivity's RecyclerView? Since everything else is working I am having troubles figuring out where the bug lies. Any help with how to figure out bugs like this on my own would also be appreciated.
Here is how I'm passing the intent from the Main Activity to Estimate Activity:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, EstimateActivity.class);
intent.putExtra("cartList", cartList);
v.getContext().startActivity(intent);
}
});
This is the Estimate Activity:
public class EstimateActivity extends AppCompatActivity {
private RecyclerView eRecyclerview;
private CartAdapter eAdapter;
private RecyclerView.LayoutManager eLayoutManager;
private ArrayList<Inventory> eCartList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_estimate);
eCartList = new ArrayList<Inventory>();
Bundle bundle = getIntent().getExtras();
eCartList = bundle.getParcelableArrayList("cartList");
eRecyclerview = findViewById(R.id.recyclerview);
eLayoutManager = new LinearLayoutManager(this);
eAdapter = new CartAdapter(eCartList); // THIS IS WHERE IM PASSING THE LIST
eRecyclerview.setLayoutManager(eLayoutManager);
eRecyclerview.setAdapter(eAdapter);
}
}
Here is the CartAdapter:
public class CartAdapter extends RecyclerView.Adapter<CartAdapter.CartViewHolder> {
private ArrayList<Inventory> eInventoryList;
public static class CartViewHolder extends RecyclerView.ViewHolder {
private TextView eCardCode;
private TextView eCardName;
private CardView eContainerView;
public CartViewHolder(View itemView) {
super(itemView);
eCardCode = itemView.findViewById(R.id.code_view);
eCardName = itemView.findViewById(R.id.name_view);
eContainerView = itemView.findViewById(R.id.container_view2);
}
}
public CartAdapter(ArrayList<Inventory> eCartList){ //this is where i recieve the list
eInventoryList = eCartList;
}
#NonNull
#Override
public CartViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.text_row, parent, false);
CartViewHolder mvh = new CartViewHolder(view);
return mvh;
}
#Override
public void onBindViewHolder(#NonNull CartViewHolder holder, int position) {
Inventory currentItem = eInventoryList.get(position);
holder.eCardName.setText(currentItem.getName());
holder.eCardCode.setText(currentItem.getCode());
holder.eContainerView.setTag(currentItem);
}
#Override
public int getItemCount() {
return eInventoryList.size();
}
}
Lastly here is the XML for the CardView in the RecyclerView
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#color/black"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="5dp"
android:padding="5dp"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:id="#+id/container_view2"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp">
<TextView
android:id="#+id/code_view"
android:text="code_view"
android:textSize="10sp"
android:textColor="#color/white"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
/>
<TextView
android:id="#+id/name_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:text="Name Name Name Name Name"
android:textColor="#android:color/white"
android:textSize="20sp"
android:textStyle="bold"
android:paddingTop="10dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>

Try to debug the application, put a breakpoint after receiving the bundle content and check what values ​​it has to make sure that it is arriving well, you can do the same inside the adapter.
This can be done every time you need to know what values ​​the variables have at a specific time and place
Good luck in this new world !!

Related

Unable to display two items in customview

I want to show items in my custom listview but I cant seem to get it to work. Activity just crashes. My purpose is to show both items in a listview. I am able to retrieve and store the necessary data from firebase into separate arraylists but unable to show them in the listview. Attaching all necessary codes below.
This is my customadapter class
public class CustomAdapter extends ArrayAdapter<String>{
private final Activity context;
private final ArrayList main;
private final ArrayList sub;
public CustomAdapter(Activity context,
ArrayList main, ArrayList sub) {
super(context, R.layout.colortext, main);
this.context = context;
this.main = main;
this.sub = sub;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.colortext, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.textmain);
TextView txtSub=rowView.findViewById(R.id.address);
txtTitle.setText((Integer) main.get(position));
txtSub.setText((Integer) sub.get(position));
return rowView;
}
}
xml of custom
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView1">
</ListView>
<TextView
android:id="#+id/textmain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="6dip"
android:paddingRight="10dip"
android:textSize="23dp"
android:textColor="#01B9F5"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox"
android:clickable="true"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="6dip"
android:paddingRight="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:focusable="false"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello"
android:id="#+id/address"
android:layout_below="#+id/checkBox"
android:textColor="#color/white"/>
</RelativeLayout>
mainactivity
mDatabase.child("users").child(mUserId).child("locs").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds:dataSnapshot.getChildren())
{
main.add(ds.child("title").getValue(String.class));
String g = main.toString();
sub.add(ds.child("addr").getValue(String.class));
String s = sub.toString();
ListView list = findViewById(R.id.listView1);
CustomAdapter adapter2 = new CustomAdapter(Location.this, main, sub);
list.setAdapter(adapter2);
}
}
Try this. In your adapter add these methos to add item in your arraylist.
public class CustomAdapter extends ArrayAdapter<String>{
private final ArrayList mainList;
private final ArrayList subList;
public void addMain(Main main){
mainList.add(main);
notifyDataSetChanged();
}
public void addSub(Sub sub){
subList.add(sub);
notifyDataSetChanged();
}
// manage your data in onBind
#Override
public void onBindViewHolder(final ViewHolder holder, int
position) {
if(position < mainList.size() ){
// display mainlist related data here
// after adding items in mainlist, this will executed.
}
if(position > mainList.size()-1) {
// display sublist related data here
}
}
}
You need to add mainList first for the condition in onbind will be met.
You can add data to your adapter like below.
// For example this is that path of your main lists
DatabaseReference ref = database.getReference().child("main");
ref.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
// I assume you have a custom object. In this case let's just say `Main`
Main main = dataSnapshot.getValue(Main.class);
adapter.addMain(notification);
}
// ... other override methods

How do I create an OnClickListener for a button within my list adapter that will allow me to set the visibility for an EditText view

I currently have a listview that contains a button and EditText view. How do I properly implement an OnClickListener in my list adapter so when each button is clicked, the associated EditText within the view is hidden via the setVisibility method.
Based on my current implementation of the OnClickListener in my list adapter, when I click a button to hide the corresponding EditText within the view it hides the very last EditText within the viewport and does not hide the corresponding EditText that it's in the same view as the button. Below is my listview xml file (inspection_single_row.xml), my list adapter (InspectionAdapter.java) and main activity (MainActivity).
inspection_single_row.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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Was the sink cleaned floors mopped"
android:id="#+id/text_id"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/check_boxes"
android:layout_marginBottom="20dp"
android:gravity="center_horizontal">
<RadioGroup
android:id="#+id/groupRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:orientation="horizontal">
<RadioButton
android:id="#+id/radioComplete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Complete"
android:checked="false"
android:textColor="#color/grey_mid"/>
<RadioButton
android:id="#+id/radioIncomplete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Incomplete"
android:checked="false"
android:textColor="#color/grey_mid"
android:layout_marginLeft="25dp"/>
</RadioGroup>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="click"
android:onClick="clickMe"
android:id="#+id/btn"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/master_linlayout"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<EditText
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginBottom="20dp"
android:gravity="top"
android:padding="10dp"
android:textSize="14sp"
android:background="#drawable/border2"
android:inputType="textMultiLine"
android:textColor="#color/grey_mid"
android:id="#+id/edit_text"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
InspectionAdapter.java
public class InspectionAdapter extends ArrayAdapter<InspectionObject> {
ArrayList<InspectionObject> arrayList;
Context context;
int Resource;
LayoutInflater layoutInflater;
ProgressHolder holder;
public InspectionAdapter(Context context, int resource, ArrayList<InspectionObject> objects) {
super(context, resource, objects);
this.context = context;
arrayList = objects;
Resource = resource;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
private static class ProgressHolder {
public RadioGroup radio_group;
public EditText deficiency_notes;
public TextView inspection_task;
public RadioButton radio_yes;
public RadioButton radio_no;
public LinearLayout master_layout;
public Button my_button;
}
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View v = convertView;
holder = new ProgressHolder();
if(v == null)
{
v = layoutInflater.inflate(Resource, null);
holder.radio_group = (RadioGroup)v.findViewById(R.id.groupRadio);
holder.deficiency_notes = (EditText)v.findViewById(R.id.edit_text);
holder.inspection_task = (TextView)v.findViewById(R.id.text_id);
holder.radio_yes = (RadioButton)v.findViewById(R.id.radioComplete);
holder.radio_no = (RadioButton)v.findViewById(R.id.radioIncomplete);
holder.master_layout = (LinearLayout)v.findViewById(R.id.master_linlayout);
holder.my_button = (Button)v.findViewById(R.id.btn);
v.setTag(holder);
}else{
holder = (ProgressHolder)v.getTag();
}
final InspectionObject inspectionObject = arrayList.get(position);
holder.my_button.setTag(position);
holder.deficiency_notes.setTag(position);
holder.my_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = (Integer) v.getTag(); //the real and updated position
Log.i("ConfirmAdapter","Button # position : " + pos);
Log.i("ConfirmAdapter","EditText # position : " + holder.deficiency_notes.getTag());
}
});
return v;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
ListView lv;
InspectionAdapter inspection_adapter;
ArrayList<InspectionObject> inspectionList;
Boolean eval;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = findViewById(R.id.listView2);
inspectionList = new ArrayList<InspectionObject>();
inspectionList = new ArrayList<InspectionObject>();
inspectionList.add(new InspectionObject(true, "", "Were the floor mopped?"));
inspectionList.add(new InspectionObject(true, "", "Were the mirrors cleaned?"));
inspectionList.add(new InspectionObject(false, "", "Were the toilets cleaned?"));
inspectionList.add(new InspectionObject(true, "", "Was high/low dusting performed?"));
inspection_adapter = new InspectionAdapter(getApplicationContext(), R.layout.inspection_single_row, inspectionList);
lv.setAdapter(inspection_adapter);
}
}
Edit:
In this part
Log.i("ConfirmAdapter","EditText # position : " + holder.deficiency_notes.getTag())
you are still referencing the holder variable that is created last. As i said before: In getView, for every view, you create a new ProgressHolder assigning it to holder variable. So holder is overwritten everytime getView is called. That's why, Log.i gives your last item.
Try the following:
Put the new ProgressHolder inside if clause.
if(v == null)
{
holder = new ProgressHolder();
This way it only creates a new instance, when the view is null.
Instead of setting the tag for the button to position you can set it to holder like this
holder.my_button.setTag(holder);
you don't need to set tag for EditText.
Then in the onClick you get the corresponding instance of ProgressHolder via getTag() and change the visibilty like this:
holder.my_button.setTag(holder);
holder.my_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ProgressHolder clickedHolder = (ProgressHolder)view.getTag();
clickedHolder.deficiency_notes.setVisibility(View.GONE);
}
});

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 )

Setting listeners on buttons in a ListView populated by a CursorAdapter from a Fragment

Although a developer for more years than I can remember I'm new to Java/Android and the whole OOP thing... Yes we do still exist... shambling relics of a more sequential age...!!
Anyway I have a fragment which invokes a CursorAdapter to populate a ListView.
The ListView has several TextViews and two buttons on each row.
I have been trying to set up listeners on each of the buttons so far without success...
I've searched this forum (and others) and I've used several bits of code suggested by contributors..
From my research my biggest problem seems to be that I'm doing all this from a Fragment.
My question is how do I pass the base ListView id from the Fragment to the Adapter...???
My ListView Child XML...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_marginTop="#dimen/textViewMarginTop"
android:layout_marginRight="#dimen/textViewMarginRight"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="12345678"
android:id="#+id/job_id"
/>
<TextView
android:layout_marginTop="#dimen/textViewMarginTop"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="abcdef"
android:id="#+id/from_location"
android:layout_toRightOf="#+id/job_id"
/>
<TextView
android:layout_marginTop="#dimen/textViewMarginTop"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Blanchardstown"
android:id="#+id/to_location"
android:layout_toRightOf="#+id/from_location"
/>
<Button
android:layout_width="50dp"
android:layout_height="wrap_content"
android:id="#+id/job_bid"
android:text="#string/joblist_bid"
android:layout_below="#+id/job_id"
android:layout_toLeftOf="#+id/job_details"
/>
<Button
android:layout_width="80dp"
android:layout_height="wrap_content"
android:id="#+id/job_details"
android:text="#string/joblist_details"
android:layout_below="#+id/to_location"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
My ListView Parent XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_marginTop="#dimen/textViewMarginTop"
android:layout_marginRight="#dimen/textViewMarginRight"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="12345678"
android:id="#+id/job_id"
/>
<TextView
android:layout_marginTop="#dimen/textViewMarginTop"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="abcdef"
android:id="#+id/from_location"
android:layout_toRightOf="#+id/job_id"
/>
<TextView
android:layout_marginTop="#dimen/textViewMarginTop"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Blanchardstown"
android:id="#+id/to_location"
android:layout_toRightOf="#+id/from_location"
/>
<Button
android:layout_width="50dp"
android:layout_height="wrap_content"
android:id="#+id/job_bid"
android:text="#string/joblist_bid"
android:layout_below="#+id/job_id"
android:layout_toLeftOf="#+id/job_details"
/>
<Button
android:layout_width="80dp"
android:layout_height="wrap_content"
android:id="#+id/job_details"
android:text="#string/joblist_details"
android:layout_below="#+id/to_location"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
My Fragment Code
public class jobListFragment extends ListFragment
{
private final String m_LogcatTag = "assignment5";
public ListView m_jobListView;
public JobDataAdpter m_jobDataAdapter;
JobsDataSource m_datasource;
private SQLiteDatabase m_database;
CursorAdapter m_cursoradaptor;
public View m_jobListFragmentBaseView;
Cursor m_cursor;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
m_jobListFragmentBaseView = inflater.inflate(layout.joblistfragment, container, false);
ListView m_jobListView = (ListView)m_jobListFragmentBaseView.findViewById(android.R.id.list);
//Declare an object to handle database I-O and open a channel to the database
m_datasource = new JobsDataSource (getActivity().getApplicationContext());
m_datasource.open();
m_cursor = m_datasource.getAllJobs();
m_jobDataAdapter = new JobDataAdpter(getActivity().getApplicationContext(), m_cursor, 0);
m_jobListView.setAdapter(m_jobDataAdapter);
return m_jobListFragmentBaseView;
}
My Adapter Code
public class JobDataAdpter extends CursorAdapter
{
private final String mLogcatTag = "assignment5";
protected ListView mListView; THIS IS THE VARIABLE IN QUESTION
public JobDataAdpter(Context context, Cursor cursor, int flags )
{
super(context, cursor, 0);
}
protected static class RowViewHolder
{
public TextView m_job_bid;
public TextView m_job_details;
}
//End New Code\
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
View view = View.inflate(context, R.layout.jobslistlayout, null);
RowViewHolder holder = new RowViewHolder();
holder.m_job_bid = (TextView) view.findViewById(R.id.job_bid);
holder.m_job_details = (TextView) view.findViewById(R.id.job_details);
holder.m_job_bid.setOnClickListener(m_OnBidClickListener);
holder.m_job_details.setOnClickListener(m_OnDetailsClickListener);
view.setTag(holder);
return view;
}
#Override
public void bindView(View view, Context context, Cursor cursor)
{
TextView m_job_IdView = (TextView) view.findViewById(R.id.job_id);
TextView m_from_LocationView = (TextView) view.findViewById(R.id.from_location);
TextView m_to_LocationView = (TextView) view.findViewById(R.id.to_location);
// Extract properties from cursor
long row_id = cursor.getLong((cursor.getColumnIndexOrThrow(MySQLiteOpenHelper.m_COLUMN_ID)));
String m_job_id = cursor.getString(cursor.getColumnIndexOrThrow(MySQLiteOpenHelper.m_COLUMN_JOB_ID));
String m_From_Location = cursor.getString(cursor.getColumnIndexOrThrow(MySQLiteOpenHelper.m_COLUMN_JOB_FROM_LOCATION));
String m_To_Location = cursor.getString(cursor.getColumnIndexOrThrow(MySQLiteOpenHelper.m_COLUMN_JOB_TO_LOCATION));
// Populate fields with extracted properties
m_job_IdView.setText(m_job_id);
m_from_LocationView.setText(String.valueOf(m_From_Location));
m_to_LocationView.setText(String.valueOf(m_To_Location));
}
private View.OnClickListener m_OnBidClickListener = new View.OnClickListener()
{
#Override
public void onClick(View v)
{ HERE IS WHERE THE VARIABLE IS USED
final int position = mListView.getPositionForView((View) v.getParent());
Log.v(mLogcatTag, "Title clicked, row %d" + position);
}
};
private View.OnClickListener m_OnDetailsClickListener = new View.OnClickListener()
{
#Override
public void onClick(View v)
{
final int position = mListView.getPositionForView((View) v.getParent());
Log.v(mLogcatTag, "Text clicked, row %d" + position);
}
};
}
OK guys .....That's it.... Hope you can help

CardView in fragment is invisible

I have a main activity (ActivityMain.java) that I would like to use to navigate between four fragments. In one of these fragments, I'm attempting to place a CardView in conjunction with a RecyclerView to create a vertical list of cards. However, so far I've been unable to get any of the cards to display when I run the app. The CardView will display perfectly in Android Studio's design preview, but when an actual device/emulator is used it disappears.
I've tried to manually set the visibility of the CardView through Java, but it continued to stay invisible. I believe that the fragment's layout could be covering the CardView's layout, but I'm still very new to Android development so I'm not completely sure what the problem could be.
Below I've pasted the classes and XML files that are likely to be associated with my problem.
AdapterMainFeed.java
public class AdapterMainFeed extends RecyclerView.Adapter<AdapterMainFeed.ViewHolderMainFeed> {
private ArrayList<Article> listArticlesMain = new ArrayList<>();
private LayoutInflater inflater;
public AdapterMainFeed(Context context) {
inflater = LayoutInflater.from(context);
}
public void setArticlesMain(ArrayList<Article> listArticlesMain) {
this.listArticlesMain = listArticlesMain;
notifyDataSetChanged();
}
#Override
public ViewHolderMainFeed onCreateViewHolder(ViewGroup container, int i) {
View layout = inflater.inflate(R.layout.view_main_feed, container, false);
Article article1 = new Article("asdf", "ghj", "klm", new Date(0));
Article article2 = new Article("sdfg", "hjk", "lmn", new Date(0));
Article article3 = new Article("dfgh", "jkl", "mno", new Date(0));
listArticlesMain.add(article1);
listArticlesMain.add(article2);
listArticlesMain.add(article3);
return new ViewHolderMainFeed(layout);
}
#Override
public void onBindViewHolder(ViewHolderMainFeed viewHolderMainFeed, int i) {
Article currentArticle = listArticlesMain.get(i);
viewHolderMainFeed.articleTitle.setText(currentArticle.getTitle());
viewHolderMainFeed.articleAuthor.setText(currentArticle.getAuthor());
viewHolderMainFeed.articleWebsite.setText(currentArticle.getWebsite());
DateFormat formatter = DateFormat.getDateTimeInstance();
final String timePosted = formatter.format(currentArticle.getTimePosted());
viewHolderMainFeed.articleTime.setText(timePosted);
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public int getItemCount() {
return listArticlesMain.size();
}
static class ViewHolderMainFeed extends RecyclerView.ViewHolder {
TextView articleTitle;
TextView articleAuthor;
TextView articleWebsite;
TextView articleTime;
public ViewHolderMainFeed(View itemView) {
super(itemView);
articleTitle = (TextView) itemView.findViewById(R.id.mainArticleTitle);
articleAuthor = (TextView) itemView.findViewById(R.id.mainArticleAuthor);
articleWebsite = (TextView) itemView.findViewById(R.id.mainArticleWebsite);
articleTime = (TextView) itemView.findViewById(R.id.mainArticleTime);
}
}
}
view_main_feed.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
tools:context="com.convergeapp.converge.ActivityMain">
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
card_view:cardCornerRadius="7dp"
card_view:cardBackgroundColor="#color/colorPurpleSeance"
android:id="#+id/mainArticleCard"
android:layout_width="match_parent"
android:layout_height="300dp"
android:padding="8dp"
android:clickable="true">
<RelativeLayout
android:id="#+id/mainArticleLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<TextView
android:id="#+id/mainArticleTitle"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
<TextView
android:id="#+id/mainArticleAuthor"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="#id/mainArticleTitle"/>
<TextView
android:id="#+id/mainArticleWebsite"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="#+id/mainArticleTime"/>
<TextView
android:id="#+id/mainArticleTime"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
After playing around with some changes, I finally discovered the solution. What I did was add the Article objects to the list in the main fragment's onCreateView method. Additionally, I instantiated CardView in the ViewHolder, though that didn't help me see the cards initially.
So, if you're ever in a similar predicament and can't figure out the solution, try to actually create the objects before setting them.
You need to call notifyDataSetChanged(); inside onCreateViewHolder after adding the new articles so that the itemcount is updated

Categories