Add RadioButton inside RadioGroup in ListView - java

I've been trying to add radio buttons inside a radio group in a listview.
So, if I click a radio button it checks it but if I click another one, it doesn't remove the previous selection. I'm a beginner in android so forgive me If I'm doing something terribly wrong.
This is my Main Activity and I have the radio group outside the list
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="20dp">
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/car_list_radio_group">
<ListView
android:id="#+id/vehicle_list"
android:clickable="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></ListView>
</RadioGroup>
</LinearLayout>
This is my custom view for each item on the list
<?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"
android:alwaysDrawnWithCache="true"
android:backgroundTint="#ffffff">
<RadioButton
android:id="#+id/car_selected"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:buttonTint="#color/tesla_red"
android:padding="10dp"
android:text=""
android:textColor="#android:color/white"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/vin"
android:layout_width="183dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/car_selected"
android:layout_marginStart="30dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#android:color/white"
android:textSize="12sp" />
</RelativeLayout>
This is my adapter for the list
private final class VehiclesAdapter extends BaseAdapter {
LayoutInflater inflter;
ArrayList<VehicleItem> vehiclesArray;
public VehiclesAdapter(Context applicationContext, ArrayList<VehicleItem> items) {
vehiclesArray = items;
inflter = (LayoutInflater.from(applicationContext));
}
#Override
public int getCount() {
return vehiclesArray.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return Long.parseLong(vehiclesArray.get(position).getId());
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
VehicleHolder holder;
VehicleItem vehicleItem = vehiclesArray.get(position);
if (convertView == null) { // if convertView is null
convertView = inflter.inflate(R.layout.vehicle_item, parent, false);
holder = new VehicleHolder();
holder.name = convertView.findViewById(R.id.car_selected);
holder.vin = convertView.findViewById(R.id.vin);
// initialize views
convertView.setTag(holder); // set tag on view
} else {
holder = (VehicleHolder) convertView.getTag();
// if not null get tag
// no need to initialize
}
if(vehicleItem.getId().equals(currentVehicle)){
holder.name.setChecked(true);
}
holder.name.setText(vehicleItem.getName());
holder.vin.setText(vehicleItem.getVin());
//update views here
return convertView;
}
}
If you need to see something else please let me know.

RadioGroup is a LinearLayout and is meant to have RadioButton children in order to handle check/uncheck properly. You will have to drop it and handle the check/uncheck logic yourself from the adapter.

add this in your list_item.xml
You will get radioGroup in each row
And remove RadioGroup from main_activity.xml
hope this will help you...
<?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"
android:alwaysDrawnWithCache="true"
android:backgroundTint="#ffffff">
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/car_list_radio_group">
<RadioButton
android:id="#+id/car_selected"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:buttonTint="#color/tesla_red"
android:padding="10dp"
android:text=""
android:textColor="#android:color/white"
android:textSize="16sp"
android:textStyle="bold" />
<RadioButton
android:id="#+id/car_not_selected"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_torightof="#+id/car_selected"
android:buttonTint="#color/tesla_red"
android:padding="10dp"
android:text=""
android:textColor="#android:color/white"
android:textSize="16sp"
android:textStyle="bold"
</RadioGroup>
<TextView
android:id="#+id/vin"
android:layout_width="183dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/car_selected"
android:layout_marginStart="30dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#android:color/white"
android:textSize="12sp" />
</RelativeLayout>

Related

bottom navigation data is not displaying using recyclerview using cardview

