Unable to display two items in customview - java

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

Related

TextView not appearing. Blank CardViews

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 !!

how can i code public class for SimpleCursorAdapter?

I am new to android and I need to use ListView for my project. I use a sample from the internet which has no public class for ListView so I am not able to code flexible. how can I code public class for this.
public class LIGHTS extends AppCompatActivity {
ListView users_list;
private DatabaseManager dbManager;
private SimpleCursorAdapter adapter;
private DatabaseHelper dbHelper;
final String[] from = new String[]{dbHelper._ID, dbHelper.TITLE, dbHelper.DESC};
final int[] to = new int[]{R.id.id, R.id.KEYCODE, R.id.NAME};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lights);
startconnection();
dbManager = new DatabaseManager(this);
dbManager.open();
Cursor cursor = dbManager.fetch();
users_list = findViewById(R.id.users_list);
adapter = new SimpleCursorAdapter(this, R.layout.adapter, cursor, from, to, 0);
users_list.setAdapter(adapter);}
and the fetch() is in below code in dbmanager:
public Cursor fetch() {
String[] columns = new String[]{dbHelper._ID, dbHelper.TITLE, dbHelper.DESC};
Cursor cursor = database.query(dbHelper.TABLE_NAME, columns, null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
Here's an example based upon your code that handles clicking a button for each item in the list.
If you click a switch then it displays the id of the item via a toast.
This utilises a Custom Adapter based upon (extends) the CursorAdapter class.
First the layout adapter.xml used for the item (should have the basics of your's and includes a switch who's id is the_switch) :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/id"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/KEYCODE"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/NAME"
android:layout_width="0dp"
android:layout_weight="6"
android:layout_height="wrap_content" />
<Switch
android:id="#+id/the_switch"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:focusable="false"
/>
</LinearLayout>
The Activity Lights.java is now :-
public class Lights extends AppCompatActivity {
ListView users_list, alt_users_list;
private DatabaseManager dbManager;
private MyCustomCursorAdapter adapter;
//private DatabaseManager dbHelper; //?????? a second not needed
Cursor cursor;
Context mContext;
//<<<<<<<<<< Not needed although could be passed
//final String[] from = new String[]{DatabaseManager._ID, DatabaseManager.TITLE, DatabaseManager.DESC};
//final int[] to = new int[]{R.id.id, R.id.KEYCODE, R.id.NAME};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
setContentView(R.layout.activity_lights);
startconnection(); //?????? dummied out
users_list = findViewById(R.id.users_list);
alt_users_list = findViewById(R.id.alt_users_list);
dbManager = new DatabaseManager(this);
dbManager.open();
manageListView(); //Handles the ListView
}
// Moved here handles list refresh if called (e.g. in onResume)
private void manageListView() {
cursor = dbManager.fetch();
//Setup the adapter if not already setup else swaps (refreshes) the cursor
if (adapter == null) {
adapter = new MyCustomCursorAdapter(this, cursor);
users_list.setAdapter(adapter);
users_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(mContext,"You clicked on the item with an ID of " + String.valueOf(id),Toast.LENGTH_SHORT).show();
}
});
} else {
adapter.swapCursor(cursor);
}
}
private void startconnection(){}
#Override
protected void onDestroy() {
super.onDestroy();
// Close the Cursors when done with them
cursor.close();
}
#Override
protected void onResume() {
super.onResume();
// Refresh the listviews when returning to the activity
manageListView();
}
}
Comments try to explain changes (basically it is quite similar).
The biggest change is that the setting up of the listview has been moved to a method of it's own, which also handles refreshing the listview (redisplaying it after the underlying data has been changed).
The instantiation of the adapter is also simpler than for the SimpleCursorAdapter (the layout and column to view handling coded in the adapter).
The adapter myCustomAdapter.java is :-
public class MyCustomCursorAdapter extends CursorAdapter {
public MyCustomCursorAdapter(Context context, Cursor c) {
super(context, c, 0);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (position % 2 == 0) {
view.setBackgroundColor(0xFFAAAAFF);
} else {
view.setBackgroundColor(0xAAAAAAFF);
}
return view;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.adapter,parent,false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
((TextView)view.findViewById(R.id.id)).setText(cursor.getString(cursor.getColumnIndex(DatabaseManager._ID)));
((TextView)view.findViewById(R.id.KEYCODE)).setText(cursor.getString(cursor.getColumnIndex(DatabaseManager.TITLE)));
((TextView)view.findViewById(R.id.NAME)).setText(cursor.getString(cursor.getColumnIndex(DatabaseManager.DESC)));
Switch thisswitch = view.findViewById(R.id.the_switch);
thisswitch.setTag(cursor.getString(cursor.getColumnIndex(DatabaseManager._ID)));
thisswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(buttonView.getContext(),
"You clicked the switch for ID " + (String) buttonView.getTag() +
" the status is now " + (new Boolean(isChecked)).toString(),
Toast.LENGTH_SHORT)
.show()
;
}
});
}
}
bindView has primarily been used it :-
binds the values from the columns of the cursor to the views for each item
and in this case sets the tag of the switch to the id and then adds an onCheckChangedListener for the Button.
bindView has the advantage that the cursor and context are passed to it.
getView can also be used, it has the advantage of having the position of the item in the list passed.
In this case it has been used to alternate the background colour for each item.
Result
Here's a screen shot showing the toast (note testing data was added to the underlying database, so this will obviously vary from yours) :-
Additional
It might be that you need to handle the switch check change in the owning activity.
The following changes show a basic means, via an interface, of handling the switch event in the activity, rather than in the adapter.
First the interface myOnCheckedChangedInterface.java
public interface myOnCheckedChangedInterface {
void myOnCheckedChangedHandler(String id, boolean check_status);
}
Second change Lights.java by adding the handler method myOnCheckedChangedHandler
#Override
public void myOnCheckedChangedHandler(String id, boolean check_status) {
Toast.makeText(
this,
"You changed the status for the row with an id of " + id +
" the status is now " + new Boolean(check_status).toString(),
Toast.LENGTH_SHORT).show();
}
Ignore the error that the method doesn't override method from it's superclass.
Third change the Class declaration to implement the interface by adding implements myOnCheckedChangedInterface as per :-
public class Lights extends AppCompatActivity implements myOnCheckedChangedInterface {
Lastly change MyCustomCursorAdapter to be able to call the myOnCheckedChangedHandler
e.g.
public class MyCustomCursorAdapter extends CursorAdapter {
Lights calling_activity; //<<<<<<<<<<########### ADDED for interface
public MyCustomCursorAdapter(Context context, Cursor c) {
super(context, c, 0);
this.calling_activity = (Lights) context; //<<<<<<<<<<########### ADDED for interface
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (position % 2 == 0) {
view.setBackgroundColor(0xFFAAAAFF);
} else {
view.setBackgroundColor(0xAAAAAAFF);
}
return view;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.adapter,parent,false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
((TextView)view.findViewById(R.id.id)).setText(cursor.getString(cursor.getColumnIndex(DatabaseManager._ID)));
((TextView)view.findViewById(R.id.KEYCODE)).setText(cursor.getString(cursor.getColumnIndex(DatabaseManager.TITLE)));
((TextView)view.findViewById(R.id.NAME)).setText(cursor.getString(cursor.getColumnIndex(DatabaseManager.DESC)));
Switch thisswitch = view.findViewById(R.id.the_switch);
thisswitch.setTag(cursor.getString(cursor.getColumnIndex(DatabaseManager._ID)));
thisswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
/**
Toast.makeText(buttonView.getContext(),
"You clicked the switch for ID " + (String) buttonView.getTag() +
" the status is now " + (new Boolean(isChecked)).toString(),
Toast.LENGTH_SHORT)
.show()
**/
calling_activity.myOnCheckedChangedHandler((String)buttonView.getTag(),isChecked); //<<<<<<<<<<########### ADDED for interface
}
});
}
}
See commments with //<<<<<<<<<<########### ADDED for interface for changes
The original Toast has been commented out as it is no longer needed
Note this isn't the tidiest way as the Adapter is tied to a Lights activity, it's just meant to be a simple example.
In order to customize a ListAdapter, you need to create your own custom ListAdapter class that is based on, or 'extends' the built-in ListAdapter (such as SimpleListAdapter or BaseAdapter). Then, you can customize the appearance and what fields of data to display. Below is an example of a custom ListAdapter I called ClaimsListAdapter.java that 'extends' the built-in class called BaseAdapter:
package com.mycompany.myapp.adapter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import com.mycompany.myapp.ClaimListFragment;
import com.mycompany.myapp.R;
import com.mycompany.myapp.TripListFragment;
import com.mycompany.myapp.model.ClaimItem;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
// You might be able to extend SimpleListAdapter instead if you wish
public class ClaimListAdapter extends BaseAdapter {
private Context context;
private ArrayList<ClaimItem> claimItems;
ClaimListFragment fragment;
//I'm passing references to both the active Context as well as the active Fragment
//You might only need to pass the active Context
public ClaimListAdapter(ClaimListFragment fragment, Context context, ArrayList<ClaimItem> claimItems){
this.context = context;
this.claimItems = claimItems;
this.fragment = fragment;
}
#Override
public int getCount() {
return claimItems.size();
}
#Override
public Object getItem(int position) {
return claimItems.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#SuppressLint("InflateParams")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
//This is the layout for the list item. A SimpleListAdapter doesn't need one
//since it only has one text view, but this allows you to create multiple lines
//and/or multiple fields, buttons, checkboxes etc if you wish
convertView = mInflater.inflate(R.layout.claim_list_item, null);
}
//Get a reference to all of the items in the layout you wish to change
Button btnDelete = (Button) convertView.findViewById(R.id.claim_delete_in_list);
//Note, here I'm saving the row number in the tag of the button to tell the fragment
//which row in the array to delete.
btnDelete.setTag(position);
//Here is an example of setting a click listener for a button in the list
btnDelete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Integer position = (Integer)v.getTag();
//Call the Public method in the parent Fragment (or Activity) to delete from the
//array and refresh the list
fragment.deleteItemList(position);
}
});
btnDelete.setVisibility(View.GONE);
//Get a reference to all of the text fields in the list item
TextView txtTitle = (TextView) convertView.findViewById(R.id.claim_title);
TextView txtStatus = (TextView) convertView.findViewById(R.id.claim_status);
TextView txtDate = (TextView) convertView.findViewById(R.id.claim_date);
TextView txtDistance = (TextView) convertView.findViewById(R.id.claim_distance);
TextView txtAmount = (TextView) convertView.findViewById(R.id.claim_amount);
String claim_title = claimItems.get(position).getDocumentID();
String claim_status = claimItems.get(position).getClaimStatus();
txtTitle.setText(claim_title);
txtStatus.setText(claim_status);
return convertView;
}
}
And the claim_list_item.xml layout file:
<?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:baselineAligned="false"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/whole_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:background="#drawable/list_selector_light"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/top_layout"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_weight="0.48"
android:background="#00000000"
android:orientation="horizontal" >
<TextView
android:id="#+id/claim_title"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginBottom="2dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp"
android:layout_weight="0.73"
android:background="#00000000"
android:gravity="start|center_vertical"
android:text=""
android:textColor="#FFFFFFFF"
android:textSize="16sp" />
<TextView
android:id="#+id/claim_status"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:background="#00000000"
android:gravity="end|center_vertical"
android:text=""
android:textColor="#FFFFFFFF"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_weight="0.48"
android:background="#00000000"
android:orientation="horizontal" >
<TextView
android:id="#+id/claim_date"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:background="#00000000"
android:gravity="start|center_vertical"
android:text=""
android:textColor="#FFFFFFFF"
android:textSize="14sp" />
<TextView
android:id="#+id/claim_distance"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="50dp"
android:layout_marginStart="50dp"
android:layout_marginRight="50dp"
android:layout_marginEnd="50dp"
android:layout_weight="1.0"
android:layout_gravity="center"
android:background="#00000000"
android:gravity="center|center_vertical"
android:text=""
android:textSize="12sp"
android:textColor="#FFFFFFFF"/>
<TextView
android:id="#+id/claim_amount"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:background="#00000000"
android:gravity="end|center_vertical"
android:text=""
android:textColor="#FFFFFFFF"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/claim_delete_in_list"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:text="#string/delete"
android:textSize="16sp"
android:textColor="#FFFFFFFF"
android:background="#android:color/holo_red_dark"
/>
</LinearLayout>

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);
}
});

