Android Studio with Java: ArrayAdapter not working correctly - java

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.

Related

Android Java Cannot find local variable

My MainActivity
public class MainActivity extends AppCompatActivity {//My MainActivity
ArrayList<Response> responses = new ArrayList<>();
private String BASE_URL = "https://covid-193.p.rapidapi.com/";
RecyclerView recyclerView;
MyRecycleViewAdapter myRecycleViewAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycleViewCovid);
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create());//this is convert json
Retrofit retrofit = builder.build();
Covid19WebApi covid19WebApi = retrofit.create(Covid19WebApi.class);
Call<Covid19Model> call = covid19WebApi.getData();//this is call api interfacee method
call.enqueue(new Callback<Covid19Model>() {
#Override
public void onResponse(Call<Covid19Model> call, retrofit2.Response<Covid19Model> response) {
responses = (ArrayList<Response>) response.body().getResponse();//This is my Api response 232 items
LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(layoutManager);
myRecycleViewAdapter = new MyRecycleViewAdapter(responses);//My Adapter passing data in my Adapter
recyclerView.setAdapter(myRecycleViewAdapter);
}
#Override
public void onFailure(Call<Covid19Model> call, Throwable t) {
Toast.makeText(MainActivity.this,t.getLocalizedMessage(),Toast.LENGTH_LONG).show();//this is toast message failure
}
});
}
My Adapter
public class MyRecycleViewAdapter extends RecyclerView.Adapter<MyRecycleViewAdapter.ViewHolder>
{
private ArrayList<Response> responseArrayList;
public MyRecycleViewAdapter ( ArrayList<Response> responseArrayList) {
this.responseArrayList = responseArrayList;//Response data items in My Adapter
}
#NonNull
#Override
public MyRecycleViewAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());//Api passing data added this layout
View view = layoutInflater.inflate(R.layout.row_layout,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyRecycleViewAdapter.ViewHolder holder, int position) {
holder.setData(responseArrayList.get(position));//Api Items List
}
#Override
public int getItemCount() {
return responseArrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView continent;
TextView country;
TextView population;
TextView casesNew;
TextView caseActive;
TextView caseCritical;
TextView caseRecovered;
TextView caseM_pop;
TextView caseTotal;
TextView deathsNew;
TextView deathsM_pop;
TextView deathsTotal;
TextView testsM_pop;
TextView testsTotal;
TextView day;
TextView time;
TextView Deaths;
TextView Tests;
TextView Cases;
public ViewHolder(#NonNull View itemView) {//My ViewHolder Connect Api data and View Layout TextView
super(itemView);
continent = itemView.findViewById(R.id.continentName);//My layout Textview id
country = itemView.findViewById(R.id.countryName);
population =itemView.findViewById(R.id.populationName);
casesNew = itemView.findViewById(R.id.CaseNewName);
caseActive = itemView.findViewById(R.id.casesActiveName);
caseCritical=itemView.findViewById(R.id.criticalName);
caseRecovered=itemView.findViewById(R.id.recoveredName);
caseM_pop = itemView.findViewById(R.id.M_popName);
caseTotal = itemView.findViewById(R.id.totalCases);
deathsNew = itemView.findViewById(R.id.deathsNewName);
deathsM_pop = itemView.findViewById(R.id.deaths1M_pop);
deathsTotal = itemView.findViewById(R.id.totalDeaths);
testsM_pop = itemView.findViewById(R.id.tests1M_pop);
testsTotal = itemView.findViewById(R.id.testsTotal);
day = itemView.findViewById(R.id.dayName);
time = itemView.findViewById(R.id.timeName);
Cases = itemView.findViewById(R.id.Cases);
Deaths = itemView.findViewById(R.id.Deaths);
Tests = itemView.findViewById(R.id.Tests);
}
private void setData(Response response){
Response res = response;
continent.setText(res.getContinent());//Debugging this TextView Api data is null
country.setText(res.getCountry());
population.setText(res.getPopulation());
casesNew.setText(res.getCases().getNew());
caseActive.setText(res.getCases().getActive());
caseCritical.setText(res.getCases().getCritical());
caseRecovered.setText(res.getCases().getRecovered());
caseM_pop.setText(res.getCases().get1MPop());
caseTotal.setText(res.getCases().getTotal());
deathsM_pop.setText(res.getDeaths().get1MPop());
deathsNew.setText(res.getDeaths().getNew());
deathsTotal.setText(res.getDeaths().getTotal());
testsM_pop.setText(res.getTests().get1MPop());
testsTotal.setText(res.getTests().getTotal());
day.setText(res.getDay());
time.setText(res.getTime());
Tests.setText(res.getTests().toString());
Cases.setText(res.getCases().toString());
Deaths.setText(res.getDeaths().toString());
}
}
}
My Problems
Please Help me What is the Problem?
I pull the data with the Api, but when I debug, I see that the data comes to My MainActivity and is added to the Adapter from there, but when the data reaches the Viewholder, it becomes null when set to the TextView. I don't understand what the problem is.
My Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#8BC34A"
android:orientation="vertical">
<TextView
android:id="#+id/continentName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:hint="continent"
android:textColor="#color/black"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/countryName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="country"
android:textColor="#color/black"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/continentName" />
<TextView
android:id="#+id/populationName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="population"
android:textColor="#color/black"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/countryName" />
<TextView
android:id="#+id/dayName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="day"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/populationName" />
<TextView
android:id="#+id/timeName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="time"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/dayName" />
<TextView
android:id="#+id/CaseNewName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="new"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/timeName" />
<TextView
android:id="#+id/casesActiveName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="active"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/CaseNewName" />
<TextView
android:id="#+id/criticalName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="critical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/casesActiveName" />
<TextView
android:id="#+id/recoveredName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="recovered"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/criticalName" />
<TextView
android:id="#+id/M_popName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="1M_pop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/recoveredName" />
<TextView
android:id="#+id/totalCases"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="total"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/M_popName" />
<TextView
android:id="#+id/deaths1M_pop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="1M_pop(deaths)"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/totalCases" />
<TextView
android:id="#+id/deathsNewName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="new(Deaths)"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/deaths1M_pop" />
<TextView
android:id="#+id/totalDeaths"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="total(deaths)"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/deathsNewName" />
<TextView
android:id="#+id/tests1M_pop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="1M_pop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/totalDeaths" />
<TextView
android:id="#+id/testsTotal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:hint="total(tests)"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tests1M_pop" />
<TextView
android:id="#+id/Deaths"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="Deaths"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/testsTotal" />
<TextView
android:id="#+id/Tests"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="Tests"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Deaths" />
<TextView
android:id="#+id/Cases"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:hint="Cases"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Tests" />
</LinearLayout>"