Hi in the below code I am working with bottom navigation it contains different option in that one thing is account .If click on the account bottom tab want to display the cardview in recyclerviews
But data is not coming anything. It is displaying empty.
using this below xml to display the content in listview format
accounts.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
Fragment_account.xml:
<?xml version="1.0" encoding="utf-8"?>
<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"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:background="#color/White">
<android.support.v7.widget.CardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardBackgroundColor="#color/slivergray"
app:cardCornerRadius="8dp"
app:cardElevation="4dp"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/White">
<ImageView
android:id="#+id/appImage"
android:layout_width="72dp"
android:layout_height="72dp"
android:layout_marginLeft="16dp"
android:background="#drawable/ic_account_circle_black_24dp"
android:backgroundTint="#color/gray"
android:scaleType="centerCrop"
tools:ignore="ContentDescription"/>
<TextView
android:id="#+id/headingText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/appImage"
android:paddingLeft="16sp"
android:paddingRight="16dp"
android:text="Apollo Hospital"
android:textColor="#color/black"
android:textSize="18sp"
tools:ignore="RtlHardcoded"/>
<TextView
android:id="#+id/subHeaderText"
style="#style/Base.TextAppearance.AppCompat.Subhead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/headingText"
android:layout_toRightOf="#+id/appImage"
android:paddingLeft="16dp"
android:text="Hospital"
android:paddingRight="16dp"
android:textColor="#color/gray"
android:textSize="15sp"/>
<TextView
android:id="#+id/subHeadingText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/subHeaderText"
android:layout_toRightOf="#+id/appImage"
android:gravity="left"
android:lines="5"
android:maxLines="5"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="16dp"
android:text="stories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detailstories_detail"
android:textColor="#color/gray"
android:textSize="14sp"/>
<TextView
android:id="#+id/textViewOptions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:text="⋮"
android:textColor="#color/gray"
android:layout_marginRight="10dp"
android:textAppearance="?android:textAppearanceLarge"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#id/subHeadingText">
<LinearLayout
android:layout_width="209dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="30dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:backgroundTint="#color/gray"
android:src="#drawable/ic_account_circle_black_24dp"></ImageView>
<Button
android:id="#+id/action1"
style="#style/Base.Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Action 1"
android:textColor="#color/gray"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="right">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:src="#drawable/ic_account_circle_black_24dp"
android:layout_marginLeft="50dp"></ImageView>
<Button
android:id="#+id/action2"
style="#style/Base.Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Action 2"
android:textColor="#color/gray"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</RelativeLayout>
AccountFragment.java:
public class AccountFragement extends Fragment {
public AccountFragement(){
}
RecyclerView rv;
private List<List_Data> list_data;
private MyAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate (R.layout.account, container, false);
rv=(RecyclerView) rootView.findViewById(R.id.recyclerview);
//rv.setHasFixedSize(true);
rv.setLayoutManager(new LinearLayoutManager(getContext()));
list_data=new ArrayList<>();
adapter=new MyAdapter(list_data,getContext());
rv.setAdapter(adapter);
return rootView;
}
private void setupData(List<List_Data> list_data) {
adapter=new MyAdapter(list_data,getContext());
rv.setAdapter(adapter);
}
}
List_Data.java:
public class List_Data {
String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
MyAdapter.java:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<List_Data>list_data;
private Context context;
private String title1;
public MyAdapter(List<List_Data> list_data, Context context) {
this.list_data = list_data;
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_account,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
List_Data listData=list_data.get(position);
// title1=list_data.get(0).getTitle();
holder.title.setText(listData.getTitle());
}
#Override
public int getItemCount() {
return list_data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView img;
TextView title,subtile;
public ViewHolder(View itemView) {
super(itemView);
title=(TextView)itemView.findViewById(R.id.headingText);
subtile=(TextView)itemView.findViewById(R.id.subHeaderText);
title.setText(title1);
}
}
}
the code you have written is somehow incomplete, first you can not view any data because there are no data in the List list_data in the AccountFragment so the adapter is does not know where to fetch data from.
When you have data in the list then you can populate to the adapter.
I recommend you follow the best approach from the link below in case you have any problem please do let us know.
https://www.androidhive.info/2017/12/android-working-with-bottom-navigation/
And try your best to start using AndroidX in your project

Android Studio with Java: ArrayAdapter not working correctly

