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.
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 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));
I created a custom ArrayAdapter and inside the ArrayAdapter there's a LinearLayout that adds views via iteration. Now after showing the data that needs to be shown on a ListView all will be well until I scroll down or up the list, when a row undergoes recycling and binding the new view to display on the recycled row I get duplicates, however this duplicates have show no data(text) on the views. Here is the view ListView before scrolling
https://postimg.org/image/z36eqhacd/
and here is the ListView after scrolling
https://postimg.org/image/dzv28kj9t/
I've tried multiple methods but can't seem to get it to work.
public class SurveyAdapter extends ArrayAdapter<SurveyModel> {
public SurveyAdapter(Context context, ArrayList<SurveyModel> objects) {
super(context, R.layout.survey_card, objects);
}
#NonNull
#Override
public View getView(int position, View convertView, #Nullable ViewGroup parent) {
//Get the data item for this position
SurveyModel surveyModel = getItem(position);
//Construct the ViewHolder
ViewHolder viewHolder = new ViewHolder();
//Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
//If there's no view to re-use, inflate a new view for a row
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.survey_card, parent, false);
viewHolder.mContainer = (LinearLayout) convertView.findViewById(R.id.choice_container);
viewHolder.question = (TextView) convertView.findViewById(R.id.question);
//Cache the viewHolder object inside the new view
convertView.setTag(viewHolder);
} else {
//View is being recycled, retrieve the viewHolder object from tag
viewHolder = (ViewHolder) convertView.getTag();
}
assert surveyModel != null;
//Populate the data from the data object
String choices = surveyModel.getChoice();
//Scan each string separately
//Strings are separated with a comma ','
Scanner scanner = new Scanner(choices).useDelimiter(",\\s*");
//Create an array list to store each string separately
ArrayList<String> choiceList = new ArrayList<>();
//Get all strings from scanner separately
while (scanner.hasNext()) {
String choice = scanner.next(); //Get each string from scanner
choiceList.add(choice); //Store the string in an ArrayList(choiceList)
}
//Convert choiceList(ArrayList) to a StringArray(choiceArray)
String[] choiceArray = choiceList.toArray(new String[choiceList.size()]);
int choiceNum; //Will store number of choices to be displayed
if (choices.contains("Other,true") || choices.contains("Other,false")) {
//Set number or choices to the length(number of items) of the choiceList(ArrayList)
//Minus 1 to avoid showing "true" as an option
choiceNum = choiceArray.length - 1;
} else {
//Set number or choices to the length(number of items) of the array
choiceNum = choiceArray.length;
}
//Get number of choices from choiceNum
final int numOfChoices = choiceNum;
//Populate each choice string from choiceArray
for (int i = 0; i < numOfChoices; i++) {
//Create a new CheckBox for each choice
AppCompatCheckBox checkBox = new AppCompatCheckBox(getContext());
checkBox.setLayoutParams(new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//Add the CheckBox to its survey_view_list view(mContainer)
viewHolder.mContainer.addView(checkBox);
if (viewHolder.mContainer.getChildAt(i) instanceof AppCompatCheckBox) {
//Set each data item in the choiceArray on its own CheckBox according to position
((AppCompatCheckBox) viewHolder.mContainer.getChildAt(i)).setText(choiceArray[i]);
final ViewHolder finalViewHolder = viewHolder; //Get viewHolder for inner class access
final int checkBoxPosition = i; //Set position of the checked CheckBox
((AppCompatCheckBox) viewHolder.mContainer.getChildAt(i)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
if (checked) {
for (int i = 0; i < numOfChoices; i++) {
//Disable all CheckBoxes when a CheckBox is checked
finalViewHolder.mContainer.getChildAt(i).setEnabled(false);
//Re-enable the checked CheckBox only
finalViewHolder.mContainer.getChildAt(checkBoxPosition).setEnabled(true);
}
} else {
for (int i = 0; i < numOfChoices; i++) {
//Enable all CheckBoxes when the checked CheckBox is unchecked
finalViewHolder.mContainer.getChildAt(i).setEnabled(true);
}
}
}
});
}
}
//Populate the data from the data object via the viewHolder object into the view
viewHolder.question.setText(surveyModel.getQuestion());
//Return the completed view to render on screen
return convertView;
}
//View lookup cache
private static class ViewHolder {
LinearLayout mContainer;
TextView question;
}`
Here is the Object Model
public class SurveyModel {
private String question, choice;
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getChoice() {
return choice;
}
public void setChoice(String choice) {
this.choice = choice;
}
}
Here is the method from the Activity I attach the ArrayAdapter to a ListView
//Method to populate and display data populated from the database
private void populateAndDisplayData() {
//Construct ArrayList
ArrayList<SurveyModel> surveyModelArrayList = new ArrayList<>();
//Construct adapter
SurveyAdapter adapter = new SurveyAdapter(CreateFromScratch.this, surveyModelArrayList);
//Attach the adapter to the ListView
binding.list.setAdapter(adapter);
//Create a ListView
ListView listView = new ListView(CreateFromScratch.this);
//Get all the data from the database
Cursor allDataCursor = tempSurveyDatabase.fetchAllData();
//StringArray to hold the strings from the database
String[] from = new String[]{CustomSurveyDatabase.QUESTION, CustomSurveyDatabase.CHOICES};
//Views to display the string data from StringArray(from)
int[] to = new int[]{R.id.question, R.id.choices};
//Construct a SimpleCursorAdapter
SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(CreateFromScratch.this, R.layout.survey_card, allDataCursor, from, to);
// /Attach the adapter to the ListView
listView.setAdapter(simpleCursorAdapter);
//Construct StringBuilder
StringBuilder jsonBuilder = new StringBuilder();
//Strings to hold the item's data from the database
String question = null;
String choices = null;
//Proceed with collecting each item's data if the SimpleCursorAdapter is not empty
if (simpleCursorAdapter.getCount() > 0) {
//Get every item's id from the database
for (int i = 0; i < simpleCursorAdapter.getCount(); i++) {
//Get each item's database id
long itemId = listView.getAdapter().getItemId(i);
//Get each item from the database
try {
//Construct cursor to fetch an item's data from the database
Cursor cursor = tempSurveyDatabase.fetchItem(itemId);
question = tempSurveyDatabase.questionHolder;
choices = tempSurveyDatabase.choiceHolder;
} catch (SQLException e) {
Log.v(TAG, e.getMessage());
}
if (i == 0) {
jsonBuilder.append("{\"survey\": [").append("{\"question\":\"").append(question).append("\",");
jsonBuilder.append("\"choices\":\"").append(choices).append("\"},");
} else {
jsonBuilder.append("{\"question\":\"").append(question).append("\",");
jsonBuilder.append("\"choices\":\"").append(choices).append("\"},");
}
}
//Remove the last comma from the StringBuilder
jsonBuilder.deleteCharAt(jsonBuilder.length() - 1);
//Close JSON file scopes
jsonBuilder.append("]}");
//Save temporary survey file on the SDCARD
try {
//Delete existing temporary survey
if (TEMP_SURVEY.exists()) {
//noinspection ResultOfMethodCallIgnored
TEMP_SURVEY.delete();
}
FileWriter fileWriter = new FileWriter(TEMP_SURVEY);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(String.valueOf(jsonBuilder));
bufferedWriter.close();
} catch (IOException e) {
Log.v(TAG, "Temp Survey JSON file failed to write.\nReason: " + e.getMessage());
}
//Check if the temporary survey file exists
if (TEMP_SURVEY.exists()) {
//Read the temporary survey file if it exists
new ReadFile(ReadFile.data, ReadFile.output, TEMP_SURVEY);
//Parse JSON string from StringBuilder
try {
JSONObject jsonObjectMain = new JSONObject(ReadFile.output);
JSONArray jsonArray = jsonObjectMain.getJSONArray(SurveyJSONKeys.ARRAY_KEY);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
SurveyModel surveyModel = new SurveyModel();
surveyModel.setQuestion(jsonObject.getString(SurveyJSONKeys.QUESTION_KEY));
surveyModel.setChoice(jsonObject.getString(SurveyJSONKeys.CHOICES_KEY));
surveyModelArrayList.add(surveyModel);
}
} catch (JSONException e) {
Log.v(TAG, "Unable to parse JSON file.\nReason: " + e.getMessage());
}
//Notify the adapter for changes in order to refresh views
adapter.notifyDataSetChanged();
}
}
}`
Since your item view is recycled every time, the container of your answers are just appended with more answers. You have 2 options:
Remove all child views in your container before populating with answers
Do not force a recycle by using getViewTypeCount() and getItemViewType(int).
I have following Json Response,I am trying to set color as per DisplyText,but it only set last color code to whole String,
JSON Response
JAVA Code
ch_list = new ArrayList<String>();
color_list=new ArrayList<String>();
try {
for (int i = 0; i < response.length(); i++) {
JSONObject person = (JSONObject) response
.get(i);
System.out.println("person"+person);
String searchcode = person.getString("searchCode");
System.out.println("searchcode"+searchcode);
JSONArray ja = person.getJSONArray("itemList");
for (int j = 0; j < ja.length(); j++) {
JSONObject jo = ja.getJSONObject(j);
SearchResultModel ch = new SearchResultModel();
ch.setSearch_Name(jo.getString("productName"));
ch.setSearch_Img(jo.getString("productImage"));
ch.setSearch_ImgLoc(jo.getString("productImageLoc"));
ch.setSearch_Price(jo.getString("productSP"));
ch.setSearch_RatingImg(jo.getString("pRatingImgName"));
ch.setSearch_RatingImgPath(jo.getString("pRatingImgPath"));
JSONArray txtdetail=jo.getJSONArray("productOfferText");
System.out.println("txtdetailarray"+txtdetail);
StringBuilder sb = new StringBuilder();
for(int k=0;k<txtdetail.length();k++)
{
JSONObject jdetail=txtdetail.getJSONObject(k);
disptext=jdetail.getString("displayText");
dispclr=jdetail.getString("displayColor");
ch_list.add(disptext);
color_list.add(dispclr);
sb.append(disptext);
System.out.println("clr" + color_list.get(k).toString());
colors=color_list.get(k);
String string = colors;
String[] parts = string.split("\\*");
int part1 = Integer.parseInt(parts[0]);
int part2 = Integer.parseInt(parts[1]);
int part3 = Integer.parseInt(parts[2]);
hex = String.format("#%02X%02X%02X", part1, part2, part3);
System.out.println("hexa"+hex);
// System.out.println("textnames" + ch_list.get(k).toString());
}
detailtext=sb.toString().replaceAll("\\\\n", "\n");
// System.out.println("gh"+sb.toString()+detailtext);
System.out.println("Output: " + sb.toString().replaceAll("\\\\n", "\n"));
searchlist.add(ch);
}
}
Adapter
public class CustomListAdapterCountry extends BaseAdapter {
private AQuery aQuery;
private Activity activity;
private LayoutInflater inflater;
private List<SearchResultModel> movieItems;
private List<String> DispItems;
ImageLoader imageLoader = MyApplication.getInstance().getImageLoader();
public CustomListAdapterCountry(Activity activity, List<SearchResultModel> movieItems,ArrayList<String> DispItems) {
this.activity = activity;
this.movieItems = movieItems;
this.DispItems = DispItems;
aQuery = new AQuery(this.activity);
}
#Override
public int getCount() {
return movieItems.size();
}
#Override
public Object getItem(int location) {
return movieItems.get(location);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_item_searchresult, null);
if (imageLoader == null)
imageLoader = MyApplication.getInstance().getImageLoader();
NetworkImageView iv = (NetworkImageView) convertView
.findViewById(R.id.search_image);
ImageView ratingiv = (ImageView) convertView
.findViewById(R.id.search_rating);
TextView title = (TextView) convertView.findViewById(R.id.search_title);
TextView price = (TextView) convertView.findViewById(R.id.search_price);
TextView dettext = (TextView) convertView.findViewById(R.id.search_detailtext);
// getting movie data for the row
SearchResultModel m = movieItems.get(position);
iv.setImageUrl(m.getSearch_ImgLoc() + m.getSearch_Img(), imageLoader);
aQuery.id(ratingiv).image(m.getSearch_RatingImgPath() + m.getSearch_RatingImg(), true, true, 0, R.mipmap.ic_launcher);
//Log.d("searchimgs", joined);
// title
title.setText(m.getSearch_Name());
price.setText("$"+m.getSearch_Price());
dettext.setText(detailtext);
dettext.setTextColor(Color.parseColor(hex));
return convertView;
}
}
You can try the following method. Format your text as HTML and use it for the display.
private String addColor(String str, String hexColor)
{
String html = "";
if(str.contains("\\\\n"))
{
html = "</br>";
str.replaceAll("\\\\n", "");
}
html = html + "<font color='"+hexColor+"'>"+str+"</font>";
return html;
}
Delete sb.append(disptext); from where it is right now and add sb.append(addColor(disptext, hex)); after hex = String.format("#%02X%02X%02X", part1, part2, part3);
Replace dettext.setText(detailtext); with dettext.setText(Html.fromHtml(detailtext)); and delete dettext.setTextColor(Color.parseColor(hex));.
This should do the job.
Also, you are not using your ArrayLists ch_list and color_list which can also be used for this task.
To change color of prticular words in textview and not change the whole color you will have to use
String noColor = "I like the ";
String redColor = "<font color='#EE0000'>color red</font>";
yourTextView.setText(Html.fromHtml(noColor + redColor));
Just use this and change to your needs. Split your string into smaller strings and appy color to each as need then merge them using Html.fromHtml
i am using volley library for web api call and retrieving json data using for loop and setting that data on custom listview but listview only show two values of that data like if my loop run for 4 times only 2 or 4 numbered value is shown on listview .
Below is my code for fetching values from json data.
details=new ArrayList();
depart_adapter = new Result_dep_adapter(Result.this, android.R.layout.simple_list_item_2, details);
return_adapter = new Result_ret_adapter(Result.this, android.R.layout.simple_list_item_2, details);
departlist.setAdapter(depart_adapter);
returnlist.setAdapter(return_adapter);
try {
for (int k = 0; k < response.length(); k++) {
custom_result=new Custom_Result();
Log.i("response",","+ response.length());
JSONObject obj = response.getJSONObject(k);
JSONObject flight = obj.getJSONObject("Flights");
JSONArray array = flight.getJSONArray("Segments");
for (int i = 0; i < array.length(); i++) {
JSONObject depart = (JSONObject) array.get(i);
String departdate_time = depart.getString("DepartureTime");
String arrivedate_time = depart.getString("ArrivalTime");
Boolean returnflight=depart.getBoolean("IsReturnFlight");
duration=depart.getString("FlightDuration");
Log.i("segments",","+returnflight);
JSONObject Airline=depart.getJSONObject("Airline");
String Airline_code=Airline.getString("Name");
JSONObject Airlinename=depart.getJSONObject("MarketingCarrier");
String name=Airlinename.getString("Name");
Log.i("Airline code", Airline_code);
String[] departtime = departdate_time.split("T");
String[] arrivetime = arrivedate_time.split("T");
if (!returnflight) {
String ddate = departtime[0];
String dtime = departtime[1].substring(0, 5);
Log.i("time", dtime);
String duration = depart.getString("FlightDuration");
String Adate = arrivetime[0];
String Atime = arrivetime[1].substring(0, 5);
custom_result.setDepart_date(ddate);
custom_result.setDepart_time(dtime);
custom_result.setArrive_date(Adate);
custom_result.setArrive_time(Atime);
custom_result.setDep_duration(duration);
custom_result.setDep_Airline_name(name);
custom_result.setDep_Airline_code(Airline_code);
}else{
String Rdate = departtime[0];
String Rtime = departtime[1].substring(0, 5);//return depart time
Log.i("time", Rtime);
String return_duration = depart.getString("FlightDuration");
String ARdate = arrivetime[0];
String ARtime = arrivetime[1].substring(0, 5);
custom_result.setRet_arr_date(ARdate);
custom_result.setRet_arr_time(ARtime);
custom_result.setRet_dep_date(Rdate);
custom_result.setRet_dep_time(Rtime);
custom_result.setRet_duration(return_duration);
custom_result.setRet_Airline_name(name);
custom_result.setRet_Airline_code(Airline_code);
}
}
String stops = flight.getString("Stops");
JSONObject Fare = flight.getJSONObject("Fare");
String published_fare = Fare.getString("PublishedFare");
custom_result.setStops(stops);
custom_result.setPrice(published_fare);
details.add(custom_result);
}
I am new to android please help me ..
Thank you
This my adpater code
private class ViewHolder
{
ImageView Airline_logo;
TextView Airline_code,dep_time,dep_arr_time,dep_stops,dep_duration,
dep_price;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
// custom_result=Custom_Result.getCustom_result();
custom_result=getItem(position);
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if(convertView==null)
{
convertView=inflater.inflate(R.layout.result_listview,null);
holder=new ViewHolder();
holder. Airline_logo=(ImageView) convertView.findViewById(R.id.dep_airlines_image);
holder.Airline_code=(TextView) convertView.findViewById(R.id.dep_airline_code);
holder.dep_time=(TextView) convertView.findViewById(R.id.dep_depart_time);
holder.dep_arr_time=(TextView) convertView.findViewById(R.id.dep_arrive_time);
holder.dep_stops=(TextView) convertView.findViewById(R.id.dep_stops_info);
holder.dep_duration=(TextView) convertView.findViewById(R.id.dep_duration);
holder.dep_price=(TextView) convertView.findViewById(R.id.dep_airline_price);
convertView.setTag(holder);
}else
{
holder=(ViewHolder)convertView.getTag();
}
String imagename=custom_result.getDep_Airline_code().toLowerCase();
String path="drawable/"+imagename;
// String logo=path.toLowerCase();
// String PACKAGE_name=getContext().getPackageName();
int imageresource=context.getResources().getIdentifier(path, null, context.getPackageName());
if(imageresource==0)
{
imageresource=R.drawable.flight_icon;
}
Drawable image=context.getResources().getDrawable(imageresource);
// holder. Airline_logo.setImageBitmap(BitmapFactory.decodeResource(getContext().getResources(),image));
holder.Airline_logo.setImageDrawable(image);
holder.Airline_code.setText(custom_result.getDep_Airline_name());
holder.dep_time.setText(custom_result.getDepart_time());
holder.dep_arr_time.setText(custom_result.getArrive_time());
holder.dep_stops.setText(custom_result.getStops());
holder.dep_duration.setText(custom_result.getDep_duration());
holder.dep_price.setText(custom_result.getPrice());
return convertView;
}
You are using android.R.layout.simple_list_item_2 as the layout for the row.
That layout only has 2 textviews so it cant show more than 2 values.
If you need to show more things you need to create your custom adapter from the BaseAdapter Class.
Then in the getView() method you can draw the row with anything you need.
Hope this helps.