List elements and tabbed-panel contents are populating from json file.
When I click A element in listview1 it has to go to tabbeddpanel(above).
When I click B element in listview1 it has to go to listview2 screen and when I click B1 element in listview2 it has to go to tabbedpanel(below).
Help me in doing this
Modify the data set passed to your listview1 so that you can add a tag here.
For example, the data set you're passing in your listview1 is an ArrayList of tis object.
public class myData {
private String content;
private int tag; // Add a tag attribute to handle them differently in the list.
}
// Here's the list passed to your `listview1`
private ArrayList<myData> myDataList = new ArrayList<myData>();
Now inside your bindView function check for the tag of the object of that position and handle the action differently like this.
if(myDataList.get(position).getTag() == 0) {
// Do this
} else {
// Do something else
}
Related
I'm trying to create a method to set a listener to each of my views inside a list like:
private fun setListeners() {
val clickableViews: List<View> =
listOf(box_one_text, box_two_text, box_three_text,
box_four_text, box_five_text)
for(item in clickableViews){
item.setOnClickListener{makeColored(it)}
}
}
box_one_text, box_two_text and so on are the id of the views inside my xml file and I'm trying to set a color of it when they are clicked like:
fun makeColored(view: View) {
when (view.id) {
// Boxes using Color class colors for background
R.id.box_one_text -> view.setBackgroundColor(Color.DKGRAY)
R.id.box_two_text -> view.setBackgroundColor(Color.GRAY)
// Boxes using Android color resources for background
R.id.box_three_text -> view.setBackgroundResource(android.R.color.holo_green_light)
R.id.box_four_text -> view.setBackgroundResource(android.R.color.holo_green_dark)
R.id.box_five_text -> view.setBackgroundResource(android.R.color.holo_green_light)
else -> view.setBackgroundColor(Color.LTGRAY)
}
}
the problems is that all of the elements inside the list are all red lines or can't reference by the list
First thing first, the list type is wrong. You said that box_one_text and box_two_text are the view id. View id type is Int, so you should change the list to list of Int
val clickableViews: List<Int> =
listOf(box_one_text, box_two_text, box_three_text,
box_four_text, box_five_text)
Then, to apply a click listener to each of the id, you need to find the view using findViewById
for(item in clickableViews){
findViewById<View>(item).setOnClickListener{makeColored(it)}
}
Or if you use view binding, you can follow below code:
val clickableViews: List<View> =
listOf(binding.boxOneText, binding.boxTwoText, binding.boxThreeText,
binding.boxFourText, binding.boxFiveText)
for(item in clickableViews){
item.setOnClickListener{makeColored(it)}
}
Try this:
private fun setListeners() {
val clickableViews: List<Int> =
listOf(R.id.box_one_text, R.id.box_two_text, R.id.box_three_text,
R.id.box_four_text, R.id.box_five_text)
for(item in clickableViews){
findViewById<View>(item).setOnClickListener{makeColored(it)}
}
}
My Adapter requires a context in order to apply resources to views, therefore when instantiating it, I might do the following within my Activity:
myAdapter = new MyAdapter(this);
As my adapter also needs data from an activity, I might do this:
myAdapter = new MyAdapter(myItemsArrayList,this);
As my adapter might also need to know which items in the ArrayList are selected, I might pass it that list too:
myAdapter = new MyAdapter(myArrayItemsList,mySelectedItemsArrayList,this);
And as there may be other states (e.g. whether to display photos in a list of people, the constructor call is starting to get quite lengthy:
myAdapter = new MyAdapter(myArrayItemsList,mySelectedItemsArrayList,myPreference1,myPreference2,this);
Given that the only place this adapter will be used is from a particular activity, how bad would it be to just make those attributes in the activity public, so that I can access them via the activity that has been passed (e.g myActivity.myArrayItemsList)?
Many thanks in advance for any advice!
Given that the only place this adapter will be used is from a particular activity, how bad would it be to just make those attributes in the activity public, so that I can access them via the activity that has been passed (e.g myActivity.myArrayItemsList)?
That's a bad code and bad behavior. You're code will be tightly coupled. And usually, you will borrow the same behavior to your next project.
Instead of passing each state to your constructor, you can simplify it by passing a State object to your adapter. Create the State class something like this:
public class State {
List<String> selectedItems;
boolean displayPeople;
}
then you can create a simple constructor like this:
State state = new State();
state.selectedItems = mSelectedItems;
state.displayPeople = true;
myAdapter = new MyAdapter(items, state, this);
So, whenever you need to update a new state, you just need to add it the State class and update the Adapter according to it.
Considering you are using the Item object for myArrayItemsList.
So your list should look like this:
ArrayList<Item> myArrayItemsList = new ArrayList();
and then you want to add the selected items in the list you could add a boolean to the Item object ex:
public class Item {
private String itemName;
private boolean selected = false;
public Item(){}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName= itemName;
}
public boolean isSelected () {
return selected;
}
public void setSelected(boolean selected) {
this.selected= selected;
}
}
and just check your item list in the adapter if the item is selected.
So your adapter would only pass two parameters:
MyAdapter myAdapter = new MyAdapter(myArrayItemsList, this);
But then again you want to pass only one parameter in adapter, you can set your ArrayList to static
public static ArrayList<Item> myArrayItemsList = new ArrayList();
and pass only this your adapter
MyAdapter myAdapter = new MyAdapter(this);
used the static ArrayList in your adapter but it is not advisable using those static data because the data could be Garbage Collected in the memory.
I have a list view that is populated by an observable Array list. My steps are the following
Populate observable array list
Populate list view using listView.setItems() method
set custom cell using listView.setCellFactory(),
Attach listener for list view.
More particularly attachListener happens this way
private void attachListViewListener() {
testServersListView.getSelectionModel().selectedItemProperty().addListener((ObservableValue <? extends TestserverInformation> ov, TestserverInformation oldValue, TestserverInformation newValue )->{
this.selectedInfo = testServersListView.getSelectionModel().getSelectedItem();
updateTabs();
});
}
I want to be able to refesh the list by pushing a button. The methods are the following
#FXML
public void btnRefreshServerListClicked(){
log.info("Refreshing list");
testServersListView.getItems().clear();
fillObservableTestserverInfoList();
testServersListView.setItems(observableTestserverInfoList);
selectAndUpdate(); //selects the first element on the list
}
The problem is that when I clear the list items selectedInfo variable is set to null and the Listener is tiggered, causing a nullpointerexception (because selectAndUpdate uses selectedInfo for updating it's controls. Can I remove the listener without having to set it as a different variable e.g?
private myListener = new ChangeListener<...>(){
};
My program is based on a API. I got a JList with and a model that has a some names. And a selectListener to get the seleted item and a button to send that item to the other window which has another Here is my first list:
First List (window) and send the items to the other list.
final DefaultListModel<String> Names = new DefaultListModel<String>();
final JList<MyAPI> Places = new JList(Names);
private JList<MyAPI> locList;
private DefaultListModel<MyAPI> favourites;
public AddLocation(JList<MyAPI> locList, DefaultListModel<MyAPI> favourites){
this.locList = locList;
this.favourites = favourites;
}
addThis.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Object chose = Places.getSelectedValue();
favourites.addElement((MyAPI) chose); // error in this line
}
});
And this is the other window that selected items should be added to here:
final DefaultListModel<MyAPI> favourites;
final JList<MyAPI> locList;
favourites = new DefaultListModel<MyAPI>();
locList = new JList<MyAPI>(favourites);
So now both windows loads and the first list loads with its names in it. but when I press the button add this, it gives error and points to this line:
favourites.addElement((MyAPI) chose);
How can I solve it?
Your first model is defined like this:
final DefaultListModel<String> Names ...;
Your second model is defined like this:
final DefaultListModel<MyAPI> favourites;
Your first list model conains String instances, your second model contains MyAPI instances. So when this line is executed:
favourites.addElement((MyAPI) chose);
you are trying to make a MyAPI out of a String, which does not work and probably a ClassCastException is thrown.
Either you need to declare the second list model as final DefaultListModel<String> favourites; or you create an instance of MyAPI based on the selected String (new MyAPI(chose) ?).
Okay so i have a database, and i want it so you can favorite items.
Would i make it so you can save items then it will load it into a list view?
this is my load thing
//Calls the database, gets a list of names.
// if listofnames.size()==0 keep name, otherwise
// change name to first name.
ArrayList<String> nameList = new ArrayList<String>();
favList = db.getName();
if(favList.size()>0){
name.setText(favList.get(0));
But that just sets a text i want it to add items..
You may use ArrayAdapter or SimpleAdapter or BaseAdapter - through which you may bind dataSource (List<T>) to the ListView.
You can do this as follows
public class MyClass extends ListActivity{
public void onCreate(Bundle bundle){
//get the names from database
setListAdapter(new ArrayAdapter<E>(this,R.layout.xml_filename,your_list);
}
}
Remember in this case your xml file should be the TextView (I.e the items what list view should contain). You cannot pass an xml file with a ListView directly.
If you have still some problem, then post your code which can be solved.
You can follow the given link for more clarification.
http://developer.android.com/resources/tutorials/views/hello-listview.html