I'm making an app in which the user can enter a few items and have it displayed as he adds items. I'm using an adapter to achieve this but instead of showing me a neat list with items, the adapter creates a list of several views repeating themselves (i.e Instead of just using the given TextView as a Template for the entry in the listview, it uses all views in that activity).
Here is the code:
This is from the createEntry class. This code initiates the adapter and loads the previous entries for a particular day. It send adds them all to the adapter.
public void loadEntries() {
events = new ArrayList<>();
adapter = new EntryAdapter(this, entries);
ListView listView = findViewById(R.id.entry_list);
listView.setAdapter(adapter);
new AsyncTask<Void, Void, List<String>>() {
#Override
protected List<String> doInBackground(Void... voids) {
return appDB.entryDao().getEntriesFromDate(day, monthNum, year);
}
#Override
protected void onPostExecute(List items) {
generateEntryTable(items);
}
}.execute();
}
public void generateEntryTable(List items) {
for (int i = 0; i < items.size(); i++) {
adapter.add(new TableEntries(year, monthNum, day, entryContent));
}
}
This is my custom EntryAdapter class:
public class EntryAdapter extends ArrayAdapter<Entry> {
public EntryAdapter(Context context, ArrayList<Entry> entries) {
super(context, 0, entries);
}
#Override
public View getView(int position, View convertView, ViewGroup container {
Entry entry = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_create_event, container, false);
}
TextView newEntry = convertView.findViewById(R.id.new_entry);
newEntry.setText(entry.getContent());
return convertView;
}
}
This is the XML-file:
<androidx.constraintlayout.widget.ConstraintLayout 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:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CreateEntry">
<TextView
android:id="#+id/date"
android:layout_width="222dp"
android:layout_height="44dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="215dp"
android:layout_marginRight="215dp"
android:layout_marginBottom="685dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="403dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="116dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="200dp"
android:background="#5CFF9800"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="#+id/linearLayout2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/date"
app:layout_constraintVertical_bias="0.0">
<TextView
android:id="#+id/textView3"
android:layout_width="112dp"
android:layout_height="35dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="276dp"
android:layout_marginRight="276dp"
android:layout_marginBottom="8dp"
android:text="#string/event"
android:textSize="24sp" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/addEvents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:contentDescription="#string/add_events"
android:cropToPadding="true"
android:onClick="addEvent"
android:scaleType="fitStart"
android:tint="#FF3F02"
app:srcCompat="#android:drawable/ic_menu_add" />
<EditText
android:id="#+id/addEventsPrompt"
android:layout_width="353dp"
android:layout_height="30dp"
android:background="#android:color/transparent"
android:clickable="true"
android:freezesText="false"
android:gravity="start|fill|center_vertical"
android:hint="#string/startTyping"
android:inputType="textShortMessage|textLongMessage|textMultiLine|textPersonName|text"
android:textAllCaps="false"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textSize="14sp" />
</TableRow>
<ListView
android:id="#+id/event_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="#+id/entry_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/new_entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="156dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="#+id/toDoTitle"
android:layout_width="102dp"
android:layout_height="33dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="322dp"
android:layout_marginRight="322dp"
android:text="#string/to_do"
android:textSize="24sp" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/addToDo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:background="#00FFFFFF"
android:contentDescription="#string/add_to_dos"
android:onClick="addToDo"
android:scaleType="fitStart"
android:tint="#FF3B00"
app:srcCompat="#android:drawable/ic_menu_add" />
<EditText
android:id="#+id/addToDoPrompt"
android:layout_width="344dp"
android:layout_height="32dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_weight="100"
android:autofillHints=""
android:background="#android:color/transparent"
android:gravity="start|fill_horizontal|center_vertical"
android:hint="#string/startTyping"
android:inputType="textShortMessage|textLongMessage|textMultiLine|text"
android:textSize="14sp" />
<TextView
android:id="#+id/new_to_do"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
</LinearLayout>
If you need to see any other code, please let me know.
The difference in usage of adapter to the tutorials that I have seen is that, in my app, the listview is not the only view displayed in the activity so that may be the/a problem.
You're going about this in the wrong manner. In your EntryAdapter:
#Override
public View getView(int position, View convertView, ViewGroup container {
Entry entry = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext())
.inflate(R.layout.activity_create_event, container, false);
}
...
In that inflate statement you are inflating the entire XML layout for your Activity (R.layout.activity_create_event) for every row of your ListView. Instead, you should be using a much simpler layout containing just your TextView in a separate layout XML file, for example let's call this res/layout/list_row.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/new_entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Placeholder Text" />
</LinearLayout>
Then replace the layout you're inflating in getView() like so:
#Override
public View getView(int position, View convertView, ViewGroup container {
Entry entry = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext())
.inflate(R.layout.list_row, container, false);
}
...
I'd also recommend looking into RecyclerView as that has now superceded ListView.

