Use a Spinner to change a TextView Android - java

I'm trying to use a spinner selection to change a TextView. Where I think my error is, is that "onItemSelected" "is never used". I'm very new to android/java so i'm having a hard time figuring out why this is happening.
public class activity_game extends AppCompatActivity {
public String myString1 = "Counter will increase every 3 seconds";
public TextView myCounterText;
private Spinner mySpinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
Spinner dropdown = (Spinner)findViewById(R.id.spinner);
String[] items = new String[]{"Select your difficulty!", "Easy", "Medium", "Hard"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
dropdown.setAdapter(adapter);
}
public void onItemSelected(AdapterView<?> parent, View arg1, int pos, long arg3) {
TextView myCounterText = (TextView)findViewById(R.id.myCounter);
Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
if (mySpinner.getSelectedItem().equals("Easy")){
myCounterText.setText("myString1");
}
}
public void toActivityPlay (View view) {
Intent toActivityPlay = new Intent(this, activity_play.class);
startActivity(toActivityPlay);
}
}
Thanks in advance.
Edit: Here is my XML (with unnecessary stuff edited out so that I can post without having "mostly code".
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background"
tools:context="com.example.newpc.fizzbuzz.activity_game">
<TextView
android:id="#+id/counterText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="#string/counter_text"
android:textAlignment="center"
android:textColor="#color/text_shadow"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.229"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<Button
android:id="#+id/startButton"
android:layout_width="239dp"
android:layout_height="73dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:background="#color/FizzBuzz_yellow"
android:text="#string/start_game"
android:textAllCaps="false"
android:textColor="#fff"
android:textSize="36sp"
android:textStyle="bold"
android:shadowColor="#color/text_shadow"
android:shadowDx="2"
android:shadowDy="2"
android:shadowRadius="3"
android:onClick="toActivityPlay"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.863"
app:layout_constraintHorizontal_bias="0.531"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<Spinner
android:id="#+id/spinner"
android:layout_width="240dp"
android:layout_height="28dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:background="#ffffff"
android:dropDownWidth="match_parent"
android:entries="#+id/difficulty"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.137" />
</android.support.constraint.ConstraintLayout>

try this use setOnItemSelectedListener of spinner
Spinner dropdown = (Spinner)findViewById(R.id.spinner);
String[] items = new String[]{"Select your difficulty!", "Easy", "Medium", "Hard"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
dropdown.setAdapter(adapter);
dropdown.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String text=spin.getSelectedItem().toString();
if (text.equals("Easy")){
myCounterText.setText("myString1");
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});

Related

how can disable scroll recyclerView when moving map?

I created a recyclerView that has several items but the problem is when I want to look for the address inside the map
When I scroll through the map, the recycler view shifts and the map loses scrollability.
enter link description here
//Recycler View Adapter Class
public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.Holder> {
private List<FakeAddressList> objectList;
public AddressAdapter(List<FakeAddressList> list) {
this.objectList = list;
}
#NonNull
#Override
public Holder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_address, parent, false);
return new Holder(view);
}
#Override
public void onBindViewHolder(#NonNull Holder holder, int position) {
holder.bindAddressList(objectList.get(position));
holder.imgClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
objectList.remove(position);
notifyDataSetChanged();
}
});
holder.imgEditLocation.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AddAddressFragment addressFragment = new AddAddressFragment();
FragmentTransaction ft = fm.beginTransaction();
Bundle bundle = new Bundle();
bundle.putString("Title", objectList.get(position).getTitle());
bundle.putString("Address", objectList.get(position).getAddress());
bundle.putString("Phone", objectList.get(position).getPhoneNumber());
addressFragment.setArguments(bundle);
ft.replace(R.id.content_view, addressFragment).addToBackStack(null).commit();
}
});
holder.FooterLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AddDataFragment addDataFragment = new AddDataFragment();
FragmentTransaction ft = fm.beginTransaction();
Bundle bundle = new Bundle();
bundle.putString("Title", objectList.get(position).getAddress());
bundle.putString("Address", objectList.get(position).getAddress());
bundle.putString("Phone", objectList.get(position).getPhoneNumber());
addDataFragment.setArguments(bundle);
ft.replace(R.id.content_view, addDataFragment).addToBackStack(null).commit();
}
});
}
#Override
public int getItemCount() {
return objectList.size();
}
public class Holder extends RecyclerView.ViewHolder {
public TextView txtTitle;
public TextView txtSelect;
public TextView txtAddress;
public TextView txtPhone;
public ImageView imgClose;
public ImageView imgEditLocation;
public ConstraintLayout FooterLayout;
public MapView map;
public Holder(#NonNull View itemView) {
super(itemView);
txtTitle = itemView.findViewById(R.id.txtTitle);
txtAddress = itemView.findViewById(R.id.txtAddress);
txtPhone = itemView.findViewById(R.id.txtPhoneNumber);
txtSelect = itemView.findViewById(R.id.txtSelect);
imgClose = itemView.findViewById(R.id.imgClose);
imgEditLocation = itemView.findViewById(R.id.imgEditLocation);
FooterLayout = itemView.findViewById(R.id.FooterLayout);
map = itemView.findViewById(R.id.mapView);
txtTitle.setTypeface(Font_shabnam);
txtAddress.setTypeface(Font_shabnam);
txtPhone.setTypeface(Font_shabnam);
txtSelect.setTypeface(Font_shabnam);
}
public void bindAddressList(final FakeAddressList fakeAddressList) {
txtTitle.setText(fakeAddressList.getTitle());
txtAddress.setText(fakeAddressList.getAddress());
txtPhone.setText(fakeAddressList.getPhoneNumber());
if (fakeAddressList.getLng() != null) {
double lng = Double.valueOf(fakeAddressList.getLng());
double lat = Double.valueOf(fakeAddressList.getLat());
addUserMarker(new LngLat(lng, lat));
map.setFocalPointPosition(
new LngLat(Double.valueOf(fakeAddressList.getLng()), Double.valueOf(fakeAddressList.getLat())), 0.25f);
} else {
LngLat focalPoint;
focalPoint = new LngLat(51.33800, 35.69997);
map.setFocalPointPosition(focalPoint, 0f);
}
map.setZoom(15, 0.25f);
map.getLayers().add(NeshanServices.createBaseMap(NeshanMapStyle.STANDARD_DAY));
}
// This method gets a LngLat as input and adds a marker on that position
private void addUserMarker(LngLat loc) {
Marker marker;
VectorElementLayer userMarkerLayer;
userMarkerLayer = NeshanServices.createVectorElementLayer();
map.getLayers().add(userMarkerLayer);
// Creating marker style. We should use an object of type MarkerStyleCreator, set all features on it
// and then call buildStyle method on it. This method returns an object of type MarkerStyle
MarkerStyleCreator markStCr = new MarkerStyleCreator();
markStCr.setSize(20f);
// markStCr.setBitmap(BitmapUtils.createBitmapFromAndroidBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_marker)));
markStCr.setBitmap(BitmapUtils.createBitmapFromAndroidBitmap(BitmapFactory.decodeResource(itemView.getResources(), R.drawable.ic_cherry)));
MarkerStyle markSt = markStCr.buildStyle();
// Creating user marker
marker = new Marker(loc, markSt);
// Clearing userMarkerLayer
// userMarkerLayer.clear();
// Adding user marker to userMarkerLayer, or showing marker on map!
userMarkerLayer.add(marker);
}
}
}
Recycler View Xml Layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/HeaderLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:background="#drawable/_orange_shape_b_r_l"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<ImageView
android:id="#+id/imgClose"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:src="#drawable/ic_close"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imgEditLocation"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:src="#drawable/ic_edit_location"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="تهرانپارس"
android:textSize="18dp"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="#+id/imgEditLocation"
app:layout_constraintEnd_toStartOf="#+id/imgEditLocation"
app:layout_constraintStart_toEndOf="#+id/imgClose"
app:layout_constraintTop_toTopOf="#+id/imgClose" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/MiddleLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/HeaderLayout"
app:layout_constraintVertical_bias="0.0">
<org.neshan.ui.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="300dp"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imgAddress"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:src="#drawable/ic_address"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/mapView"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/txtAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="خیابان فلانی کوچه 1"
android:textSize="18dp"
app:layout_constraintBottom_toBottomOf="#+id/imgAddress"
app:layout_constraintTop_toTopOf="#+id/imgAddress" />
<ImageView
android:id="#+id/imgPhone"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:src="#drawable/ic_phone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/imgAddress"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/txtPhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="0916123456"
android:textSize="18dp"
app:layout_constraintBottom_toBottomOf="#+id/imgPhone"
app:layout_constraintTop_toTopOf="#+id/imgPhone" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/FooterLayout"
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="8dp"
android:background="#drawable/_black_shape_t_r_l"
android:padding="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/MiddleLayout"
app:layout_constraintVertical_bias="0.0">
<TextView
android:id="#+id/txtSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="انتخاب"
android:textColor="#color/white"
android:textSize="18dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
//Fragment that Load Recyclerview
public class RegisterAddressFragment extends Fragment
implements View.OnClickListener {
private RecyclerView recycler_SelectAddress;
private Button btnNewAddress;
private ProgressBar progressBar;
private ConstraintLayout MainLayout;
private Bundle bundle;
private ArrayList<FakeAddressList> list = new ArrayList<>();
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
this.bundle = getArguments();
if (bundle != null) {
list.add(new FakeAddressList(bundle.getString("Title"),
bundle.getString("Address"),
bundle.getString("Phone"),
bundle.getString("Lng"),
bundle.getString("Lat")));
}
}
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_register_address, container, false);
init(v);
initRecyclerView();
return v;
}
private void init(View v) {
recycler_SelectAddress = v.findViewById(R.id.recycler_SelectAddress);
btnNewAddress = v.findViewById(R.id.btnNewAddress);
btnNewAddress.setOnClickListener(this);
progressBar = v.findViewById(R.id.progressbar);
MainLayout = v.findViewById(R.id.MainLayout);
Thread thread = new Thread() {
#Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
// Do some stuff
progressBar.setVisibility(View.GONE);
MainLayout.setVisibility(View.VISIBLE);
}
});
}
};
thread.start(); //start the thread
}
private void initRecyclerView() {
list.add(new FakeAddressList("اصفهان", "شاهین شهر خیابان نوشین پلاک 1", "987654321"));
list.add(new FakeAddressList("اهواز", "کیانپارس کوچه بنی هاشم پلاک 33", "123456789"));
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
layoutManager.setOrientation(RecyclerView.VERTICAL);
AddressAdapter addressAdapter = new AddressAdapter(list);
recycler_SelectAddress.setLayoutManager(layoutManager);
recycler_SelectAddress.setAdapter(addressAdapter);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnNewAddress:
AddAddressFragment addressFragment = new AddAddressFragment();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content_view, addressFragment).addToBackStack(null).commit();
break;
}
}
}
Fragment Xml File
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="#color/tusi"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ProgressBar
android:id="#+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateTint="#color/red2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/MainLayout"
tools:visibility="visible"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_SelectAddress"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="32dp"
app:layout_constraintBottom_toTopOf="#+id/btnNewAddress"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btnNewAddress"
android:layout_width="match_parent"
android:layout_height="0dp"
android:backgroundTint="#color/navy_blue"
android:insetLeft="0dp"
android:insetTop="0dp"
android:insetRight="0dp"
android:insetBottom="0dp"
android:text="آدرس جدید"
android:textSize="22sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I found the solution :
I added below code in the adapter of the recyclerview
map.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
itemView.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});

