Listview adapter not displaying list columns after closing of Dialog - java

Setup
In my app I have a section that creates rows of data to be saved in a database. Each row is built from a textbox, number box then "set" into a listview for display. Later on the user can open a "plan" created before and review the data.
I created a listview, a custom layout for the display line and figured out how to add/display each new row. However, when I perform the open, I create a dialog to display the list for plans, user selects on and the rows should display. They do not. I trace through the code and I see the arraylist for the adapter being populated and I call the notifydatasetchanged, but the main listview remains empty.
I am not using a customer adapter class, but just the Simple adapter.
This is the beginning of the Activity. I am still learning android coding so I tend to use generic names as I learn.
public class TrackItEqMainActivity extends AppCompatActivity {
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
public int rowCount = 1;
private static final String eTAG = "Exception";
final Context context = this;
// global variables needed for processing the add gaits display
**private ListView lstGaits;
private ListAdapter adapter;
private ArrayList<HashMap<String, String>> mylist;**
// *****
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(eTAG, "onCreate Build");
setTitle(R.string.activityManagePlans);
setContentView(R.layout.activity_track_it_eq_build);
Toolbar mySecondaryToolbar = (Toolbar) findViewById(R.id.my_secondaryToolbar);
setSupportActionBar(mySecondaryToolbar);
setActivityBuildListeners();
// set the objects needed to handle the dynamic gaits list
ListView lstGaits = (ListView) findViewById(R.id.lstMyGaits);
mylist = new ArrayList<HashMap<String, String>>();
try {
adapter = new SimpleAdapter(this, mylist, R.layout.gaits_detail,
new String[] { "keyGait", "keyTime" }, new int[] {
R.id.txtdisplayGait, R.id.txtdisplayTime });
lstGaits.setAdapter(adapter);
} catch (Exception e) {
Log.i(eTAG, e.getMessage());
}
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
SO when a user taps a new button a set of controls appear allowing to select a gait and enter a time. They then tap on the set button and in the set onClick, I add the data to the internal array and display the custom layout on the screen.
private Button.OnClickListener onClick_btnSet = new OnClickListener() {
#Override
public void onClick(View v) {
Spinner spinner = (Spinner) findViewById(R.id.spinner_gaits);
TextView textGait = (TextView) spinner.getSelectedView();
String gtResult = textGait.getText().toString();
TextView txtTime = (TextView) findViewById(R.id.txtSelected);
String tmeResult = txtTime.getText().toString();
buildDisplayLine(true,gtResult,tmeResult);
txtTime.setText("");
spinner.requestFocus();
}
};
The function that builds the line and adds to the array is this:
private void buildDisplayLine(boolean isNew, String gait, String time) {
HashMap<String, String> map2 = new HashMap<String, String>();
map2.put("keyGait",gait); // put the col data in
map2.put("keyTime", time); // put the col data in
mylist.add(map2);
if (isNew) {
((BaseAdapter) (adapter)).notifyDataSetChanged();
}
}
All that works although a strange side effect is that the list will not display if the number keypad is visible. When I clear it off the list appears.
Ifa use wants to look at or change a plan they tap the open button. IT display a dialog box that shows a list of plans. Tap on a plan and all the elements should display:
private final ThreadLocal<OnClickListener> onClick_btnOpen = new ThreadLocal<OnClickListener>() {
#Override
protected OnClickListener initialValue() {
return new OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.activity_track_it_eq_open_plan);
dialog.setTitle("Open Exercise Plan...");
Button diaCancelButton = (Button) dialog.findViewById(R.id.btnCancelOpen);
diaCancelButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
// get a list of files from the local app plans
final eqDatabaseService eqDB = new eqDatabaseService(context, 2);
ArrayList<HashMap<String,String>> allPlans = eqDB.getPlanList();
if (allPlans.isEmpty()) {
Toast toast = new Toast(context);
toast.setGravity(Gravity.TOP, 0, 0);
Toast.makeText(context, "No Files to open, Please create one!", Toast.LENGTH_LONG).show();
return;
}
ListView lvPlan = (ListView) dialog.findViewById(R.id.lvPlans);
try {
adapter = new SimpleAdapter(context, allPlans, R.layout.plans_columns,
new String[] { "keyName", "keyENum" }, new int[] {
R.id.txtPlanName, R.id.txtNumElements });
lvPlan.setAdapter(adapter);
} catch (Exception e) {
}
//********************************************************
lvPlan.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
TextView txtplan = (TextView) v.findViewById(R.id.txtPlanName);
String planName = txtplan.getText().toString();
List<eqSessions_dt> myPlan = eqDB.getCurrentPlan(planName);
clearGrid();
for (eqSessions_dt row : myPlan) {
buildDisplayLine(false, row.get_gait(),Integer.toString(row.get_time()));
}
((BaseAdapter)(adapter)).notifyDataSetChanged();
dialog.dismiss();
}
});
dialog.show();
}
};
}
};
I have played around with this, did look at other similar questions and answers, but didn't see anything that would explain why the notify would not work. I avoided doing a custom adapter, trying to keep this simple at first. What am I missing that stops the list from displaying. I see the arraylist being filled and all related objects are global to the activity. I'll keep playing around, but I hope otehr eyes see what I miss.
This is the activity 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
tools:context="com.newproject.jhull3341.trackiteq.TrackItEqMainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/my_secondaryToolbar"
android:layout_width="fill_parent"
android:layout_height="?attr/actionBarSize"
android:background="#236dc1"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/lyFileAction">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="match_parent"
android:text="New"
android:id="#+id/btnNew"
android:layout_row="0"
android:layout_column="0"
android:layout_gravity="center_horizontal" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="match_parent"
android:text="Open"
android:id="#+id/btnOpen"
android:layout_row="0"
android:layout_column="1"
android:layout_gravity="center_horizontal" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="match_parent"
android:text="Save"
android:id="#+id/btnSave"
android:layout_row="0"
android:layout_column="2"
android:layout_gravity="center_horizontal" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="match_parent"
android:text="Delete"
android:id="#+id/btnDelete"
android:layout_row="0"
android:layout_column="3"
android:layout_gravity="center_horizontal" />
</GridLayout>
<GridLayout
android:layout_width="367dp"
android:layout_height="wrap_content"
android:id="#+id/grdEntry">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/label_time"
android:id="#+id/lblTime"
android:layout_row="0"
android:layout_column="1"
android:layout_marginBottom="5dp" />
<Spinner
android:layout_width="147dp"
android:layout_height="wrap_content"
android:id="#+id/spinner_gaits"
android:layout_row="1"
android:layout_column="0"
android:focusable="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/label_Gait"
android:id="#+id/lblGait"
android:layout_row="0"
android:layout_column="0"
android:layout_marginBottom="5dp" />
<EditText
android:layout_width="124dp"
android:layout_height="wrap_content"
android:id="#+id/txtSelected"
android:layout_row="1"
android:layout_column="1"
android:layout_gravity="fill_horizontal|center_vertical"
android:singleLine="true"
android:numeric="integer" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="fill_parent"
android:text="Set"
android:id="#+id/btnSet"
android:layout_row="1"
android:layout_column="2"
android:layout_gravity="center_vertical"
android:clickable="true" />
</GridLayout>
<ListView
android:id="#+id/lstMyGaits"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_below="#+id/grdEntry" />
</LinearLayout>
this is the custom layout for each display row.`
<?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:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtdisplayGait"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="TextView"
android:textSize="18sp" />
<TextView
android:id="#+id/txtdisplayTime"
android:layout_width="80dp"
android:layout_height="44dp"
android:gravity="center_vertical"
android:text="TextView"
android:textSize="18sp" />
<ImageButton
android:id="#+id/btnRemoveLine"
android:layout_width="25dp"
android:layout_height="44dp"
android:layout_weight="0.39"
android:background="#color/wallet_bright_foreground_holo_dark"
android:elevation="1dp"
android:src="#android:drawable/btn_dialog" />
</LinearLayout>`