Send data inside recyclerview with button in Fragment

I have 2 layouts where first layout for Fragment and second layout for RecyclerView. I have created CardView in RecyclerView, every CardView has some data. And I have created button below RecyclerView where the function of button for sending Data on CardView. My problem is I don't know how to send data when i click button in Fragment.
Fragment Layout :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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/container_daftar_alamat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:scrollbars="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".ui.menu.menu_signin.menu.MenuAddressFragment">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<Button
android:id="#+id/buttonMoveTambahAlamat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:background="#null"
android:drawableStart="#drawable/ic_tambah_alamat"
android:drawablePadding="10dp"
android:text="Tambah Alamat"
android:textAllCaps="false"
android:textColor="#color/colorRed"
android:textSize="14sp"
android:textStyle="normal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="Daftar Alamat"
android:textColor="#color/colorRed"
android:textSize="14sp"
android:textStyle="bold" />
</RelativeLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rc_tambah_alamat"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/gunakanAlamat"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginStart="10dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="100dp"
android:background="#drawable/bg_button_red"
android:text="Gunakan Alamat"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:textSize="16sp" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</FrameLayout>
RecyclerView Layout :
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="12dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="12dp"
android:orientation="vertical"
app:cardBackgroundColor="#color/colorWhite"
app:cardCornerRadius="4dp"
app:cardElevation="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<RadioButton
android:id="#+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/radioButton">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp">
<TextView
android:id="#+id/nameUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:justificationMode="inter_word"
android:text="Muhammad Rafi Bahrur Rizki"
android:textColor="#color/colorBlack"
android:textSize="14sp"
android:textStyle="bold"
tools:ignore="UnusedAttribute" />
<TextView
android:id="#+id/addressUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:maxLines="1"
android:text="(Alamat Kantor)"
android:textSize="14sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<TextView
android:id="#+id/streetName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:justificationMode="inter_word"
android:text="Mangga Dua Square Lantai 1 Jakarta,"
android:textColor="#color/textColorSaksFifthAvenue"
android:textSize="14sp"
tools:ignore="UnusedAttribute" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<TextView
android:id="#+id/blokAddressUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:text="Blok C no. 148 - 150"
android:textColor="#color/textColorSaksFifthAvenue"
android:textSize="14sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<TextView
android:id="#+id/cityAddressUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:text="DKI Jakarta - 15025"
android:textColor="#color/textColorSaksFifthAvenue"
android:textSize="14sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<TextView
android:id="#+id/countryAddressUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:text="DKI Jakarta - 15025"
android:textColor="#color/textColorSaksFifthAvenue"
android:textSize="14sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<TextView
android:id="#+id/phoneUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:text="0812951825"
android:textColor="#color/textColorSaksFifthAvenue"
android:textSize="14sp" />
</TableRow>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<Button
android:id="#+id/buttonEdit"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginStart="8dp"
android:drawableStart="#drawable/ic_edit"
android:text="Edit"
android:textAllCaps="false"
android:textSize="12sp" />
<Button
android:id="#+id/buttonDelete"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginStart="10dp"
android:layout_toEndOf="#id/buttonEdit"
android:drawableStart="#drawable/ic_delete"
android:text="Hapus"
android:textAllCaps="false"
android:textSize="12sp" />
</RelativeLayout>
</TableLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
When i select RadioButton, only data in RadioButton will be obtained. And when i click button, the data will be send to another activity.
Adapter RecyclerView :
public class AdapterGetAddress extends RecyclerView.Adapter<AdapterGetAddress.ViewHolder> {
Context context;
private List<ModelGetAddress> modelGetAddressList;
private BaseApiService baseApiService;
private int previousSelected = -1;
public AdapterGetAddress(Context context, List<ModelGetAddress> modelGetAddressList) {
this.context = context;
this.modelGetAddressList = modelGetAddressList;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_address, parent, false);
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, final int position) {
final ModelGetAddress adapterAddress = modelGetAddressList.get(position);
}
#Override
public int getItemCount() {
return modelGetAddressList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView nameUser, addressUser, streetName, blokAddressUser, cityAddressUser, countryUser, phoneUser;
private RadioButton radioButton;
public ViewHolder(#NonNull View itemView) {
super(itemView);
nameUser = itemView.findViewById(R.id.nameUser);
addressUser = itemView.findViewById(R.id.addressUser);
streetName = itemView.findViewById(R.id.streetName);
countryUser = itemView.findViewById(R.id.countryAddressUser);
blokAddressUser = itemView.findViewById(R.id.blokAddressUser);
cityAddressUser = itemView.findViewById(R.id.cityAddressUser);
phoneUser = itemView.findViewById(R.id.phoneUser);
radioButton = itemView.findViewById(R.id.radioButton);
radioButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
previousSelected = getAdapterPosition();
notifyItemRangeChanged(0, modelGetAddressList.size());
// I want this All String will be send when i click button in Fragment
String getNameUser = nameUser.getText().toString();
String getAddressUser = streetName.getText().toString();
String getCountryUser = countryUser.getText().toString();
String getBlokUser = blokAddressUser.getText().toString();
String getCityUser = cityAddressUser.getText().toString();
String getPhoneUser = phoneUser.getText().toString();
}
});
}
}
}
Code in Fragment :
public class MenuAddressFragment extends Fragment {
private Button gunakanAlamat;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_menu_address, container, false);
gunakanAlamat = view.findViewById(R.id.gunakanAlamat);
gunakanAlamat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Here's were i'm stuck
}
});
return view;
}
}
You can solve this via an interface.
public interface ItemClickedCallback{
void onItemClicked(String nameUser //, the other data you wanna pass );
}
pass it through the constructor of your adapter.
Use this adapter constructor
ItemClickedCallback callback;
public AdapterGetAddress(Context context, List<ModelGetAddress> modelGetAddressList, ItemClickedCallback callback) {
this.context = context;
this.modelGetAddressList = modelGetAddressList;
this.callback= callback;
}
When checking a radio button of any item; call the interface method, like this.
radioButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
previousSelected = getAdapterPosition();
notifyItemRangeChanged(0, modelGetAddressList.size());
// I want this All String will be send when i click button in Fragment
String getNameUser = nameUser.getText().toString();
String getAddressUser = streetName.getText().toString();
String getCountryUser = countryUser.getText().toString();
String getBlokUser = blokAddressUser.getText().toString();
String getCityUser = cityAddressUser.getText().toString();
String getPhoneUser = phoneUser.getText().toString();
callback.onItemClicked(getNameUser //, the rest..);
}
});
Now, when you initialize the adapter in the fragment, create the new interface via the constructor and assign the data to class variables like this.
public class MenuAddressFragment extends Fragment {
private Button gunakanAlamat;
private String nameUser_;
// the rest...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_menu_address, container, false);
AdapterGetAddress adapter = new AdapterGetAddress(getActivity, list, new ItemClickedCallback() {
#Override
public void onItemClicked(String userName //, the rest..) {
// assign to the class variable like this
nameUser_ = userName;
// the rest..
}
}););
gunakanAlamat = view.findViewById(R.id.gunakanAlamat);
gunakanAlamat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Use the data as you want..
}
});
return view;
}
}

