Android listview insert the deleted Value in same position - java

I am working in list view. In it, there are two lists such that if I select a value from base list, it will add to the child list and remove the item from base list, all are working fine. My query is delete list item from child list it will re-add to the base list.
contacts = jsonObj.getJSONArray(TAG_PRODUCTLIST);
for (int i = 0; i < contacts.length(); i++)
{
JSONObject c = contacts.getJSONObject(i);
Object id = c.get(TAG_CATID);
Object em = c.get(TAG_CAT);
contact1 = new HashMap<>();
contact1.put(TAG_CATID, id);
contact1.put(TAG_CAT, em);
JSONArray productss = c.getJSONArray(TAG_PRODUCT);
worldpopulationlist.add(new QuoteSectionItem(em.toString()));//listview section
for (int j = 0; j < productss.length(); j++)
{
JSONObject d = productss.getJSONObject(j);
Object pid = d.get(TAG_PID);
Object price = d.get(TAG_PRICE);
Object pn = d.get(TAG_PNAME);
contact = new HashMap<>();
contact.put(TAG_PID, pid);
contact.put(TAG_PRICE, price);
contact.put(TAG_PNAME, pn);
contact.put(TAG_PDESC, pn);
Quote_Products worldpopulation = new Quote_Products(id,em,pid,pn,price);//listview item
worldpopulationlist.add(worldpopulation);
} }
this is base list creation and i create child list like` SparseBooleanArray selected = listviewadapter.getSelectedIds();
for (int i = (selected.size() - 1); i >= 0; i--)
{
if (selected.valueAt(i))
{
QuoteItem selectedI = listviewadapter.getItem(selected.keyAt(i));
Quote_Products selecteditem = (Quote_Products) selectedI;
System.out.println("Selected Items : "+selecteditem.getCountry());
System.out.println("Selected Items : "+selecteditem.getRank());
System.out.println("Selected Items : "+selecteditem.getPopulation());
System.out.println("Products:"+product);
productList.add(product);
Quote_SelectedProducts worldpopulation1 = new Quote_SelectedProducts(
selecteditem.getCidid(),
selecteditem.getCname(),
selecteditem.getRank(),
selecteditem.getCountry(),
selecteditem.getPopulation());
worldpopulationlist1.add(worldpopulation1);
listviewadapter.remove(selecteditem);
}
}
`
after this are successfully done i need to to how to to reinsert the list value once delete value form child list
SparseBooleanArray selected = listviewadapter1.getSelectedIds();
for (int i = (selected.size() - 1); i >= 0; i--)
{
if (selected.valueAt(i))
{
Quote_SelectedProducts selecteditem = listviewadapter1.getItem(selected.keyAt(i));
listviewadapter1.remove(selecteditem);
System.out.println("Delete after ignore "+selecteditem.getCidid());
System.out.println("Delete after ignore "+selecteditem.getCname());
System.out.println("Delete after ignore "+selecteditem.getRank());
System.out.println("Delete after ignore "+selecteditem.getCountry());
System.out.println("Delet after ignore "+selecteditem.getPopulation());
Quote_Products worldpopulation = new Quote_Products(selecteditem.getCidid(),
selecteditem.getCname(),selecteditem.getRank(),
selecteditem.getRank(),selecteditem.getPopulation());
System.out.println(" EEE "+String.valueOf(worldpopulation));
worldpopulationlist.add(worldpopulation);
System.out.println(" EEE "+String.valueOf(worldpopulationlist));
}
the value delete from child list how to re insert the child list value in the base list
Thanks in advance

Related

JavaFX tableView content from wekaapi is missing

I've got big problem with visualiation of arff files in javaFX table view. My code :
ArffLoader loader = new ArffLoader();
loader.setFile(arff);
Instances data = loader.getDataSet();
List<TableColumn<Instance, String>> atrributes = new ArrayList<>();
for (int i = 0; i < data.numAttributes(); i++) {
atrributes.add(new TableColumn<Instance,String>` (data.attribute(i).name()));
}
List <Instance> instances = new ArrayList<>();
for (int i =0;i<data.size();i++)
{
instances.add(data.get(i));
}
ObservableList<Instance> tableContent = FXCollections.observableArrayList(instances);
table.getColumns().removeAll();
table.getColumns().addAll(atrributes);
table.setItems(tableContent);
table.setVisible(true);
Names of columns(attributes) are set properly but contest is not shwon(tableContent variable)
You need to set a cell value factory for each table column. This is a function that maps the Instance in each row to the value to be displayed in the corresponding cell in that column.
I am not familiar with Weka, but looking at the Javadocs, I think you need
for (int i = 0; i < data.numAttributes(); i++) {
TableColumn<Instance, String> column
= new TableColumn<Instance,String>(data.attribute(i).name());
final int attIndex = i ;
column.setCellValueFactory(cellData ->
new SimpleStringProperty(cellData.getValue().toString(attIndex)));
atrributes.add(column);
}

get unchecked and checked values from a checkbox

I'm trying to print the contents of my checkbox list. I'd like to display all the unchecked (false values) and checked (true values) in the order that it appears in the list view. So far I can only get the true values, how can I get the unchecked false values?
public void selection (){
final ListView lv = (ListView)findViewById(R.id.treeQuestionsList);
Intent intent = getIntent();
int id2 = intent.getIntExtra("id2", 0);
SparseBooleanArray checked = lv.getCheckedItemPositions();
int size = checked.size();
for (int i = 0; i < size; i++) {
int key = checked.keyAt(i);
entries.add(new Observation(id2,lv.getCheckedItemPositions().get(key)));
Log.d(Constants.TAG,"--ID:="+id2+"--checkeddata----"+ entries.get(i).answers);
}
}
From the documentation available on Android Developers, you might need to use a combination of the value along with the key in order to get the desired result.
for (int i=0; i < checked.size(); i++) {
if (checked.valueAt(i)) {
int key = checked.keyAt(i);
Log.i(TAG, key + " is selected");
} else {
int key = checked.keyAt(i);
Log.i(TAG, key + " is not selected");
}
}
You can also take a look at what getCheckedItemPositions() does.

Temp arrays not working after adding 3rd Item

I had to make a Temp array to keep resizing the array list if the user decides to keep adding items to the cart, but my Temp array works until I try to add 3 different items to the cart.
I was instructed to do it this way instead of an array list to show the difficulty of arrays.
orderProduct [productCount] = aProduct;
orderQuantity [productCount] = aQuantity;
}
}
You forgot to increase productCount when there is already a product in the cart.
Moreover, you can just set the product and quantity array to the temp arrays instead of copying back.
orderProduct = tempOrderedProducts;
orderQuantity = tempOrderedQuantity;
Because you forgot productCount++ after resizing the array.
The following code will work:
public void setOrderProduct(Product aProduct, int aQuantity) {
if (productCount == 0) {
orderProduct[0] = aProduct;
orderQuantity[0] = aQuantity;
} else {
Product[] tempOrderedProducts = new Product[orderProduct.length + 1];
int[] tempOrderedQuantity = new int[orderQuantity.length + 1];
for (int i = 0; i < orderProduct.length; i++) {
tempOrderedProducts[i] = orderProduct[i];
tempOrderedQuantity[i] = orderQuantity[i];
}
orderProduct = new Product[tempOrderedProducts.length];
orderQuantity = new int[tempOrderedQuantity.length];
for (int i = 0; i < orderQuantity.length; i++) {
orderProduct[i] = tempOrderedProducts[i];
orderQuantity[i] = tempOrderedQuantity[i];
}
orderProduct[productCount] = aProduct;
orderQuantity[productCount] = aQuantity;
productCount++; //you forgot this
}
}
What's more, there is a simple way to deal with array copy:
public void setOrderProduct(Product aProduct, int aQuantity) {
if (productCount == 0) {
orderProduct[0] = aProduct;
orderQuantity[0] = aQuantity;
} else {
Product[] tempOrderedProducts = new Product[orderProduct.length + 1];
int[] tempOrderedQuantity = new int[orderQuantity.length + 1];
//System.arraycopy is more convenient and efficient
System.arraycopy(orderProduct, 0, tempOrderedProducts, 0, orderProduct.length);
System.arraycopy(orderQuantity, 0, tempOrderedQuantity, 0, orderQuantity.length);
//you don't need to copy back, just re-assign the reference
orderProduct = tempOrderedProducts;
orderQuantity = tempOrderedQuantity;
orderProduct[productCount] = aProduct;
orderQuantity[productCount] = aQuantity;
productCount++;
}
}

Jfreechart: how to get the individual items in a BoxAndWhiskerChart

I am generating a box and whisker chart with one item per category.I also want to generate a report with the mean, median and all the values per item in the BoxPlot. So, after i create the dataset, defaultboxandwhiskercategorydataset based on the categoryType, I call the method convertReportData to fetch each item in the defaultboxandwhiskercategorydataset and save the mean, median etc into another data object later for report generation. But it just prints only one category. Could anyone please help me to figure out what is wrong?
My boxplot
Code:
public static BoxAndWhiskerCategoryDataset createDataset() {
startTime = inputData.getItimeFrom();
endTime = inputData.getItimeTo();
List<String> categorylist = new ArrayList<>();
categorylist.add("Distance 0-20");
categorylist.add("Distance 20-40");
categorylist.add("Distance 40-60");
categorylist.add("Distance 60-80");
categorylist.add("Distance 80-100");
categorylist.add("Distance >100");
Map<String, List<Double>> map = new HashMap<String, List<Double>>();
map = addDistance(values_list);
DefaultBoxAndWhiskerCategoryDataset defaultboxandwhiskercategorydataset = new DefaultBoxAndWhiskerCategoryDataset();
for (String categoryType : categorylist) {
map.remove(null);
for (Map.Entry<String, List<Double>> entry : map.entrySet()) {
if (entry.getKey().equalsIgnoreCase(categoryType)) {
defaultboxandwhiskercategorydataset.add(entry.getValue(),
categoryType, " ");
}
}
}
convertReportData(defaultboxandwhiskercategorydataset, categorylist);
return defaultboxandwhiskercategorydataset;
}
private static void convertReportData(DefaultBoxAndWhiskerCategoryDataset boxandwhiskercategorydataset, List<String> latencyTypelist) {
report = new HashMap<>();
for (int i = 0; i < boxandwhiskercategorydataset.getColumnKeys().size(); i++) {
BoxAndWhiskerItem item = boxandwhiskercategorydataset.getItem(i, 0);
ReportData data = new ReportData();
data.setMean(item.getMean());
data.setMedian(item.getMedian());
data.setQ1(item.getQ1());
data.setQ3(item.getQ3());
data.setMaxOutlier(item.getMaxOutlier());
data.setMaxRegularNumber(item.getMaxRegularValue());
data.setMinOutlier(item.getMinOutlier());
data.setMinRegularNumber(item.getMinRegularValue());
data.setOutliers(item.getOutliers());
report.put(boxandwhiskercategorydataset.getRowKey(i).toString(),
data);
}
}
The problem is with
for (int i = 0; i < boxandwhiskercategorydataset.getColumnKeys().size(); i++) {
you are using getColumnKeys whereas you have only one Column. It should have been,
for (int i = 0; i < boxandwhiskercategorydataset.getRowKeys().size(); i++) {

How to add different child lists for different parents lists in ExpandableListView for Android?

I'm using an ExpandableListView to get an expandable list. I have 3 expandable "parent" lists: Price, Details, Notes. I want to be able to add unique child lists for each parent list. But the way its set up now, the same child list is being added for each parent list. How do I make it so I can add unique, separate child lists for Price, Details, and Notes?
Here is my code:
public class Assignment extends ExpandableListActivity {
int listFlag = 0;
#SuppressWarnings("unchecked")
public void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_assignment);
SimpleExpandableListAdapter expListAdapter =
new SimpleExpandableListAdapter(
this,
createGroupList(), // Creating group List.
R.layout.group_row, // Group item layout XML.
new String[] { "Group Item" }, // the key of group item.
new int[] { R.id.row_name }, // ID of each group item.-Data under the key goes into this TextView.
createChildList(), // childData describes second-level entries.
R.layout.child_row, // Layout for sub-level entries(second level).
new String[] {"Sub Item"}, // Keys in childData maps to display.
new int[] { R.id.grp_child} // Data under the keys above go into these TextViews.
);
setListAdapter( expListAdapter ); // setting the adapter in the list.
}catch(Exception e){
System.out.println("Errrr +++ " + e.getMessage());
}
}
//Create Headings of Assignment attributes
private List createGroupList() {
ArrayList result = new ArrayList();
//Create string array for Topic headings
String[] topics = {"Price", "Details", "Notes"};
//Iterate through array of names to lay them out correctly
for( int i = 0 ; i < 3 ; ++i ) {
listFlag = i;
HashMap m = new HashMap();
m.put( "Group Item", topics[i] ); // the key and it's value.
result.add( m );
}
return (List)result;
}
#SuppressWarnings("unchecked")
private List createChildList() {
ArrayList result = new ArrayList();
for( int i = 0 ; i < 3 ; ++i ) { // this -15 is the number of groups(Here it's fifteen)
/* each group need each HashMap-Here for each group we have 3 subgroups */
ArrayList secList = new ArrayList();
for( int n = 0 ; n < 1 ; n++ ) {
HashMap child = new HashMap();
child.put( "Sub Item", " "test"));
secList.add( child );
}
result.add( secList );
}
return result;
}
Android does not support nested listviews.
Your createChildList needs to return a List<ArrayList<HashMap<String, String>>>.
Your createChildList should look something like this:
private List<ArrayList<HashMap<String, String>>> createChildList(int maxValue) {
ArrayList<ArrayList<HashMap<String, String>>> groupList =
new ArrayList<ArrayList<HashMap<String, String>>>();
for (int i = 0; i <= maxValue; i++) {
ArrayList<HashMap<String, String>> childList =
new ArrayList<HashMap<String, String>>();
for (int j = 0; j <= maxValue; j++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("Sub Item", "test: " + String.valueOf(j));
childList.add(map);
}
groupList.add(childList);
}
return groupList;
}
Let me know if that works.

Categories

Resources