Custom ListView with image, text and add item button

I am quite new to Android and can't achieve this, been searching all day.
Layout I'm trying to create.
I have created the custom xml layout, I have found ways to add items on create, but I need the list to be empty and than when the button is pressed to add from a list.
This is the layout:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<ImageView
android:id="#+id/info_img_view"
android:layout_width="30dp"
android:layout_height="30dp" />
<TextView
android:id="#+id/info_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<TextView
android:id="#+id/info_time_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="right"
/>
</TableRow>
I have a ListView in the main activity:
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
How do I go about this? I would appreciate it if someone can point me to a resource where I can learn what the code is actually doing, not the tutorials I find where I just copy and paste...
Thanks!
Edit, a bit more explanation of what I'm trying to achieve
I have 6 buttons. When a button is pressed it should add a list item with two textviews, and one image out of total three images.
So for instance if Button1 is pressed: Add list item > "Text one" "Text one" "imageTwoOfThree".
Than, if Button2 is pressed: Add list item on top > "Text two" "Text two" "imageTwoOfThree"
And so on... The text is hardcoded.
Here use this:
I have created a list with dummydata you can change the text and Image according to you
First create a class Data:
public class Data {
private String name,price;
private int imageId;
public Data(){}
public Data(String name,String price,int imageId){
this.name = name;
this.price = price;
this.imageId = imageId;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
}
Then create a ListView Adapter to handle your data:
public class ListViewAdaptor extends RecyclerView.Adapter<ListViewAdaptor.MyViewHolder> {
private List<Data> mDataList;
public class MyViewHolder extends RecyclerView.ViewHolder{
public TextView name,price;
public ImageView imageView;
public MyViewHolder(View view){
super(view);
name = (TextView) view.findViewById(R.id.name);
price= (TextView) view.findViewById(R.id.price);
imageView = (ImageView) view.findViewById(R.id.image);
}
}
public ListViewAdaptor(List<Data> dataList){
this.mDataList = dataList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_view_item, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Data data = mDataList.get(position);
holder.name.setText(data.getName());
holder.price.setText(data.getPrice());
holder.imageView.setImageResource(data.getImageId());
}
#Override
public int getItemCount() {
return mDataList.size();
}
}
layout for your list view items name it list_view_item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_launcher"
android:id="#+id/image"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/name"
android:text="name"
android:gravity="center"
android:textSize="26sp"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/price"
android:text="price"
android:gravity="center"
android:textSize="26sp"
android:layout_weight="1"/>
</LinearLayout>
Then from your activity where you want to add listView add recyclerView in layout:
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/recycler_view">
</android.support.v7.widget.RecyclerView>
Then use this recyclerview like this:
//I have called it from my MainActivity you can use it in whatever activity you'll like
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private ListViewAdaptor mAdapter;
private List<Data> mDataList = new ArrayList<>();
private static final String TAG = "MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mAdapter = new ListViewAdaptor(mDataList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setAdapter(mAdapter);
prepareList();
}
public void prepareList(){
Data data = new Data("Item1","Price1",R.drawable.star);
mDataList.add(data);
data = new Data("Item2","Price2",R.drawable.star);
mDataList.add(data);
data = new Data("Item3","Price3",R.drawable.star);
mDataList.add(data);
data = new Data("Item4","Price4",R.drawable.star);
mDataList.add(data);
data = new Data("Item5","Price5",R.drawable.star);
mDataList.add(data);
}
}
Hope this helps!!!
You will find lots of online resources for this. But let me put it in a brief way.
Assuming you want to store 2 textviews in one single row.
1. For each layout of the row, you will need to make a custom layout xml file and design your layout there.
Next, make a class which stores the data and returns the data through getters.
class Category {
private String categoryName;
private String categoryImageURL;
Category (String categoryName, String categoryImageUrl) {
this.categoryName = categoryName;
this.categoryImageURL = categoryImageUrl;
}
String getCategoryName () {
return categoryName;
}
String getCategoryImageURL () {
return categoryImageURL;
}
}
Now make a custom arrayadapter which will link the data and the layout.
Extend the arrayadapter class with the class you defined above.
private class categoryArrayAdapter extends ArrayAdapter<Category> {
private Context context;
private List<Category> categories;
public categoryArrayAdapter (Context context, int resource, ArrayList<Category> objects) {
super(context, resource, objects);
this.context = context;
this.categories = objects;
}
public View getView (int position, View convertView, ViewGroup parent) {
Category category = categories.get(position);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.category_row_view, null);
TextView categoryListRowText = (TextView) view.findViewById(R.id.categoryListRowText);
TextView categoryListRowImage = (TextView) view.findViewById(R.id.categoryListRowImage);
categoryListRowText.setText(category.getCategoryName());
categoryListRowImage.setText(category.getCategoryImageURL());
return view;
}
}
Finally link the adapter to your listview
ArrayAdapter<Category> adapter = new categoryArrayAdapter(this, 0, categories);
categoryListView.setAdapter(adapter);
Hope this answered you question.

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

Categories