Android Fragment transaction from list's element

I have an ArrayAdapter MealAdapter and list's element meal_item, there are a few textviews and FrameLayout. I want to put fragment MealDetails in this FrameLayout by click on imageview. MealAdapter isn't used in Activity but in another fragment.
My item is:
<?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:background="#fff">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!--icon-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/meal_number_icon"
android:layout_width="70dp"
android:layout_height="70dp"
android:padding="6dp"
android:src="#drawable/ic_menu_gallery"
android:layout_centerInParent="true" />
</RelativeLayout>
</LinearLayout>
<!--name-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/protein_source_name"
android:text="sdfsdfsdf"
android:textSize="15sp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fat_source_name"
android:textSize="15sp"
android:textStyle="bold"
android:text="sdfsdfsdf"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/carb_source_name"
android:textSize="15sp"
android:textStyle="bold"
android:text="sdfsdfsdf"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/vegetable_source_name"
android:textSize="15sp"
android:textStyle="bold"
android:text="sdfsdfsdf"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/protein_source_weight"
android:text="sdfsdfsdf"
android:textSize="15sp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fat_source_weight"
android:textSize="15sp"
android:textStyle="bold"
android:text="sdfsdfsdf"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/carb_source_weight"
android:textSize="15sp"
android:textStyle="bold"
android:text="sdfsdfsdf"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/vegetable_source_weight"
android:textSize="15sp"
android:textStyle="bold"
android:text="sdfsdfsdf"/>
</LinearLayout>
</LinearLayout>
<!-- refreshMeal -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/meal_accept_icon"
android:layout_width="70dp"
android:layout_height="70dp"
android:padding="6dp"
android:src="#drawable/ic_menu_gallery"
android:layout_centerInParent="true" />
</RelativeLayout>
</LinearLayout>
<!--info-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<RelativeLayout
android:id="#+id/info_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/meal_details_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#drawable/ic_menu_gallery"
android:layout_centerInParent="true"
android:tint="#8a000000"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
<FrameLayout
android:id="#+id/meal_frameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginRight="4dp">
</FrameLayout>
</LinearLayout>
and adapter:
public class MealAdapter extends ArrayAdapter {
List list = new ArrayList();
private Context context;
public MealAdapter(Context context, int resource) {
super(context, resource);
this.context = context;
}
public static class DataHandler{
ImageView numberIcon;
ImageView detailsIcon;
ImageView acceptIcon;
TextView proteinSourceName;
TextView fatSourceName;
TextView carbSourceName;
TextView vegetableSourceName;
TextView proteinSourceWeight;
TextView fatSourceWeight;
TextView carbSourceWeight;
TextView vegetableSourceWeight;
FrameLayout detailsLayout;
}
#Override
public void add(Object object) {
super.add(object);
list.add(object);
}
#Override
public int getCount() {
return this.list.size();
}
#Override
public Object getItem(int position) {
return this.list.get(position);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
final DataHandler dataHandler;
if(convertView==null){
LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.meal_item,parent,false);
dataHandler = new DataHandler();
dataHandler.numberIcon = (ImageView) view.findViewById(R.id.meal_number_icon);
dataHandler.detailsIcon = (ImageView) view.findViewById(R.id.meal_details_icon);
dataHandler.acceptIcon = (ImageView) view.findViewById(R.id.meal_accept_icon);
dataHandler.proteinSourceName =(TextView) view.findViewById(R.id.protein_source_name);
dataHandler.fatSourceName =(TextView) view.findViewById(R.id.fat_source_name);
dataHandler.carbSourceName =(TextView) view.findViewById(R.id.carb_source_name);
dataHandler.vegetableSourceName =(TextView) view.findViewById(R.id.vegetable_source_name);
dataHandler.proteinSourceWeight =(TextView) view.findViewById(R.id.protein_source_weight);
dataHandler.fatSourceWeight =(TextView) view.findViewById(R.id.fat_source_weight);
dataHandler.carbSourceWeight =(TextView) view.findViewById(R.id.carb_source_weight);
dataHandler.vegetableSourceWeight =(TextView) view.findViewById(R.id.vegetable_source_weight);
dataHandler.detailsLayout=(FrameLayout)view.findViewById(R.id.meal_frameLayout);
view.setTag(dataHandler);
}
else{
dataHandler=(DataHandler) view.getTag();
}
LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final MealDataProvider mealDataProvider;
mealDataProvider = (MealDataProvider) this.getItem(position);
dataHandler.numberIcon.setImageResource(mealDataProvider.getNumberIcon());
dataHandler.detailsIcon.setImageResource(mealDataProvider.getShowIcon());
dataHandler.acceptIcon.setImageResource(mealDataProvider.getAcceptIcon());
dataHandler.proteinSourceName.setText(mealDataProvider.getProteinSourceName());
dataHandler.fatSourceName.setText(mealDataProvider.getFatSourceName());
dataHandler.carbSourceName.setText(mealDataProvider.getCarbSourceName());
dataHandler.vegetableSourceName.setText(mealDataProvider.getVegetableSourceName());
dataHandler.proteinSourceWeight.setText(mealDataProvider.getProteinSourceWeight());
dataHandler.fatSourceWeight.setText(mealDataProvider.getFatSourceWeight());
dataHandler.carbSourceWeight.setText(mealDataProvider.getCarbSourceWeight());
dataHandler.vegetableSourceWeight.setText(mealDataProvider.getVegetableSourceWeight());
dataHandler.detailsIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(),"click",Toast.LENGTH_LONG).show();
MealDetails mealDetails = new MealDetails();
FragmentTransaction ft = ((FragmentActivity)context).getSupportFragmentManager().beginTransaction();
ft.replace(dataHandler.detailsLayout.getId(),mealDetails);
}
});
return view;
}
}
onClick method works (there is "click" toast) but nothing else happens, the is nothing in AndroidMonitor if I click dataHandler.detailsIcon.
How is it possible to put fragment in framelayout in list's element?
Edit
I forgot about commit(), so now it works but only for FrameLayout from first list's element. It doesn't matter which list's element I click, fragment MealDetails is always place in the same FrameLayout.
Probably because every FrameLayout have the same id(?) is there any way to solve it?
You have forgotten to commit() your FragmentTransaction.
If you don't commit it, nothing will happen. Android Studio should be warning about this as well.

