I call the method below to update my listview
ListView list = (ListView) listView.findViewById(R.id.plan_list);
itemsList = sortAndAddSections(getItems_search(name));
ListAdapter adapter = new ListAdapter(getActivity(), itemsList);
list.setAdapter(adapter);
but that code is associated with a value that changes and also this is the other code
private ArrayList<plan_model> getItems_search(String param_cusname) {
Cursor data = myDb.get_search_plan(pattern_email, param_name);
int i = 0;
while (data.moveToNext()) {
String date = data.getString(3);
String remarks = data.getString(4);
items.add(new plan_model(cusname, remarks);
}
return items;
}
and this is my sorter
private ArrayList sortAndAddSections(ArrayList<plan_model> itemList) {
Collections.sort(itemList);
plan_model sectionCell;
tempList.clear();
tmpHeaderPositions.clear();
String header = "";
int addedRow = 0;
int bgColor = R.color.alt_gray;
for (int i = 0; i < itemList.size(); i++) {
String remarks = itemList.get(i).getRemarks();
String date = itemList.get(i).getDate();
if (!(header.equals(itemList.get(i).getDate()))) {
sectionCell = new plan_model(remarks, date);
sectionCell.setToSectionHeader();
tmpHeaderPositions.add(i + addedRow);
addedRow++;
tempList.add(sectionCell);
header = itemList.get(i).getDate();
bgColor = R.color.alt_gray;
}
sectionCell = itemList.get(i);
sectionCell.setBgColor(bgColor);
tempList.add(sectionCell);
if (bgColor == R.color.alt_gray) bgColor = R.color.alt_white;
else bgColor = R.color.alt_gray;
}
tmpHeaderPositions.add(tempList.size());
for (int i = 0; i < tmpHeaderPositions.size() - 1; i++) {
sectionCell = tempList.get(tmpHeaderPositions.get(i));
sectionCell.setDate(sectionCell.getDate() + " (" +
(tmpHeaderPositions.get(i + 1) - tmpHeaderPositions.get(i) - 1) + ")");
}
return tempList;
}
my question is the value name changes but my listview is not how can I update my listview? because i need to update it based on search parameter
If your itemList is being updated properly, you don't need to create another instance of the adapter, just use notifyDataSetChanged():
private void createList() {
ListView list = (ListView) listView.findViewById(R.id.plan_list);
itemsList = sortAndAddSections(getItems_search(name));
adapter = new ListAdapter(getActivity(), itemsList);
list.setAdapter(adapter);
}
private void updateList() {
sortAndAddSections(getItems_search(name)); // Update itemList without re-assign its value, otherwise the adapter will loose reference
adapter.notifyDataSetChanged()
}
In getItems_search() add this line at the beginning:
items.clear();
Every time the value of name changes you have to do the following:
itemsList.clear();
itemsList = sortAndAddSections(getItems_search(name));
list.setAdapter(new ListAdapter(getActivity(), itemsList));
Related
I am new in android and I am trying to get the duplicate values in an array list. I have searched over the internet but I got nothing.
My project has two array lists one is for numbers and the second one is for displaying them. And if the first array list contains a duplicate values, the second one will display all the values and the duplicate ones will be displayed with a stars to mark them as a duplicate values.
So I have tried the following code but I have got nothing. In this situation, array list contains [9,9,5,7].
The problem is I do not get what I need, here is my code.
Note that: arrayList object that contains the numbers, array object will display them.. So any help on this?
arrayList.add(Integer.valueOf(students.getSeatnum()));
array.add(students.getStuden_name() + ", Student " + students.getStudent_id() + ", Seat Num: " + students.getSeatnum());
Set<Integer> seenValues = new HashSet();
for(Integer value: arrayList) {
if(seenValues.contains(value)) {
array.add(students.getStuden_name() + ", Student " + students. getStudent_id() + ", Seat Num: " + students.getSeatnum()+ "****");
adapter = new ArrayAdapter<String>(StudentInfo.this, android.R.layout.simple_list_item_1, array);
listView.setAdapter(adapter);
} else {
adapter = new ArrayAdapter<String>(StudentInfo.this, android.R.layout.simple_list_item_1, array);
listView.setAdapter(adapter);
}
}
Create an Adapter which accepts arrayList you have created.
Assuming you are using ListView from your variable name convention.
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final List<String> values;
public MySimpleArrayAdapter(Context context, List<String> values) {
super(context, -1, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context. getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
textView.setText(values.get(position));
return rowView;
}
}
Your method will look like this
ArrayList<String> array = new ArrayList<>();
Map<Integer,Integer> seenValues = new HashMap<>();
int i=0;
for(int value : arrayList){
if(seenValues.containsKey(value)){
if(seenValues.get(value) !=-1){
array.set(seenValues.get(value), String.valueOf(value) + "*");
seenValues.put(value,-1);
}
array.add(String.valueOf(value)+"*");
}else{
array.add(String.valueOf(value));
seenValues.put(value,i);
}
i++;
}
MySimpleArrayAdapter adapter = new adapter(context,array);
setListAdapter(adapter);
You can do it this way:
public class Test {
List<Integer> digits ;
List<String> digitsWatch;
public Test() {
digits = Arrays.asList(9,9,9,5,7,3,3);
digitsWatch = new ArrayList<>(digits.size());
// copying your array
for (int i = 0; i < digits.size(); i++) {
digitsWatch.add(digits.get(i).toString());
}
//filtred values
boolean hasDublicaqtes = false;
for (int i = 0; i < digits.size() -1 ; i++) {
for (int j = i +1; j < digits.size() ; j++) {
if(digitsWatch.get(j).equals(digits.get(i).toString()) ) {
hasDublicaqtes = true;
String tmp = digitsWatch.get(i) + "*";
digitsWatch.set(j,tmp);
}
}
if(hasDublicaqtes) {
String tmp = digitsWatch.get(i) + "*";
digitsWatch.set(i,tmp);
}
hasDublicaqtes = false;
}
}
}
Maybe it's not an optimal way but it works!
I have 2 xml files
Header and Section
then i use some condition to segregate those two and the output of that is this
|8/2/2018|
----------
Data 1
Data 2
|8/2/2018|
----------
Data 1
Data 2
Data 3
It is a listview that groups data with same date and the 1 date header for them.
My question is how can I count each data then update the header? like this
|8/2/2018 (2)|
----------
Data 1
Data 2
|8/2/2018 (3)|
----------
Data 1
Data 2
Data 3
Im using Listview and ListAdapter extends ArrayAdapter
where do i will update?
here
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ItemModel cell = (ItemModel) getItem(position);
if (cell.isSectionHeader()) {
//Display Date in Header XML
} else {
//Display in Section XML
}
}
or here
private ArrayList sortAndAddSections(ArrayList<ItemModel> itemList) {
ArrayList<ItemModel> tempList = new ArrayList<>();
Collections.sort(itemList);
String header = "";
for (int i = 0; i < itemList.size(); i++) {
if (!(header.equals(itemList.get(i).getDate()))) {
String data = itemList.get(i).getRemarks();
String date = itemList.get(i).getDate()
ItemModel sectionCell = new ItemModel(date,data);
sectionCell.setToSectionHeader();
tempList.add(sectionCell);
header = itemList.get(i).getDate();
}
tempList.add(itemList.get(i));
}
return tempList;
}
Updated
Here is how I transfer the data from database to array
private ArrayList<ItemModel> getItems() {
Cursor data = myDb.get_plan(email);
ArrayList<ItemModel> items = new ArrayList<>();
while (data.moveToNext()) {
String date = data.getString(3);
String data1 = data.getString(4);
items.add(new ItemModel(date,data1));
}
return items;
}
then this where the condition goes
private ArrayList sortAndAddSections(ArrayList<ItemModel> itemList) {
ArrayList<ItemModel> tempList = new ArrayList<>();
String header = "";
for (int i = 0; i < itemList.size(); i++) {
if (!(header.equals(itemList.get(i).getDate()))) {
String date = itemList.get(i).getDate();
String getData1 = itemList.get(i).getData1();
ItemModel sectionCell = new ItemModel(date,data1);
sectionCell.setToSectionHeader();
tempList.add(sectionCell);
header = itemList.get(i).getDate();
}
tempList.add(itemList.get(i));
}
return tempList;
}
this is where i set it in textview
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ItemModel cell = (ItemModel) getItem(position);
if (cell.isSectionHeader()) {
v = inflater.inflate(R.layout.section_header, null);
v.setClickable(false);
TextView header = (TextView) v.findViewById(R.id.list_date);
header.setText(cell.getDate());
} else {
v = inflater.inflate(R.layout.listview_plan, null);
TextView tv_data1 = v.findViewById(R.id.list_data1);
tv_cusname.setText(cell.getData1());
}
return v;
}
Try this:
private ArrayList sortAndAddSections(ArrayList<ItemModel> itemList) {
ArrayList<ItemModel> tempList = new ArrayList<>();
ArrayList<Integer> tmpHeaderPositions = new ArrayList<>(); // Added
String header = "";
ItemModel sectionCell; // Changed
int addedRow = 0; // Edited
for (int i = 0; i < itemList.size(); i++) {
if (!(header.equals(itemList.get(i).getDate()))) {
String date = itemList.get(i).getDate();
String getData1 = itemList.get(i).getData1();
sectionCell = new ItemModel(date,data1); // Changed
sectionCell.setToSectionHeader();
tmpHeaderPositions.add(i + addedRow); // Edited
addedRow++; // Edited
tempList.add(sectionCell);
header = itemList.get(i).getDate();
}
tempList.add(itemList.get(i));
}
// Added this code block
tmpHeaderPositions.add(tempList.size());
for (int i = 0; i < tmpHeaderPositions.size() - 1; i++) {
sectionCell = tempList.get(tmpHeaderPositions.get(i));
sectionCell.setDate(sectionCell.getDate() + "(" +
(tmpHeaderPositions.get(i + 1) - tmpHeaderPositions.get(i) - 1) + ")");
}
return tempList;
}
Hope that helps!
You can setText later when you get the list of data and then with the help of arraylist.length() you can set the text into section.
I want to update State list according to the country type in autocompletetextview. I have used the below code .It is working fine for the first time .I write country but once i edit the country name .It is not updating the state list(used notifyDataSetChanged but its not working).Please check the code .Thanks!
AutoCompleteTextView country_edit,state_edit;
for (int i = 1; i < global.getExcel().length; i++) {
String[] separated = global.getExcel()[i].split(",");
code = separated[0]; // this will contain "Fruit"
country = separated[1];
Log.e("length", code + "==" + country);
Log.e("sep", separated + "====" + global.getExcel()[i]);
countrylist.add(country);
codelist.add(code);
Log.e("countrylist", countrylist + "==");
}
//==============================================ArrayAdapter Country & state
final ArrayAdapter<String> country_adapter = new ArrayAdapter<String>
(this, R.layout.custom_autolistview_list, R.id.text1, countrylist);
country_edit.setThreshold(1);
country_edit.setAdapter(country_adapter);
final ArrayAdapter<String> state_adapter = new ArrayAdapter<String>
(this, R.layout.custom_autolistview_list, R.id.text1, State_array);
state_edit.setThreshold(1);
state_edit.setAdapter(state_adapter);
state_edit.setDropDownHeight(WRAP_CONTENT);
state_adapter.notifyDataSetChanged();
country_edit.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Log.e("country", country_edit.getText().toString());
country_str = country_edit.getText().toString();
// Log.e("country", country_str);
int index = countrylist.indexOf(country_str);
Log.e("index", index + "");
String code_index = codelist.get(index);
Log.e("code", code_index);
State_list.clear();
State_array.clear();
for (int i = 0; i < global.getState().length; i++) {
if (global.getState()[i].contains("," + code_index + ",")) {
String statelist = global.getState()[i];
Log.e("statelist", statelist);
State_list.add(statelist);
Log.e("State", State_list + "");
state_name = "";
for (int j = 0; j < State_list.size(); j++) {
String[] statelist_array = State_list.get(j).split(",");
state_name = statelist_array[0];
String state_code = statelist_array[1];
Log.e("state_code", state_code);
Log.e("state_name", state_name);
}
Log.e("Stateeeeeeeee", State_array + "");
State_array.add(state_name);
Log.e("Stateeeeeeeeeeeeeeee", State_array + "");
}
state_ll.setVisibility(View.VISIBLE);
}
state_adapter.notifyDataSetChanged();
}
});
i have made ListView with three columns 'item','qty','rate' i get this entries from the user and i have made the listview work perfectly but i want to get all the values of the 'rate' column and add them for the net amount.
Here is my android code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
populateList();
adapter = new listviewAdapter(List.this, list);
lview.setAdapter(adapter);
}
private void populateList() {
HashMap temp = new HashMap();
list = new ArrayList<HashMap>();
item = etItem.getText().toString();
qty = etQty.getText().toString();
rate = etRate.getText().toString();
temp.put(FIRST_COLUMN, "1");
temp.put(SECOND_COLUMN, item);
temp.put(THIRD_COLUMN, qty);
temp.put(FOURTH_COLUMN, rate);
list.add(temp);
}
I tried out this method below but it only toast the first value.But i want to get all the values under the rate column and add them up for the net-amount.
public void get() {
StringBuilder sb = new StringBuilder();
for(int i=0; i<adapter.getCount(); i++) {
String a = ((TextView) findViewById(R.id.FourthText)).getText().toString();
adapter.getItem(i).toString();
sb.append(a);
sb.append("\n");
}
text = sb.toString();
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
}
I think this might help you.
private int GrandTotal(ListView list) {
int sum=0;
for (int i = 0; i < list.getCount(); i++) {
View v = list.getChildAt(i);
TextView rate = (TextView) v.findViewById(R.id.rate);
sum = sum + Integer.parseInt(rate.getText().toString() )
}
return sum;
}
:)
public void getTotalRate() {
int totalRate = 0;
for (int i = 0; i < list.size(); i++) {
HashMap temp = list.get(i);
/// here fourth column is rate
int rate = (int) temp.get(FOURTH_COLUMN);
totalRate = totalRate + rate;
}
Toast.makeText(getApplicationContext(), "total rate=" + totalRate, Toast.LENGTH_LONG).show();
}
I'm tried to sort the database from a web service by using the bubble sort.
itemLists[i] = new ItemList(
a.getString(id),
a.getString(nama),
a.getString(latitude),
a.getString(longitude),
a.getString(alamat));
double terendah = Double.valueOf(a.getString("TAG_TERENDAH")).doubleValue();
//double terendah = a.getDouble(terenda);
harga[i] = terendah;
//bubble sort
double tHarga;
ItemList tItemList;
for (int k = 0; k < tempatrental.length(); k++) {
for (int l = 0; l < tempatrental.length() - (k + 1); l++) {
if (harga[l] > harga[l + 1]) {
tHarga = harga[l];
tItemList = itemLists[l];
harga[l] = harga[l + 1];
itemLists[l] = itemLists[l + 1];
harga[l + 1] = tHarga;
itemLists[l + 1] = tItemList;
}
}
Anyways, database from the web service is array type, so I store it in the array from another class.
public class ItemList{
public String label1, label2, label3, label4, label5;
public ItemList(String label1, String label2, String label3, String label4, String label5) {
super();
// TODO Auto-generated constructor stub
//this.icon = icon;
this.label1 = label1;
this.label2 = label2;
this.label3 = label3;
this.label3 = label4;
this.label3 = label5;
}
The problem came when I tried to show the results of sorting in the list view.
this.setListAdapter (new ArrayAdapter<String>(TermurahActivity.this, android.R.layout.simple_list_item_1, itemLists));
Eclipse says : The Constructor ArrayAdapter(TermurahActivity, int, ItemList[]) is undefined. Please help fix to fix this...
Once you defined ArrayAdapter<String> the adapter expects a list of String.
ItemLists is a list of ItemList so you should use:
new ArrayAdapter<ItemList>(TermurahActivity.this,
android.R.layout.simple_list_item_1, itemLists);`