Spinner onItemSelectedListener not working?

I use an Android 4.4 and having trouble implementing a spinner.
Problem: the Spinner is not setting the selected item when it's choosen from the list.
according to this and houndred other posts I tried no solution seems to work for me, am I missing something very important that I'm just not realizing?.
What the program should do: I do want to scan for wifi networks and want the user to select a wifi connection from the spinner and set it.
How it is right now: The Spinner shows the available wifi networks but when I click at one it is not selected.
public class SetupActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener
{
private WifiManager wifiManager;
private ListView listView;
private Button buttonScan;
private int size = 0;
private List<ScanResult> results;
private ArrayList<String> arrayList = new ArrayList<>();
private ArrayAdapter adapter;
private Spinner spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_setup );
buttonScan = findViewById(R.id.btnScan);
buttonScan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
scanWifi();
}
});
spinner = findViewById(R.id.spinnerSSID);
listView = findViewById(R.id.wifiList);
wifiManager = (WifiManager) getApplicationContext().getSystemService( Context.WIFI_SERVICE);
if (!wifiManager.isWifiEnabled()) {
Toast.makeText(this, "WiFi is disabled ... We need to enable it", Toast.LENGTH_LONG).show();
wifiManager.setWifiEnabled(true);
}
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(adapter);
scanWifi();
ArrayAdapter<String> adp = new ArrayAdapter<String>(SetupActivity.this,
android.R.layout.simple_spinner_item, arrayList );
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adp);
spinner.setOnItemSelectedListener(this);
}
private void scanWifi() {
arrayList.clear();
registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
wifiManager.startScan();
Toast.makeText(this, "Scanning WiFi ...", Toast.LENGTH_SHORT).show();
}
BroadcastReceiver wifiReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
results = wifiManager.getScanResults();
unregisterReceiver(this);
for (ScanResult scanResult : results) {
arrayList.add(scanResult.SSID + " - " + scanResult.capabilities);
adapter.notifyDataSetChanged();
}
}
};
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
parent.getItemAtPosition(position);
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(position).toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
Here the xml from the layout:
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:tint="?attr/colorControlNormal"
tools:context=".SetupActivity">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DeviceID: 1234abcd"
app:layout_constraintBottom_toTopOf="#+id/textView4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Setup"
android:textSize="36sp"
app:layout_constraintBottom_toTopOf="#+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Content Installed!"
app:layout_constraintBottom_toTopOf="#+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose which Setup"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3" />
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="240dp"
android:layout_marginTop="196dp"
android:text="Local"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="196dp"
android:layout_marginEnd="240dp"
android:text="Online"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="432dp"
android:layout_height="297dp"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView11"
app:srcCompat="#drawable/usbstick" />
<ImageView
android:id="#+id/imageView8"
android:layout_width="11dp"
android:layout_height="252dp"
android:layout_marginTop="150dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4"
app:srcCompat="#color/colorPrimary" />
<EditText
android:id="#+id/etxtContentId"
android:layout_width="445dp"
android:layout_height="44dp"
android:layout_marginTop="63dp"
android:layout_marginEnd="3dp"
android:ems="10"
android:hint="Content ID"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView6"
app:layout_constraintTop_toBottomOf="#+id/textView12" />
<EditText
android:id="#+id/etxtWifiPassword"
android:layout_width="445dp"
android:layout_height="44dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="3dp"
android:ems="10"
android:hint="Wifi Password"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="#+id/imageView6"
app:layout_constraintTop_toBottomOf="#+id/spinnerSSID" />
<Spinner
android:id="#+id/spinnerSSID"
android:layout_width="447dp"
android:layout_height="44dp"
android:layout_marginStart="1dp"
android:layout_marginTop="38dp"
android:spinnerMode="dropdown"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView6"
app:layout_constraintTop_toBottomOf="#+id/etxtContentId" />
<Button
android:id="#+id/btnTestConnection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="24dp"
android:text="Test Connection"
app:layout_constraintStart_toEndOf="#+id/imageView6"
app:layout_constraintTop_toBottomOf="#+id/etxtWifiPassword" />
<Button
android:id="#+id/btnWifiConnect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="24dp"
android:text="Connect"
app:layout_constraintStart_toEndOf="#+id/btnTestConnection"
app:layout_constraintTop_toBottomOf="#+id/etxtWifiPassword" />
<Button
android:id="#+id/btnFinish"
android:layout_width="392dp"
android:layout_height="45dp"
android:layout_marginStart="13dp"
android:layout_marginTop="9dp"
android:layout_marginEnd="43dp"
android:text="Finish and perform online update"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView6"
app:layout_constraintTop_toBottomOf="#+id/btnTestConnection" />
<Button
android:id="#+id/btnScan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="27dp"
android:layout_marginTop="25dp"
android:text="Scan"
app:layout_constraintStart_toEndOf="#+id/btnWifiConnect"
app:layout_constraintTop_toBottomOf="#+id/etxtWifiPassword" />
<ListView
android:id="#+id/wifiList"
android:layout_width="412dp"
android:layout_height="99dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="2dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
The Important parts are there in the activity and layout.xml
private Spinner spinner;
...
spinner = findViewById(R.id.spinnerSSID);
...
ArrayAdapter<String> adp = new ArrayAdapter<String>(SetupActivity.this,
android.R.layout.simple_spinner_item, arrayList );
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adp);
spinner.setOnItemSelectedListener( new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
parent.getItemAtPosition(position);
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(position).toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
parent.getItemAtPosition(0);
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(0).toString(),
Toast.LENGTH_SHORT).show();
}
} );
And in the XML:
<Spinner
android:id="#+id/spinnerSSID"
android:layout_width="447dp"
android:layout_height="44dp"
android:layout_marginStart="1dp"
android:layout_marginTop="38dp"
android:spinnerMode="dropdown"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView6"
app:layout_constraintTop_toBottomOf="#+id/etxtContentId" />
Thanks for any help in advance!
EDIT: I noticed this error in the console:
E/dalvikvm: Could not find class 'android.widget.ThemedSpinnerAdapter', referenced from method androidx.appcompat.widget.AppCompatSpinner$DropDownAdapter.<init>
Also in the debugger I noticed the listener is not getting called...
Use
spinner.setAdapter(adapter);
When you are setting adapter to spinner.
I have checked it.
Note- There is something wrong with your adapter " adp " . I don't know why you have used that.