Errors on Adding ListView to Fragment

I am trying to add a listview to an existing android fragment. I made a layout xml file for items in the listview, modified the previous fragment.xml, wrote a new arrayadapter and added something to the fragment.java. But whenever I call the notifyDataSetChanged() method in the fragment.java, I got an error: android.content.res.Resources$NotFoundException: String resource ID #0x0, and then the app crashes. Below please find my codes.
layout.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"
android:paddingBottom="7dp">
<ImageView
android:layout_width="72dp"
android:layout_height="72dp"
android:id="#+id/iv_checkIcon"
android:layout_margin="7dp"
android:background="#EEEEEE"
android:contentDescription="Provider icon"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_toRightOf="#id/iv_checkIcon"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:layout_marginLeft="5dp"
>
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="#+id/tv_checkName"
android:text="Provider name"
android:textSize="20dp"
/>
<TextView
android:layout_width="250dp"
android:layout_height="wrap_content"
android:id="#+id/tv_checkInDate"
android:text="Check In Date"
android:textSize="14dp"
android:textColor="#888888"
android:layout_marginTop="4dp"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_checkPoints"
android:layout_alignParentRight="true"
android:text="100分"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:textSize="18dp"
android:textColor="#CF1A12"
/>
</RelativeLayout>
fragment.xml
<FrameLayout 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=".fragment.MeActivity">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/me_bg2"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/fake_head"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:id="#+id/iv_fake_head"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/iv_fake_head"
android:text="--"
android:textSize="22dp"
android:textColor="#ffffff"
android:layout_marginTop="15dp"
android:id="#+id/tv_name"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/tv_name"
android:text="积分"
android:textColor="#ffffff"
android:layout_marginTop="15dp"
android:textSize="20dp"
android:background="#drawable/me_points_bg"
android:id="#+id/tv_points"
/>
</RelativeLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lv_histories"
android:layout_gravity="bottom" />
</FrameLayout>
Arrayadapter.java
public class RecordArrayAdapter extends ArrayAdapter<CheckInRecord.CheckInRec> {
private int resourceId;
private Context context;
private List<CheckInRecord.CheckInRec> checkInRecList;
public RecordArrayAdapter(Context context, int resourceId, List<CheckInRecord.CheckInRec> checkInRecList)
{
super(context, resourceId, checkInRecList);
this.resourceId = resourceId;
this.context = context;
this.checkInRecList = checkInRecList;
}
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(resourceId, parent, false);
}
TextView textViewName = (TextView) convertView.findViewById(R.id.tv_checkName);
TextView textViewCheckInDate = (TextView) convertView.findViewById(R.id.tv_checkInDate);
TextView textViewPoints = (TextView) convertView.findViewById(R.id.tv_checkPoints);
ImageView imageViewIcon = (ImageView) convertView.findViewById(R.id.iv_checkIcon);
CheckInRecord.CheckInRec checkInRec = checkInRecList.get(position);
textViewName.setText(checkInRec.providerName);
textViewCheckInDate.setText(checkInRec.checkInDate);
textViewPoints.setText(checkInRec.providerPoints);
ImageLoader.getInstance().displayImage(checkInRec.providerIcon, imageViewIcon, Utility.displayImageOptions);
return convertView;
}
public int getIsPrize(int position) {return (this.checkInRecList.get(position).isPrize);}
}
fragment.java
public class MeFragment extends Fragment implements ApiRequestDelegate {
private TextView textViewName;
private TextView textViewPoints;
private ProgressDialog progressDialog;
private RecordArrayAdapter recordArrayAdapter;
private List<CheckInRecord.CheckInRec> checkInRecList = new ArrayList<CheckInRecord.CheckInRec>();
public MeFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ApiManager.getInstance().checkInHistories(AppDataManager.getInstance().getUserToken(), AppDataManager.getInstance().getUserPhone(),
Utility.getPictureSize(), this);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View fragmentView = inflater.inflate(R.layout.fragment_me, container, false);
textViewName = (TextView) fragmentView.findViewById(R.id.tv_name);
textViewPoints = (TextView) fragmentView.findViewById(R.id.tv_points);
ListView listViewRec = (ListView) fragmentView.findViewById(R.id.lv_histories);
recordArrayAdapter = new RecordArrayAdapter(this.getActivity(), R.layout.row_record, checkInRecList);
listViewRec.setAdapter(recordArrayAdapter);
return fragmentView;
}
#Override
public void apiCompleted(ApiResult apiResult, HttpRequest httpRequest) {
if (progressDialog!=null){
progressDialog.dismiss();
}
if (!apiResult.success){
ApiManager.handleMessageForReason(apiResult.failReason, getActivity());
return;
}
CheckInRecord checkInRecord = (CheckInRecord) apiResult.valueObject;
if (checkInRecord != null){
textViewName.setText(checkInRecord.userName);
textViewPoints.setText(String.format("积分%d分", checkInRecord.userPoints));
this.checkInRecList.clear();
this.checkInRecList.addAll(checkInRecord.checkInRecList);
recordArrayAdapter.notifyDataSetChanged();
}
}
}
Make sure checkInRec.providerName, checkInRec.checkInDate and checkInRec.providerPoints values that you set as TextView text have a type of String and cast it to String if it's integer.
// cast to String if checkInRec.checkInDate is integer
textViewPoints.setText(String.valueOf(checkInRec.providerPoints));
UPD:
It seems ListView is located behind header view. Try to change root view of fragment.xml from FrameLayout to LinearLayout with android:orientation="vertical" attribute. Like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".fragment.MeActivity">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/me_bg2"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/fake_head"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:id="#+id/iv_fake_head"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/iv_fake_head"
android:text="--"
android:textSize="22dp"
android:textColor="#ffffff"
android:layout_marginTop="15dp"
android:id="#+id/tv_name"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/tv_name"
android:text="积分"
android:textColor="#ffffff"
android:layout_marginTop="15dp"
android:textSize="20dp"
android:background="#drawable/me_points_bg"
android:id="#+id/tv_points"
/>
</RelativeLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lv_histories"
android:layout_gravity="bottom" />
</LinearLayout>