Related

recycler view hides message upward when keyboard is open / how to keep recycler View from scrolling when keyboard is on

I am making chat app but when I send message recycler view does not show first 2 messages because it is up I want something like whatsapp if I open keyboard recycler view is shown from start
I tried following but it sticks chats to end even after I close keyboard:
LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager);
. following is my code:
public class UserChat extends AppCompatActivity {
private RecyclerView recyclerView;
private Button btnSend;
private EditText messageBox;
private TextView name_txt;
final ArrayList<ChatModel> chatModels = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_chat);
recyclerView = findViewById(R.id.chat_list);
messageBox = findViewById(R.id.et_chat_box);
btnSend = findViewById(R.id.btn_chat_send);
name_txt = findViewById(R.id.name_txt);
String username = "username not set";
Bundle extras = getIntent().getExtras();
if(extras!=null){
username = extras.getString("username");
}
name_txt.setText(username);
btnSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int i = 1;
i++;
ChatModel chatModel = new ChatModel();
chatModel.setId(i);
chatModel.setMe(true);
chatModel.setMessage(messageBox.getText().toString().trim());
chatModels.add(chatModel);
ChatAdapter chatAdapter = new ChatAdapter(chatModels, getApplicationContext());
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyclerView.setAdapter(chatAdapter);
messageBox.setText("");
}
});
}
}
following is my XML :
<?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"
tools:context=".UserChat">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/chat_list"
android:layout_width="406dp"
android:layout_height="611dp"
android:paddingStart="15dp"
android:paddingTop="15dp"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toTopOf="#+id/view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar"
app:layout_constraintVertical_bias="0.974" />
<View
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#e0e0e0"
app:layout_constraintBottom_toTopOf="#+id/layout_gchat_chatbox" />
<RelativeLayout
android:id="#+id/layout_gchat_chatbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent">
<EditText
android:id="#+id/et_chat_box"
android:layout_width="333dp"
android:layout_height="48dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="0dp"
android:layout_toStartOf="#+id/btn_chat_send"
android:background="#android:color/transparent"
android:hint="Enter Message"
android:inputType="text"
android:maxLines="6"
tools:ignore="Autofill" />
<Button
android:id="#+id/btn_chat_send"
android:layout_width="78dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginEnd="0dp"
android:background="?attr/selectableItemBackground"
android:text="Send"
android:textColor="#color/teal_700" />
</RelativeLayout>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="408dp"
android:layout_height="64dp"
android:background="#2E2A2A"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.333"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/name_txt"
android:layout_width="wrap_content"
android:layout_height="39dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textColor="#FAF8F8"
android:textSize="21sp"
app:layout_constraintEnd_toEndOf="#+id/toolbar"
app:layout_constraintHorizontal_bias="0.368"
app:layout_constraintStart_toStartOf="#+id/toolbar"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="67dp"
android:layout_height="53dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toStartOf="#+id/name_txt"
app:layout_constraintHorizontal_bias="0.673"
app:layout_constraintStart_toStartOf="#+id/toolbar"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/female" />
</androidx.constraintlayout.widget.ConstraintLayout>
int i = 1;
i++;
always u send i=2
There was a problem in height of the recycler view. I saw it here: https://github.com/stfalcon-studio/ChatKit/issues/103. #andriizhumela said that you need to put match parent in width and height of recycler view . so now it is solved

