Wenas People, I have a Fragment where the listView is. And I do not understand why it does not add to the list. I'm going to try to be as clear as possible in the code because I can not think of anything else anymore. When I debug it appears that it is always in position 0. That is, what I already have is replaced.
From Here it is sent to the Fragment; It is an activity, but since I did not know how to pass from an Activity to the fragment, I did an intent to the activity where the Fragment is. (I do not know the correct way to do it, but the data is received and it shows me the data in the ListView view). I hope someone can give me a hand.
Activity2
public void guardar(View view) {
Intent i = new Intent( this, MainActivity.class);
edit = findViewById(R.id.editText);
String number= edit.getText().toString();
String imagenUri = String.valueOf(getImageUri());
i.putExtra("imagen",imagenUri);
i.putExtra("tara", number);
startActivity(i);
}
Fragment Tab1
peso = extras.getString("tara");
uri = extras.getString("imagen");
ArrayList<ImagenesFoto> results = new ArrayList<ImagenesFoto>();
ImagenesFoto sr1 = new ImagenesFoto();
sr1.setTaraImagen(peso);
sr1.setRutaImagen(uri);
results.add(sr1);
listView= view.findViewById(R.id.listView);
GridViewAdapter listViewAdapter= new GridViewAdapter(getContext(), results);
listView.setAdapter(listViewAdapter);
Adapter
public class GridViewAdapter extends BaseAdapter {
private ArrayList<ImagenesFoto> searchArrayList;
private LayoutInflater mInflater;
GridViewAdapter(Context context, ArrayList<ImagenesFoto> results) {
this.searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return searchArrayList.size();
}
@Override
public Object getItem(int position) {
return searchArrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.grid_item,null);
holder = new ViewHolder();
holder.pesoTara = convertView.findViewById(R.id.pesoTara);
holder.img = convertView.findViewById(R.id.img);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.img.setImageURI(Uri.parse(searchArrayList.get(position).getRutaImagen()));
holder.pesoTara.setText(searchArrayList.get(position).getTaraImagen());
return convertView;
}
static class ViewHolder {
TextView pesoTara;
ImageView img;
}
}
Data Model
public class ImagenesFoto {
public String rutaImagen;
public String taraImagen;
public String getRutaImagen() {
return rutaImagen;
}
public void setRutaImagen(String rutaImagen) {
this.rutaImagen = rutaImagen;
}
public String getTaraImagen() {
return taraImagen;
}
public void setTaraImagen(String taraImagen) {
this.taraImagen = taraImagen;
}
}
ListView XML
<LinearLayout
android:id="@+id/tab1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
Item of the Adapter
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:id="@+id/cardView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10">
<ImageView
android:layout_weight="2"
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="120dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="8"
android:orientation="horizontal">
<TextView
android:layout_weight="1"
android:id="@+id/textTara"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Tara"
android:textAlignment="center"
android:textColor="#000" />
<TextView
android:layout_weight="1"
android:id="@+id/pesoTara"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:hint="Tara"
android:textAlignment="center"/>
</LinearLayout>
</LinearLayout>
If you got here even though you can not answer me, I thank you for your time. Hugs