How to select a textview having an id from a custom listview in android?

I have a custom listview having the following layout:
I have defined an onClick method for 'qadd' button and 'qsub' button in xml using android:onClick property.
In my Activity class how do I select the 'quantity' textView for current position?
I tried this but it only updates 'quantity' textView at first position.
public void addClicked(View v) {
final int position = list.getPositionForView((View) v.getParent().getParent());
long rec_id = adapter.getItemId(position);
JSONObject o = (JSONObject) fullList.get(rec_id + "");
TextView q = (TextView)findViewById(R.id.quantity);
int quantity = Integer.parseInt((String) q.getText());
quantity++;
q.setText(quantity + "");
try {
String price = o.getString("price");
subtotal = subtotal + (Integer.parseInt(price) * quantity);
total_text = (TextView) findViewById(R.id.subtotal);
total_text.setText(subtotal + "");
// Log.d("position", price);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The above code goes wrong at this line:
TextView q = (TextView)findViewById(R.id.quantity);
It always selects the textView at first position.
Please ask if any other piece of code is needed.
Entire custom_list_layout.xml is as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/checkBox1" >
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dip"
android:background="#drawable/image_bg"
android:padding="3dip" >
<ImageView
android:id="#+id/item_img"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="2.5"
android:orientation="vertical" >
<TextView
android:id="#+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item name"
android:textColor="#040404"
android:textSize="15dip"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="#+id/item_desc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Item description"
android:textColor="#343434"
android:textSize="10dip" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.75"
android:gravity="right"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal" >
<TextView
android:id="#+id/rupee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="Rs. "
android:textColor="#EC1669"
android:textSize="10dip"
android:textStyle="bold" />
<TextView
android:id="#+id/item_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dip"
android:gravity="right"
android:text="0 "
android:textColor="#EC1669"
android:textSize="10dip"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="bottom|right" >
<Button
android:id="#+id/qadd"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="24dp"
android:minWidth="24dp"
android:onClick="addClicked"
android:text="+" />
<TextView
android:id="#+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="#+id/qsub"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="24dp"
android:minWidth="24dp"
android:onClick="subClicked"
android:text="-" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Link to my entire project.
Your textview to address should be the one within the list item row you specify in XML for the list adapter.
If you're using a custom adapter, within getView you should wire up the textview as follows:
// create a new TextView for each item referenced by the Adapter
public View getView(final int position, View convertView, final ViewGroup parent) {
View listView = convertView;
if (listView == null) { // if it's not recycled, initialize some attributes
// get layout from xml
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listView = inflater.inflate(R.layout.browse_product_item, null);
}
final ListProductHolder holder;
if (listView.getTag() != null) {
holder = (listProductHolder) listView.getTag();
} else {
holder = new listProductHolder();
listView.setTag(holder);
holder.text= (TextView) listView.findViewById(R.id.quantity);
One you have the proper TextView from within each item in the listView, then it's a simple process to wire up an OnClickListener for that view and respond to those click events.

Categories