How to set text to value of listview items after time intervals

I am developing an android app where a user will input text to an EditText, then it will be added to a ListView.
But once a button with onClick value "start" is clicked, it will display each item of the ListView in a TextView with id displayText, in the order of each item and it will loop(Repeat) once it is exhausted.
Here is my Code
Math.java
public class Math extends AppCompatActivity {
private ArrayList<String> items;
private ArrayAdapter<String> itemsAdapter;
private ListView lvItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_math);
lvItems = (ListView) findViewById(R.id.lvItems);
items = new ArrayList<String>();
itemsAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
lvItems.setAdapter(itemsAdapter);
}
public void AddItem(View v) {
EditText etNewItem = (EditText) findViewById(R.id.etNewItem);
String itemText = etNewItem.getText().toString();
itemsAdapter.add(itemText);
etNewItem.setText("");
}
public void start(View v){
RelativeLayout lc = (RelativeLayout)findViewById(R.id.contentContainer);
lc.setVisibility(View.GONE);
RelativeLayout tc = (RelativeLayout)findViewById(R.id.tvContainer);
tc.setVisibility(View.VISIBLE);
}
}
activity_math.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Math">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tvContainer"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="30sp"
android:id="#+id/displayText"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/contentContainer">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lvItems"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/btnAddItem" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/etNewItem"
android:layout_alignTop="#+id/btnAddItem"
android:hint="Enter a new item"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/btnAddItem"
android:layout_toStartOf="#+id/btnAddItem"
android:layout_alignParentBottom="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Item"
android:id="#+id/btnAddItem"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="AddItem"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#id/btnAddItem"
android:onClick="start"/>
</RelativeLayout>
Okay, i think the issue is that you haven't given the TextView any thing to display. you have to convert the arrayList to a String array and use setText to display the Value.
add this part to the "start" method
String ls =System.getProperty("line.separator");// i used this to seperate the array items
String joint = TextUtils.join(ls, lvitems);
result.setText(joint);
That should work