Why is my ListView showing in one fragment and not the other?

To summarize, I'm building a social media app that displays:
A Newsfeed that shows posts from all user profiles in a given account
A Timeline that shows posts from a specific user profile in the account
I've built a custom BaseAdapter to populate a custom cell within each ListView. The Newsfeed ListView (that populates posts from all users on the account) is populating correctly.
The Timeline ListView (that populates posts from one profile on the account) is not showing. I've set breakpoints to ensure that my ArrayList is not null or empty when populating the Timeline ListView. In addition, breakpoints verify that my custom adapter is, in fact, pulling data from the ArrayList and inflating cells. However, the ListView is simply not visible.
Here is the layout file for my Newsfeed fragment (that is working correctly):
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorSkyPrimary">
<android.support.constraint.ConstraintLayout
android:id="#+id/constraintLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:elevation="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="#+id/input_post"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:backgroundTint="#color/colorSkyPrimary"
android:ems="10"
android:gravity="start|top"
android:hint="#string/what_s_going_on"
android:importantForAutofill="no"
android:inputType="textMultiLine"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:targetApi="o" />
<ImageButton
android:id="#+id/button_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:backgroundTint="#android:color/white"
android:contentDescription="#string/camera_button"
app:layout_constraintBottom_toBottomOf="#+id/input_post"
app:layout_constraintEnd_toEndOf="#+id/input_post"
app:srcCompat="#drawable/camera_icon" />
<Button
android:id="#+id/button_cancel"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="48dp"
android:layout_marginTop="8dp"
android:text="#android:string/cancel"
android:textColor="#color/colorGrassPrimary"
android:visibility="gone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/post_image" />
<Button
android:id="#+id/button_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="48dp"
android:backgroundTint="#color/colorGrassPrimary"
android:text="#string/update"
android:textColor="#color/colorButtonText"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/post_image" />
<ImageView
android:id="#+id/post_image"
android:layout_width="0dp"
android:layout_height="300dp"
android:contentDescription="#string/post_image"
android:scaleType="fitCenter"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/input_post"
tools:srcCompat="#tools:sample/avatars" />
</android.support.constraint.ConstraintLayout>
<ListView
android:id="#+id/list_newsfeed"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/picker_image"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/constraintLayout"
app:layout_constraintVertical_bias="0.0" />
<android.support.constraint.ConstraintLayout
android:id="#+id/picker_image"
android:layout_width="0dp"
android:layout_height="75dp"
android:background="#android:color/white"
android:elevation="16dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<android.support.constraint.Guideline
android:id="#+id/guideline14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<ImageButton
android:id="#+id/button_camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="32dp"
android:layout_marginBottom="8dp"
android:background="#android:color/transparent"
android:contentDescription="#string/camera_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/guideline14"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/camera_icon_large" />
<ImageButton
android:id="#+id/button_gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="#android:color/transparent"
android:contentDescription="#string/gallery_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/guideline14"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.375"
app:srcCompat="#drawable/gallery_icon" />
</android.support.constraint.ConstraintLayout>
Here is the layout file for my Profile fragment (in which the ListView is not showing at runtime)
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorSkyPrimary">
<android.support.constraint.ConstraintLayout
android:id="#+id/constraintLayout4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:elevation="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/profile_photo"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:src="#drawable/male_icon_large"
app:civ_border_color="#android:color/transparent"
app:civ_border_width="2dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/display_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="#string/brelynn_mack"
android:textAlignment="viewStart"
android:textColor="#color/colorTextDark"
android:textSize="18sp"
app:layout_constraintEnd_toStartOf="#+id/button_delete_profile"
app:layout_constraintStart_toEndOf="#+id/profile_photo"
app:layout_constraintTop_toTopOf="#+id/profile_photo" />
<TextView
android:id="#+id/display_timestamp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="#string/may_14_2019_9_59_pm"
android:textAlignment="viewStart"
android:textColor="#color/colorTextLight"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/profile_photo"
app:layout_constraintTop_toBottomOf="#+id/display_name" />
<TextView
android:id="#+id/display_last_location"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="#string/_8741_grouse_run_lane_28314"
android:textAlignment="viewStart"
android:textColor="#color/colorTextPrimary"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/profile_photo"
app:layout_constraintTop_toBottomOf="#+id/display_timestamp" />
<ImageButton
android:id="#+id/button_delete_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:background="#android:color/white"
android:contentDescription="#string/trash_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/trash_icon" />
<ImageButton
android:id="#+id/button_photo"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginStart="8dp"
android:layout_marginBottom="16dp"
android:background="#android:color/white"
android:contentDescription="#string/camera_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/profile_photo"
app:layout_constraintTop_toBottomOf="#+id/profile_photo"
app:srcCompat="#drawable/camera_icon" />
<ImageButton
android:id="#+id/button_family"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:background="#android:color/white"
android:contentDescription="#string/family_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="#drawable/family_icon_small" />
<ImageButton
android:id="#+id/button_gallery"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginEnd="8dp"
android:background="#android:color/transparent"
android:contentDescription="#string/gallery_button"
android:scaleType="fitCenter"
app:layout_constraintEnd_toEndOf="#+id/profile_photo"
app:layout_constraintTop_toBottomOf="#+id/profile_photo"
app:srcCompat="#drawable/gallery_icon" />
</android.support.constraint.ConstraintLayout>
<ListView
android:id="#+id/list_posts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/constraintLayout4" />
Here is my custom adapter:
public class NewsfeedAdapter extends BaseAdapter {
// Class properties
private static final String TAG = "NewsfeedAdapter";
public static final String EXTRA_POSTS = "extra_posts";
public static final String EXTRA_POSITION = "extra_position";
private final Context context;
ArrayList<Post> posts;
Account account;
// Constructor
public NewsfeedAdapter(Context context, ArrayList<Post> posts, Account account) {
this.context = context;
this.posts = posts;
this.account = account;
}
// System generated methods
#Override
public int getCount() {
return posts.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public Object getItem(int position) {
return posts.get(position);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Post post = posts.get(position);
if(convertView == null) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
convertView = layoutInflater.inflate(R.layout.cell_newsfeed, null);
}
TextView profileNameDisplay = convertView.findViewById(R.id.display_profile_name);
String name = post.getPosterName() + " " + account.getFamilyName();
profileNameDisplay.setText(name);
TextView timestampDisplay = convertView.findViewById(R.id.display_timestamp);
SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM dd, yyyy # hh:mm a", Locale.getDefault());
String timestamp = dateFormat.format(post.getTimeStamp());
timestampDisplay.setText(timestamp);
TextView postMessageDisplay = convertView.findViewById(R.id.display_post_message);
postMessageDisplay.setText(post.getPostMessage());
if (post.getHasImage()) {
AccountUtils.loadProfilePhoto(context, convertView, post.getPosterId());
}
else {
ImageView postImage = convertView.findViewById(R.id.display_post_image);
postImage.setVisibility(View.GONE);
}
PostUtils.loadPostImage(convertView, post.getPostId());
ImageButton deleteButton = convertView.findViewById(R.id.button_delete_post);
ImageButton editButton = convertView.findViewById(R.id.button_edit_post);
toggleButtons(post, editButton, deleteButton);
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
alertBuilder.setTitle(context.getString(R.string.delete_post));
alertBuilder.setMessage(context.getString(R.string.delete_post_message));
alertBuilder.setPositiveButton(context.getString(R.string.delete), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
posts.remove(position);
PostUtils.deletePost(context, post.getPostId(), post.getPosterId());
notifyDataSetChanged();
PostUtils.listenForNews(context);
}
});
alertBuilder.setNegativeButton(context.getString(R.string.cancel), null);
AlertDialog alert = alertBuilder.create();
alert.show();
}
});
editButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent editIntent = new Intent(context, EditPostActivity.class);
editIntent.putExtra(EXTRA_POSTS, posts);
editIntent.putExtra(EXTRA_POSITION, position);
context.startActivity(editIntent);
}
});
return convertView;
}
// Custom methods
private void toggleButtons(Post post, ImageButton editButton, ImageButton deleteButton) {
long twoMinutes = System.currentTimeMillis() - (2 * 60 * 1000);
long fiveMinutes = System.currentTimeMillis() - (5 * 60 * 1000);
if (post.getTimeStamp().getTime() < fiveMinutes) {
editButton.setVisibility(View.GONE);
}
else {
editButton.setVisibility(View.VISIBLE);
}
if (post.getTimeStamp().getTime() < twoMinutes) {
deleteButton.setVisibility(View.GONE);
}
else {
deleteButton.setVisibility(View.VISIBLE);
}
}
Here are the lifecycle methods from the Newsfeed fragment that load my data and set the adapter:
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_newsfeed, container, false);
PostUtils.listenForNews(getActivity());
mPosts = PostUtils.loadNewsfeed(getActivity());
mNewsfeed = view.findViewById(R.id.list_newsfeed);
mImagePicker = view.findViewById(R.id.picker_image);
mPhotoView = view.findViewById(R.id.post_image);
AccountUtils.listenForUpdates(getActivity());
setClickListener(view);
setFocusListener(view);
return view;
}
#Override
public void onResume() {
super.onResume();
mPosts = PostUtils.loadNewsfeed(getActivity());
Account account = AccountUtils.loadAccount(getActivity());
mNewsfeedAdapter = new NewsfeedAdapter(getActivity(), mPosts, account);
mNewsfeed.setAdapter(mNewsfeedAdapter);
}
Here are the lifecycle methods from my Profile fragment where the same functionality should work for a slightly different set of data:
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile, container, false);
try {
if(getActivity().getIntent() != null && getActivity().getIntent().getAction().equals(FamilyProfileFragment.ACTION_EDIT_PROFILE)) {
mEditingSelf = false;
mIsParent = true;
mProfile = (Profile) getActivity().getIntent().getSerializableExtra(FamilyProfileFragment.EXTRA_PROFILE);
String selectedName = mProfile.getFirstName();
String loadedName = AccountUtils.loadProfile(getActivity()).getFirstName();
if(selectedName.equals(loadedName)) {
mEditingSelf = true;
}
}
else {
mEditingSelf = true;
mProfile = AccountUtils.loadProfile(getActivity());
if(mProfile instanceof Parent) {
mIsParent = true;
}
else {
mIsParent = false;
}
}
}
catch (Exception e) {
e.printStackTrace();
mEditingSelf = true;
mProfile = AccountUtils.loadProfile(getActivity());
if(mProfile instanceof Parent) {
mIsParent = true;
}
else {
mIsParent = false;
}
}
mAccount = AccountUtils.loadAccount(getActivity());
AccountUtils.loadProfilePhoto(getActivity(), view, mProfile.getProfileId());
mPhotoView = view.findViewById(R.id.profile_photo);
PostUtils.listenForTimeline(getActivity(), mProfile);
mPosts = PostUtils.loadTimeline(getActivity());
mTimeline = view.findViewById(R.id.list_posts);
setClickListener(view);
setTextDisplay(view);
populateProfile(view);
return view;
}
#Override
public void onResume() {
super.onResume();
mPosts = PostUtils.loadTimeline(getActivity());
mNewsfeedAdapter = new NewsfeedAdapter(getActivity(), mPosts, mAccount);
mTimeline.setAdapter(mNewsfeedAdapter);
}
My constraints were set up incorrectly in the Profile fragment. Updated the XML to the following:
<ListView
android:id="#+id/list_newsfeed"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/constraintLayout4" />