Add RadioButton inside RadioGroup in ListView

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>

why cant I Update text dynamically in fragments?

Here's my Xml code
<?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"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="Well heres the secret !"
android:textAlignment="center"
android:textColor="#color/bluishblack"
android:textSize="25sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="1. Secret is simple,the cards we showed you contained key numbers(very first number of the list)"
android:textAlignment="center"
android:textSize="23sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="' 2 , 4 , 1 , 16 , 32 , 8 , 64 ' "
android:textAlignment="center"
android:textColor="#color/dgreen"
android:textSize="23sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="2. Above are the key numbers. For every card that contains spectator's number you add key number(First number of card) of respective card. "
android:textAlignment="center"
android:textSize="23sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="3. And thats how you Read People's mind ! As easy as that ! "
android:textAlignment="center"
android:textSize="23sp" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<Button
android:id="#+id/play_again"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:onClick="playAgain"
android:text="Play again !"
android:textColor="#color/dgreen"
/>
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginBottom="8dp"
android:background="#drawable/googleg_standard_color_18" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="Your rating helps us to put more efforts i future devlopment,also we get to know more aboout your experiences and Views ! "
android:textAlignment="center"
android:textSize="20sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="99dp"
android:text="Rate now !" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Still Here? Do you want to amaze your friends and family.There are many awesome tricks which will blow away peoples mind yet so simple to do See below recommendations now "
android:textAlignment="center"
android:textSize="20sp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="Do Want to amaze your Freinds !!"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="26sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:background="#android:color/darker_gray" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:layout_marginTop="8dp"
android:text="Here are some of our Suggestions :- "
android:textColor="#color/colorPrimaryDark"
android:textSize="16sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_margin="8dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:background="#android:color/darker_gray" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="8dp"
android:text="Swipe to see more --->"
android:textAlignment="center"
android:textSize="22sp" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="500dp">
</android.support.v4.view.ViewPager>
</LinearLayout>
</ScrollView>
here's my java code of fragment
public class Fragment1 extends Fragment {
private TextView tv;
#Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
Intent n = new Intent(Intent.CATEGORY_BROWSABLE,Uri.parse("http://amzn.to/2rpR35P"));
ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.pager_layout, container, false);
tv = (TextView)rootView.findViewById(R.id.textDes);
tv.setText("text to set dynamically");
return inflater.inflate(R.layout.pager_layout, container, false);
}
}
Here's code for pager Adapter
public class PagerAdapter extends FragmentPagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
if (position == 0) {
return new Fragment1();
}
if (position == 1) {
return new Fragment2();
}
if (position == 2) {
return new Fragment3();
} else {
return new Fragment4();
}
}
#Override
public int getCount() {
return 4;
}
}
here's code for layout of pager
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="24dp"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/ic_icon"
android:id="#+id/image_book"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="This is description"
android:textSize="20sp"
android:id="#+id/textDes"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
<Button
android:id="#+id/getItNow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Start amazing your friends !"
android:textAlignment="center"
/>
</LinearLayout>
</LinearLayout>
How can i change text and images dynamically also i've three more fragments that use same layout(pager_layout) also can i add different intents or not?
You have to set text in your text view inside onViewCreated() method and not in onCreateView() in your fragment class:
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.pager_layout, container, false);
return view;
}
TextView tv;
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
tv = (TextView) view.findViewById(R.id.textDes);
tv.setText("text to set dynamically");
...
}

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.

Categories