Android Studio Buttons not Displaying

I made this dialog for my app that shows up allows you to delete the items from db. It used to be a textView and a button which called the function, all in a linear layout, which worked good. Now I changed to a Relative Layout so I could add another button on the same line as the other, I also added an editText.
My problem is that since I added those objects I am not able to see the buttons anymore. the editText and textView are visible.
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/dialog_deletefood"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.paulcosma.app2.SecondActivity">
<TextView
android:text="TextView"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lblFoodDetails"
android:textAlignment="center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp" />
<Button
android:text="Şterge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnDeleteEatenFood"
android:layout_alignBaseline="#+id/btnModifyEatenFood"
android:layout_alignBottom="#+id/btnModifyEatenFood"
android:layout_toStartOf="#+id/lblFoodDetails"
android:visibility="visible" />
<Button
android:layout_marginTop="100dp"
android:text="Modifică"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnModifyEatenFood"
android:layout_below="#+id/lblFoodDetails"
android:layout_toEndOf="#+id/lblFoodDetails"
android:visibility="visible" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="#+id/txtEditValue"
android:maxLength="6"
style="#style/Widget.AppCompat.AutoCompleteTextView"
android:maxLines="1"
android:textAppearance="#style/TextAppearance.AppCompat"
android:fontFamily="sans-serif"
android:textSize="14sp"
android:textAlignment="center"
android:textColorLink="#android:color/holo_blue_light"
android:inputType="numberDecimal"
android:layout_marginTop="21dp"
android:layout_below="#+id/lblFoodDetails"
android:layout_centerHorizontal="true" />
</RelativeLayout>
This dialog is supposed to show on listView item click. Below is the code I used to show it.
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HashMap<String, String> hmap = (HashMap<String, String>)parent.getItemAtPosition(position);
final String _name = hmap.get("food");
final int _id = foodDB.getFoodId(_name);
AlertDialog.Builder mBuiler = new AlertDialog.Builder(SecondActivity.this);
View mView = getLayoutInflater().inflate(R.layout.dialog_deletefood,null);
final TextView lblFoodDetails = (TextView) mView.findViewById(R.id.lblFoodDetails);
final Button btnDeleteEatenFood = (Button) mView.findViewById(R.id.btnDeleteEatenFood);
lblFoodDetails.setText("Aţi ales produsul " + _name + ". Aici puteţi adăuga sau scădea cantitatea printr-o anumită valoare sau puteţi şterge apariţia produsului.");
mBuiler.setView(mView);
final AlertDialog dialog = mBuiler.create();
btnDeleteEatenFood.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
foodDB.deleteEatenFood(_id);
Toast.makeText(SecondActivity.this,_name+" a fost sters",Toast.LENGTH_SHORT).show();
dialog.dismiss();
updateFoodList(foodDB.getAllEatenFood());
}
});
dialog.show();
}
This layout file renders the buttons in the Android layout designer as described (see below)
The problem may reside in code.
The problem was that both buttons where placed based on textView's size. When it changed, buttons got placed out of the screen
Bug:
android:layout_toEndOf="#+id/lblFoodDetails"
android:layout_toEStartOf="#+id/lblFoodDetails"