spinner not displaying when clicked

hi currently im encountering a problem where the spinner is not showing up when clicked. when i run theres not any error so i dont know whats wrong . sorry im still new to android and java pls help....
public class SpinnerInfo extends MainProduct {
Spinner spinners;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinners = (Spinner) findViewById(R.id.spinner);
final ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.type_arrays, R.layout.support_simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinners.setAdapter(adapter);
spinners.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String [] dataArray = getResources().getStringArray(R.array.type_arrays);
String type = dataArray[position];}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}}
my xml
<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="match_parent"
android:background="#android:color/darker_gray"
android:orientation="vertical"
android:elevation="1dp"
android:weightSum="1">
<TextView
android:text="Product List"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:textSize="24sp"
android:textStyle="normal|italic"
android:textAlignment="center"
android:fontFamily="casual"
android:textColor="#android:color/background_dark"
android:layout_marginTop="10dp"/>
<TextView
android:text="Select Type:"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/textView2"
android:textAlignment="textStart"
android:textStyle="normal|bold" />
<Spinner
android:layout_width="126dp"
android:layout_height="wrap_content"
android:id="#+id/spinner"
/>
<ListView
android:id="#+id/listview_product"
android:layout_width="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:divider="#android:color/darker_gray"
android:dividerHeight="8dp"
android:background="#android:color/white"
android:layout_height="match_parent">
</ListView>
final ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.type_arrays, R.layout.support_simple_spinner_dropdown_item);
to
final ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.layout.support_simple_spinner_dropdown_item, R.array.type_arrays);
By the way you are accessing to the string resources when you can do this parent.getItemAtPosition(position)

using setOnItemClickListener and new intent not working

I have a grid view with images in it. Once one is clicked it should go to PlayVideoActivity but onItemClick never get called.
I followed in the debugger and tried clicking the image but it never fired onItemClick.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videos);
gridView = (GridView) findViewById(R.id.gridView);
customGridAdapter = new GridViewAdapter(this, R.layout.row_grid,
getData());
gridView.setAdapter(customGridAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
Toast.makeText(videoChannelActivity.this, i + "#Selected",
Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(videoChannelActivity.this, PlayVideoActivity.class);
startActivity(myIntent);
}
});
}
row_grid.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="5dp"
android:background="#drawable/grid_color_selector"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="#+id/image"
android:layout_width="280dp"
android:layout_height="158dp"
android:contentDescription="#string/desc"
android:scaleType="centerInside" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:textSize="12sp" >
</TextView>
Add android:descendantFocusability="blocksDescendants" attribute in parent layout of row_grid.xml
You should remove
android:clickable="true"
android:focusable="true"

Categories

Resources