Adapter.class
holder.txt_AddToCart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String product_image = brandWiseProductArrayList.get(position).getProductImgPath();
int qty = Integer.parseInt(holder.edtxt_integer_number.getText().toString());
}
});
Here, When I am click this button must be asked to select spinner
Is there any way to validate spinner or put "select something" in the first position of a spinner which is filled by objArraylist
ProductSpinnerAdapter productSpinnerAdapter = new ProductSpinnerAdapter(context, brandWiseProductArrayList.get(position).getProductDetailsArrayList());
holder.spinner_product_details.setAdapter(productSpinnerAdapter);
Try this
String product_image = brandWiseProductArrayList.get(holder.spinner.getSelectedItemPosition()).getProductImgPath();
change holder.spinner to respective spinner variable.
Let me know if this works
Related
I've got a spinner with a list of states in an array that when a user selects it currently outputs which state they selected to the log, this is working as expected. The issue I'm facing now is when a user clicks a button once they have selected their state is pulling the data that pertains to that state from parse. My code is below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
// Add your initialization code here
com.parse.Parse.initialize(new com.parse.Parse.Configuration.Builder(getApplicationContext())
.applicationId("appidhere")
.clientKey("clientkeyhere")
.server("http://server.compute-1.amazonaws.com:80/parse/")
.build());
// find spinner object here
final Spinner locSpinner = (Spinner) findViewById(R.id.locSpinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.state_list, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
locSpinner.setAdapter(adapter);
locSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(final AdapterView<?> parent, View view,
final int position, long id) {
Log.v("item", (String) parent.getItemAtPosition(position));
final String item = locSpinner.getSelectedItem().toString();
Button search = (Button) findViewById(searchBtn);
final TextView txtState = (TextView) findViewById(R.id.txtState);
final TextView txtName = (TextView) findViewById(R.id.txtName);
search.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ParseQuery<ParseObject> query = ParseQuery.getQuery(item);
query.whereStartsWith("Name", "Paws");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> scoreList, ParseException e) {
if (e == null) {
Log.v("item", "Retrieved " + item.toString() + " state");
txtState.setText(item);
txtName.setText(item);
} else {
Log.d("item", "Error: " + e.getMessage());
}
}
});
}
});
}
I have initialized parse in onCreate but it am having trouble getting it to populate my TextView's with the fields. For right now all I'm trying to accomplish is getting the values returned and outputting it to log, but ultimate goal is to be able to have the values show up in the TextViews. Any help would be greatly appreciated!
If you haven't already, you can have parse log what it's doing like this:
Parse.initialize(new Parse.Configuration.Builder(this)
.applicationId(appId)
.clientKey("")
.server(serverUrl)
.addNetworkInterceptor(new ParseLogInterceptor())
.build()
You will also need this in your gradle file:
compile 'com.parse:parseinterceptors:0.0.2'
Most likely you're getting an HTML response, like a 404 or 500 or something like that, rather than the JSON that the parse SDK is expecting. 403 Not Authorized (off the top of my head, I might have the wrong code) is also common if the URL or application id is wrong. By logging the details you can see the URL it's using to initialize parse, and exactly what the response is in logcat.
Edit: in your state class define a method like this:
public String getName() {
return getString("name");
}
Then do this:
textView.setText(state.getName());
I have a
EditText- addArea = (EditText) findViewById(R.id.editTextArea);
and ArrayList - public static ArrayList<String> areaList = new ArrayList<String>();
and a Button- addbtn = (Button) findViewById(R.id.addBTN);
This is my code to Store Strings into array. It displaying the toastMessage but I doesn't know whether its storing Strings or not.
addbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String area = addArea.getText().toString();
if (!area.isEmpty()){
areaList.add(area);
toastmessage(area+" added successfully.");
addArea.setText("");
}
}
});
What is need is, The spinner is in the next layout. When I click the spinner it should diplay the items that i have enterd in the edittext.
To check whether the list is added, you can check by this way
for (int i=0; i<areaList.size();i++){
addArea.append(areaList.get(i));
addArea.append("\n");
}
You can know whether the string is stored into the areaList by checking the textView setText value.
To pass the areaList to next Activity, add this
Intent intent = new Intent(MainActivity.this, secondActivity.class);
intent.putExtra("areaList",areaList);
In the second Activity, use this line to get all the areaList data
ArrayList<String> areaList = (ArrayList<String>)getIntent().getSerializableExtra("areaList");
Then set the areaList to spinner
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, areaList);
spinner.setAdapter(adapter);
In order to see make sure the item is added you can print the array or check its size.
First declare a global variable as int lastSize;
lastSize = 0; //we start from 0
addbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String area = addArea.getText().toString();
if (!area.isEmpty()){
areaList.add(area);
//size has changed, means new item added successfully
if (areaList.size() > lastSize) {
toastmessage(area+" added successfully.");
addArea.setText("");
}
//lets update lastSize to record latest size
lastSize = areaList.size();
}
}
});
I have a button in a TabActivitythat when clicked, opens anAlertDialogwith 2EditTextViews` for a name and number.
When I click the Ok Button to close the Dialog, I want to pass the name back into a ListView on the TabActivity. I can get the name to pass back to the EditText box mAlertDialog on the TabActivity. But something other than the name is being displayed in the ListView.
It looks like a reference to the object "widget" (Widget is an object of the class Device, which has getName, setName methods), com.mypackage.Device#419226e0.
I'll try to post the relevant code below (yes, I know I'm not using Fragments. I found it difficult to implement horizontal scrollable tabs with a ListView with Fragments):
#SuppressWarnings("deprecation")
public class MainActivity extends TabActivity implements OnClickListener {
public static ArrayList<Device> deviceList = new ArrayList<Device>();
public static ArrayAdapter<Device> deviceAdapter=null;
private static ListView deviceListView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost= getTabHost();
TabHost.TabSpec spec;
Intent intent;
rButton = (Button)findViewById(R.id.button_register);
mAlertDialog = (EditText)findViewById(R.id.edittextresult_Name);
rButton.setOnClickListener(onRegister);
intent = new Intent (this, devices.class);
spec = mTabHost.newTabSpec("devices")
.setIndicator("Devices")
.setContent(R.id.devices);
mTabHost.addTab(spec);
deviceListView=(ListView)findViewById(R.id.rdevices);
//Attach array adapter to data array "deviceList"
deviceAdapter = new ArrayAdapter<Device>(this,android.R.layout.simple_list_item_1, deviceList);
//connect adapter "deviceAdapter" to listview widget so the activity listview is populated with data from the array
deviceListView.setAdapter(deviceAdapter);
}
private View.OnClickListener onRegister = new View.OnClickListener() {
public void onClick(View v) {
String title = "Register";
String buttonOk = "OK";
String buttonCancel = "Cancel";
String madd = "address";
String name = "widget name";
//get rdevice.xml view
LayoutInflater li = LayoutInflater.from(context);
View rView = li.inflate(R.layout.rdevice, null);
AlertDialog.Builder adRegister = new AlertDialog.Builder(context);
//set rdevice.xml to adRegister builder
adRegister.setView(rView);
//set title
adRegister.setTitle(title);
//Set EditText views to get user input
final EditText mField = (EditText)rView.findViewById(R.id.editText_Address);
final EditText nField = (EditText)rView.findViewById(R.id.editText_WidgetName);
//set dialog message
adRegister.setMessage("Message")
.setCancelable(false)
.setPositiveButton(buttonOk, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int BUTTON_POSITIVE) {
// TODO Auto-generated method stub
Device widget = new Device();
String madd = mField.getText().toString();
String name = nField.getText().toString();
widget.setName(name);
widget.setAddress(madd);
Log.d(TAG, "Address: " + madd);
Log.d(TAG, "Widget name: " + name);
//get user input and set it to result on main activity
mAlertDialog.setText(nField.getText());
deviceAdapter.add(widget);
deviceAdapter.notifyDataSetChanged();
}
})
.setNegativeButton(buttonCancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int BUTTON_NEGATIVE) {
}
});
//Create alert dialog
AlertDialog alertDialog = adRegister.create();
//show it
adRegister.show();
}
};
On debug, there are no errors in logcat.
However, when I watch the expression mAlertDialog it says
Unable to retrieve the correct enclosing instance of this
Even though the correct name is displayed in the app. When I let the program finish after debug, this reference is displayed in the ListView "com.mypackage.Device#419226e0" instead of the name I typed in the AlertDialog box.
Does this have something to do with scope or anonymous inner classes? Please help. I'm not that familiar with Java so I getting lost in the nuts and bolts here.
Ok I got it to work. This was very helpful: http://www.ezzylearning.com/tutorial.aspx?tid=6816874 , specifically this quote
You may be wondering how the ListView will display the Product object and which property of the Product object will be displayed. The answer is very simple, by default Android ListView control renders a simple TextView inside every ListView item and TextView control can only display simple text. Notice how the toString() function is overridden in the Product class we defined above. Whatever String you will return from the object toString() function will be displayed in the TextView rendered in the ListView items.
Basically, in my Device.java class, I had to override the toString method to specify which object member to pass, so I included this code
#Override
public String toString(){
return this.name;
}
I have a spinner, that uses an array from strings.xml
if the array has 5 strings (1,2,3,4,5), and i want the spinner to show second
string (2) as default value, is this possible?
I know i can re-arrange the strings order so that first one is 2,
but this doesn't look very good if spinner dialog appears as (2,1,3,4,5).
Or does the array have to be created within my activity programatically
and then use setPostion()?
I have tried this, but get a fault when creating array in activity.
Can anyone please give me an example of how to create array and use
it in spinner()
I have also searched on here for answers but cant seem to find what i need.
Thanks for looking....
I recommend you check: http://developer.android.com/resources/tutorials/views/hello-spinner.html
You should create an ArrayAdapter in your Activity.
From the above link:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
You can use
spinner.setSelection(adapter.getPosition(value)));
to set the position.
Cheers for reply.....
Didn't do what i needed, but have managed to solve my problem as follows..
First i created an array within my activity, instead of strings.xml.
String[] NoCore_Array = new String [5];
{
NoCore_Array[0] = "1";
NoCore_Array[1] = "2";
NoCore_Array[2] = "3";
NoCore_Array[3] = "4";
NoCore_Array[4] = "5";
}
Then i created the spinner using...
Spinner spnNoCore = (Spinner) findViewById(R.id.spinner_nocore);
Then created the adapter, using above array....
ArrayAdapter NoCoreAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, NoCore_Array);
spnNoCore.setAdapter(NoCoreAdapter);
Then set default position of the adapter as follows...
//Set Default Selection
spnNoCore.setSelection(1);
Then rest of spinner code for actions...
spnNoCore.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
//Get item from spinner and store in string conductorSize........
NoCore = parent.getItemAtPosition(pos).toString();
if (NoCore.equals(NoCore1)) { CoreNo = 1 ; }
if (NoCore.equals(NoCore2)) { CoreNo = 2 ; }
if (NoCore.equals(NoCore3)) { CoreNo = 3 ; }
if (NoCore.equals(NoCore4)) { CoreNo = 4 ; }
if (NoCore.equals(NoCore5)) { CoreNo = 5 ; }
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
});
Hope this may help other people who are having same problem with
setting default selection on Spinner.
However the layout of the dialog is not as good when done this way,
Radio buttons are missing, just looks like a text selection
with lines dividing the sections.
How can i set a default option in a spinner?
I filled 3 spinners with differents querys, and maybe i just want to use 2 of that spinners, not the 3. So always is a value set in the spinner. How can i avoid a value? Cause maybe if a fill an array i can set a default option in position 0, but im filling spinners with querys.
I know spinners are made to have a value, so maybe i could put them a default value so in the onitemclicklistener i can avoid using that spinner with an if (valuespinnerselected = "Default" ) dont to anything
You can configure a label and a value for each index of the Spinner.
Think that the value -1 is the default value that you want to ignore. That way, i think that this piece of code can help you:
Spinner spinner = (Spinner)findViewById(R.id.spinner);
SpinnerItem item1 = new SpinnerItem();
item1.setText("Default Query");
item1.setValue(-1);
SpinnerItem item2 = new SpinnerItem();
item2.setText("Query1");
item2.setValue(10);
SpinnerItem item3 = new SpinnerItem();
item3.setText("Query 2");
item3.setValue(20);
SpinnerItem[] data = new SpinnerItem[3];
data[0] = item1;
data[1] = item2;
data[2] = item3;
ArrayAdapter<SpinnerItem> adapter = new ArrayAdapter<SpinnerItem>(this, android.R.layout.simple_spinner_item, data);
spinner.setAdapter(adapter);
Where SpinnerItem is the class:
public class SpinnerItem {
String text;
Integer value;
public String getText() {
return text;
}
public void setText(String text){
this.text = text;
}
public Integer getValue() {
return value;
}
public void setValue(Integer value){
this.value = value;
}
public String toString() {
return text;
}
}
After that, you can get the selected item and see his value:
SpinnerItem item = (SpinnerItem) spinner.getSelectedItem();
if(item.getValue() == -1){
//do Something.
}
Hope this helped!
I think easiest would be to put a default item like you mentioned. It could be "Please select".