null pointer exception on radio group

I am trying to make a popup window in an activity. The popup window shows up when we click a button in the activity. The layout for popup window is to show a radio group containing 4 radio buttons, a button and a seek bar and has to return a value to the main activity based on the selected radio button when the button in the pop up window is pressed.
When i run the app and open the pop up window it is giving we this error:
"java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioGroup.setOnCheckedChangeListener(android.widget.RadioGroup$OnCheckedChangeListener)' on a null object reference "
The code for my main activity is:
public class CustomMenuActivity extends Activity
{
String ingredientName;
String ingredientQuantity;
private PopupWindow pw;
Button setQuantity;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_menu);
setQuantity = (Button) findViewById(R.id.btnSetQty);
setQuantity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
initiatePopupWindow();
}
});
}
private void initiatePopupWindow()
{
try {
//We need to get the instance of the LayoutInflater, use the context of this activity
LayoutInflater inflater = (LayoutInflater) CustomMenuActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Inflate the view from a predefined XML layout
View layout = inflater.inflate(R.layout.set_quantity_popup,
(ViewGroup) findViewById(R.id.popupElementid));
// create a 600px width and 570px height PopupWindow
pw = new PopupWindow(layout, 600, 570, true);
// display the popup in the center
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
RadioGroup radioGroup;
radioGroup = (RadioGroup) findViewById(R.id.radioGroupid);
final String[] tempQtyVar = new String[1];
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.rbqty30id:
tempQtyVar[0] = "30";
break;
case R.id.rbqty60id:
tempQtyVar[0] = "60";
break;
case R.id.rbqty90id:
tempQtyVar[0] = "90";
break;
case R.id.rbqtycutomid:
tempQtyVar[0] = "120";
break;
}
}
});
Button setQtyBtn = (Button) layout.findViewById(R.id.buttonOkSetQtyid);
setQtyBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ingredientQuantity = tempQtyVar[0];
Toast.makeText(getApplicationContext(),ingredientQuantity,Toast.LENGTH_LONG).show();
pw.dismiss();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
The layout.xml for my pop up window is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="#fd050505"
android:padding="30dp"
android:id="#+id/popupElementid">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="#+id/relativeLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SET QUANTITY"
android:textColor="#84e9e6"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textStyle="bold"
android:id="#+id/popupTitleid"/>
<LinearLayout
android:layout_width="600dp"
android:layout_height="wrap_content"
android:layout_below="#+id/popupTitleid"
android:orientation="horizontal"
android:background="#drawable/btn_rounded_boarder"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp">
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/radioGroupid"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:orientation="horizontal"
>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=" 30ml "
android:textColor="#84e9e6"
android:id="#+id/rbqty30id"
android:checked="true" />
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#84e9e6"
android:text=" 60ml "
android:id="#+id/rbqty60id" />
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#84e9e6"
android:text=" 90ml "
android:id="#+id/rbqty90id" />
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#84e9e6"
android:text=" Custom Value "
android:id="#+id/rbqtycutomid"
/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="600dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true">
<SeekBar
android:id="#+id/customqtySBid"
android:layout_height="fill_parent"
android:layout_width="600dp"
android:textColor="#84e9e6"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="150dp"
android:paddingBottom="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonOkSetQtyid"
android:textColor="#84e9e6"
android:text="OK"
android:background="#drawable/btn_rounded_boarder"
android:layout_marginTop="275dp"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
I tried looking for answer online but didn't really find solution specific to my problem. Please suggest any solutions.
Thanks in advance.
You seem to be inflating the wrong layout. Change the correct layout.xml in
setContentView(R.layout.activity_custom_menu);

