In my application I need to display multiple (three) listviews at a time sections wise. Can anyone suggest me a best way to implement this.
Thanks in advance,
Chandra.
Maybe you want to look into fragments.
http://developer.android.com/guide/components/fragments.html
if you create the ListView's in an XML file, you can just specify the ID attribute like so: android:id="#+id/listView1, providing a different ID for each ListView. In your Java code, you will want to extend Activity and create three ListView objects and point them towards the IDs in your XML file. Once you have a handle on the ListView's, you want to create a data source and ArrayAdapter<String> for each ListView. I prefer to use ArrayList<String> over the convential String[] simple because, to me, they are easier to use. A working Java sample below would work for a single ListView. Duplicate the variables and objects twice more with differing names for the two other ListViews. Hope this helps:
public class MainListActivityExample extends Activity {
ListView listView1;
ArrayList<String> lvContents1 = new ArrayList<String>;
ArrayAdapter<String> lvAdapter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Tell the method which layout file to look at
setContentView(R.layout.listViewExample_activity);
// Point the ListView object to the XML item
listView1 = (ListView) findViewById(R.id.listView1);
// Create the Adapter with the contents of the ArrayList<String>
lvAdapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, lvContents1);
// Attach the Adapter to the ListView
listView1.setAdapter(lvAdapter1);
// Add a couple of items to the contents
lvContents1.add("Foo");
lvContents1.add("Bar");
// Tell the adapter that the contents have changed
lvAdapter1.notifyDataSetChanged();
}
In order to add the other two ListViews, create two more ListView objects, two more ArrayList<String> objects, and two more ArrayAdapter<String> objects, each with corresponding names so you know which belongs to which. You can then follow the exact same steps to initialize them.
Related
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
convertView = ContactViewActivity.this.getLayoutInflater().inflate(R.layout.contact_view_field_row,
parent, false);
}
String value = (String)getItem(position);
TextView fieldValue = (TextView)convertView.findViewById(R.id.contact_view_row_value);
fieldValue.setText(value);
return convertView;
}
The above code is taken from a BaseAdapter class. So what is the point of setting the inflated view to convertView? The whole point of the getView is to make a view if there isn't one, give it attributes, then return it so it can be shown in the application in the ListView right? So why not just say findViewById(R.id.contact_view_row_value);? I mean what are we really doing with the layoutInflater? I'd think that since we set it equal to convertView, there would be no need to type convertView.findViewById. I mean it is already equal to an inflated XML file right? I'm quite confused here, I'd really appreciate some clarification.
Because convertView is something that can be reused, though at runtime you don't know whether you're being provided an existing View whose data must be updated, or if a new View needs to be inflated.
When your application starts, you need to inflate your Views in order for any data to be displayed in your ListView at all. Inflate means to parse the resource XML into a graphical object that and provide it with Android's Context, so it becomes something that has the potential to be rendered and interacted with on the graphical hierarchy. Later on in the application, Android tries to help you save resources and avoid unnecessary inflation by passing you a View that had been inflated earlier; that's why we check if convertView is not equal to null. If it's not, you can convert that existing View into something that displays data relevant for the current index.
However, it's worth noting that the ListView is now deprecated. Try using the RecyclerView instead, where this behaviour is a little more obvious. Here's an example.
I want to know if threre is a way to use a button.OnclickListener() to go from one activity to xml file without making a class for that xml.For example:
btn.OnClickListener(new OnclickListener(){
#Override
public void onClick(View v) {
Intent i = new Intent(ex.this, ex.class);
startActivity(i);
}};
we use the intent to go from activity class to another new activity class, and in the new class there is setContentView(R.layout.example); in onCreate method, so the xml layout will be displayed.
but I ask if there is a way to display the xml layout withot making a class activity.
There isn't if want to go to a new activity I believe you need a class. You can do this programatically without any XML.
(however I am reasonably new to android so could be wrong)
Image below is what iv constructed already, but can see the daunting task of creating it for 40 students with 250-300 entires each...What layout would i use if i was to display over 10000 textviews(strings)?
Think of it as a table where there are 40 student names down the side of the xml layout and each of these students had its own row. In that row it displayed them being absent or present with the letter A(Absent) and P(Present).
I need each student to have at least 250 entries or textviews for the school calandar year. So as you can see 250 entries multiplied by 40 students equals alot of individual textviews which is not ideal.
I am stuck on which layout to use; ListView, GridView or is there another easier layout to display all this data that is being passed from another class using the spinners of each student? So every time a user pushes the confirm button from the class with the spinners it will take that string and pass onto the class that i want to display it. Like a roll book for school. Thanks
Forget layouts here. Better draw everything completely on your own. It may be a bit more work, but you have complete control over what's going on, you don't need any XML, and you avoid surprises with the sometimes funny, sparsely documented behavior of those layouts. As a start, you create your own View class and implement a few functions:
public class UiView extends View {
#Override
protected void onDraw(Canvas canvas) {/*...*/}
#Override
public boolean onTouchEvent(MotionEvent event) {/*...*/}
}
and put it into your main layout:
<com.mytool.UiView ...
Then you have complete freedom to tune the behavior by implementing onDraw and onTouchEvent.
I am trying to develop an app with MonoTouch. For some screens I create the UI in code. I have a dynamically created RadioGroup with normally two to four RadioElements (choices). Sometimes the text (dynamic too) is too long for the RadioElement so that on an iPhone the text gets shorted by ... at the end.
I have googled and found no suggestion on how to create multiline RadioElements. I know you can create a MultilineElement if you want text over several lines, but how would I go about combining the two? I assume I have to inherit from RadioElement and override some events, but which? Is it the MonoTouch.UIKit.UITableViewCell GetCell (MonoTouch.UIKit.UITableView tv) event?
Alternatively, how would you go about creating a MultilineElement that get a check mark on it when it got pressed/selected?
Or should I be thinking completely different? Are there some other components that could solve this in a simpler way?
This is a sort-of solution (at least it solves my problem):
public class MultilineRadioElement : RadioElement
{
public MultilineRadioElement (string caption, string group)
: base(caption, group) { }
public override MonoTouch.UIKit.UITableViewCell GetCell
(MonoTouch.UIKit.UITableView tv)
{
var cell = base.GetCell (tv);
cell.TextLabel.Lines = 2;
return cell;
}
}
When I now use a MultilineRadioElement instead of a RadioElement the text shows on two lines. For me two lines are enough. If the text is even longer and you still want to fit it inside the RadioElement at its current size you will probably have to calculate the size of the text and for example set the font in cell.TextLabel.Font to a more appropriate font.
I don't know how to make the RadioElement itself get bigger, so this is my best suggestion.
By the way, you should probably also add
protected override MonoTouch.Foundation.NSString CellKey
{
get { return base.CellKey; }
}
to the MultilineRadioElement as the intellisense in my Xamarin Studio suggests that this should be done when overriding GetCell, although it seems to work without it as well.
I am very happy if someone has an even better suggestion!! (yes, two exclamation marks :) )
In my app, one scene having the popup dialog which consists of some fields and buttons. If you click on the button then I want to dismiss the popup dialog as well as update the some fields in the scene. Indirectly I want to refresh scene. is it possible?
I used the following code.Here what I did is, I get the controller of that scene and then update the field using id. but it doesn't work.
URL location = AdmincontrolController.class.getResource("admincontrol.fxml");
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(location);
fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
try {
Parent root = (Parent) fxmlLoader.load(location.openStream());
AdmincontrolController controller = fxmlLoader.getController();
System.out.println("AdmincontrolController: "+controller);
controller.setEmail(item.getEmail());
} catch (IOException ex) {
Logger.getLogger(Add_loginController.class.getName()).log(Level.SEVERE, null, ex);
}
Scenario:
scene
Popup - If we clicks on the add then we need to dismiss that dialog and change the email text on the previous scene.
As Alexander mentioned above, updating the underlying text property of the object you are using to display the email should Just Work. You need to make sure that you are working with the property (see Oracle Java FX Property Tutorial for more info). As a concrete example:
FXML
<Text fx:id="email" />
<TextField fx:id="emailInput" />
<Button onAction="#doSetEmail" text="Set Email"/>
In your controller, use the #FXML annotation to inject concrete instances of objects and set the handler to adjust the text:
Controller
#FXML
Text email;
#FXML
TextField emailInput;
#FXML
public void doSetEmail(ActionEvent ae) {
email.setText(emailInput.getText());
}
Alternatively, you could just bind the email text property to the email label property so that changes would automatically get propagated:
email.textProperty().bind(emailInput.textProperty());
You could do this in your controller initialize() method.
Now, the caveat to all this working depends on how you are handling the event and what you are doing in this. You still haven't posted the code for that as requested by the first answer, so you may be having issues there. Namely, if you are starting threads and then trying to update UI elements on the JavaFX thread from a worker thread, then you can get into trouble (potentially) with things not updating. This depends substantially on the structure of your objects, and you have not given enough information to comment in any meaningful way on that.
chooks
Everytime you have the Feeling that you manually want to update a Scene you should probably use a backgroundWorker Thread to do the work.
This way your UI Thread can use the time to update Labels etc.
JavaFX is built so that you don't need to directly call the scene update routine. All you need - update properties of scene components, and they will be updated on the nearest pulse.
So, all you need is to update properties. Or, is there any real trouble?
refreshing scene its not possible without closing...but if you can do class level declaration for control..i.e make them static its may work...
try this..
make a function in main file.
MainPanel.java
public static void SetMail(String email)
{
txtmail.setText(email);
}
LoginPanel.java
btnclear.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
MainPanel.SetMail(txtEmail.getText());
}
});