Limit a listview to 25 items

I am displaying a listview programmiclly using the following codes:
Below is how the listview is displayed programmatically:
messagesList = (ListView) findViewById(R.id.listMessages);
messageAdapter = new MessageAdapter(this);
I would want to limit the list to 25 items, and where it is not infinite.
Below is the layout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/white_wallpaper"
>
<ListView
android:id="#+id/listMessages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/divider"
android:divider="#null"
android:dividerHeight="0dp"
android:inputType="text|textNoSuggestions"
android:padding="0dip"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"
tools:listitem="#layout/message_left" />
<RelativeLayout
android:id="#+id/divider"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#color/off_white"
android:layout_above="#+id/relSendMessage" />
<RelativeLayout
android:id="#+id/relSendMessage"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:background="#ddd"
android:paddingLeft="10dp"
android:layout_alignParentBottom="true">
<EditText
android:id="#+id/messageBodyField"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="#+id/sendButton"
android:layout_alignTop="#+id/sendButton"
android:layout_marginBottom="-4dp"
android:layout_marginRight="10dp"
android:inputType="text|textNoSuggestions"
android:layout_toLeftOf="#+id/sendButton"
android:background="#android:color/white"
android:hint="#string/message_elipses"
android:textColor="#000000"
android:textColorLink="#adefda"
android:textSize="14sp" />
<Button
android:id="#+id/sendButton"
android:layout_width="72dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_margin="4dp"
android:background="#drawable/button_send" />
</RelativeLayout>
<Button
android:id="#+id/iSchedule"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:alpha="0.7"
android:background="#C11B17"
android:text="Schedule"
android:textColor="#f2f2f2"
android:textSize="20sp"
android:textStyle="bold"
android:typeface="serif" />
</RelativeLayout>
I have been suggested the following, but I am not sure how to integrate it within the above code:
#Override
public int getCount() {
return 25;
}
Thanks in advance, and for any clarification, let me know.
Update
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.messaging);
feedBack = new FeedbackDialog(this, "ID");
final TextView iSchedule = (TextView) this.findViewById(R.id.iSchedule);
iSchedule.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
MessagingActivity1.this.startActivity(new Intent(MessagingActivity1.this, ScheduleMatchOptionActivity.class));
}
});
Parse.initialize(this, "ID", "ID");
bindService(new Intent(this, MessageService.class), serviceConnection,
BIND_AUTO_CREATE);
Intent intent = getIntent();
recipientId = intent.getStringExtra("RECIPIENT_ID");
currentUserId = ParseUser.getCurrentUser().getObjectId();
messageAdapter = new MessageAdapter(this);
#Override
public int getCount() {
messagesList.setAdapter(messageAdapter);
return 25;
}
messagesList = (ListView) findViewById(R.id.listMessages);
populateMessageHistory();
messageBodyField = (EditText) findViewById(R.id.messageBodyField);
findViewById(R.id.sendButton).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View view) {
sendMessage();
}
});
}
However, I receive the following error
in the live #override it says Syntax error on token(s), misplaced construct(s)
in the public int get count "Syntax error on token(s), misplaced construct(s)"
In the return 25 - Void methods cannot return a value
You want to use that code (or similar) to override the default getCount method in ListAdapter: http://developer.android.com/reference/android/widget/ListAdapter.html
You can check an example in: http://www.vogella.com/tutorials/AndroidListView/article.html#listview_listviewexample
Hey you want to get list with limit, you have two option
1. You set array list size,
2. You can set, when you get data put for loop and set limit

Categories

Resources