java.lang.String cannot be cast to android.widget.ListAdapter - android-listview

I got this error trying to get all items with specific object and store it to my Listview.
Following the codes:
SimpleDateFormat formatdate = new SimpleDateFormat("MMM. dd, yyyy");
String selecdate = formatdate.format(date);
if (eventDates.contains(selecdate)) {
for (int i = 0; i < eventsList.size(); i++) {
HashMap<String, Object> obj = (HashMap<String, Object>) adapter.getItem(i);
ListAdapter list = (ListAdapter) obj.get("eventtitle");
myList.setAdapter(list);
}
}
else {
myList.setAdapter(null);
}

java.lang.String cannot be cast to android.widget.ListAdapter
This line is saying that your obj.get("eventtitle"); is returning String as a value not a ListAdapter
As docs says
public abstract Object getItem (int position)
Get the data item associated with the specified
position in the data set.
So might be your dataSet is of String rather than ListAdapter

Related

Facing error message when we defined the webdriver factory in groovy class

Facing error message when we defined the web driver factory in groovy.
Are there any errors in my code?
Code Snippet:
private static WebDriver driver=null;
#Keyword
public static void Customized_Start_Time()
{
driver = DriverFactory.getWebDriver();
Date date = new Date();
Date yesterday = date.previous()
SimpleDateFormat customDate;
customDate = new SimpleDateFormat("d MMM yy"); // Date format could be 03-Sep-20
String dateOutput = customDate.format(yesterday);
System.out.println(dateOutput);
//Date Format is 03-Sep-20
String[] dateParts=dateOutput.split(" ")
String res=dateParts[0]
println dateParts[0]
String beforeXpath="//table[#uitestid='gwt-debug-customFromDatePicker']/tbody/tr[2]/td/table[#class='datePickerDays']/tbody/tr[";
String AfterXpath="]/td[";
String LastXpath="]/div"
boolean flag=false;
for(int rowNum=2; rowNum<=7;rowNum++)
{
for(int colNum=2;colNum<=7;colNum++)
{
String dateval=driver.findElement(By.xpath("beforeXpath+rowNum+AfterXpath+colNum+LastXpath")).getText()
//String dateval =WebUI.getText(findTestObject('beforeXpath+rowNum+AfterXpath+colNum+LastXpath'), FailureHandling.OPTIONAL)
println (dateval)
if (dateval.equals(res))
{
driver.findElement(By.xpath("beforeXpath+rowNum+AfterXpath+colNum+LastXpath")).click()
flag=true;
break;
}
}
if(flag)
{
break;
}
}
You need to import Selenium's By library.
Add the following to the top of your script (where the other imports are):
import org.openqa.selenium.By
Or, you can automatically add the missing imports by pressing Ctrl + Shift + O.

QuickfixJ create message from xml string

QuickFixJ Message class has method toXML() which converts message into xml string.
Is there any way I can create message object from the XML string?
I need the reverse of toXML() i.e. I want to create Message from xml.
There's nothing like that built in. There doesn't really need to be, as there normally wouldn't be a use-case for it.
I've written a class which does it. The order of the tags might be different from the input message (but the FIX spec makes no guarantees about tag order, except within groups) because the XML exporter sorts by tag number, and so the original tag order is lost.
It only works on a single message in an XML file, but could easily be adapted to work on multiple messages.
You can use the standard MessageUtils.parse to create a Message from the resultant string.
Let me know if you have any problems.
class XmlMessage
{
private final String xml;
private final String delimiter;
XmlMessage(final String xml, final String delimiter)
{
this.xml = xml;
this.delimiter = delimiter;
}
public String toFixMessage() throws IOException, SAXException, ParserConfigurationException
{
final Document doc = DocumentBuilderFactory.newInstance()
.newDocumentBuilder()
.parse(new ByteArrayInputStream(xml.getBytes()));
final StringBuilder messageBuilder = new StringBuilder();
build(messageBuilder, doc, "header");
build(messageBuilder, doc, "body");
build(messageBuilder, doc, "trailer");
return messageBuilder.toString();
}
private void build(final StringBuilder messageBuilder, final Document doc, final String section)
{
final NodeList sectionRoot = doc.getElementsByTagName(section);
final NodeList sectionChildren = sectionRoot.item(0).getChildNodes();
build(messageBuilder, sectionChildren);
}
private void build(final StringBuilder messageBuilder, final NodeList nodeList)
{
final Set<String> numInGroupTags = getNumInGroupTags(nodeList);
for (int i = 0; i < nodeList.getLength(); i++)
{
final Node node = nodeList.item(i);
if (node.getNodeName().equals("field") && !numInGroupTags.contains(getTagNumber(node)))
{
messageBuilder.append(getTagNumber(node))
.append('=')
.append(node.getTextContent())
.append(delimiter);
}
else if (node.getNodeName().equals("groups"))
{
final NodeList groupElems = node.getChildNodes();
messageBuilder.append(getTagNumber(node))
.append('=')
.append(getGroupCount(groupElems))
.append(delimiter);
for (int j = 0; j < groupElems.getLength(); j++)
{
build(messageBuilder, groupElems.item(j).getChildNodes());
}
}
}
}
private Set<String> getNumInGroupTags(final NodeList nodeList)
{
final Set<String> numInGroupTags = new HashSet<>();
for (int i = 0; i < nodeList.getLength(); i++)
{
if (nodeList.item(i).getNodeName().equals("groups"))
{
numInGroupTags.add(getTagNumber(nodeList.item(i)));
}
}
return numInGroupTags;
}
private String getTagNumber(final Node node)
{
return node.getAttributes().getNamedItem("tag").getTextContent();
}
private int getGroupCount(final NodeList groupRoot)
{
int count = 0;
for (int j = 0; j < groupRoot.getLength(); j++)
{
if (groupRoot.item(j).getNodeName().equals("group")) count++;
}
return count;
}
}

How to get table metadata from camel-sql component

I'm looking for a way to get all the column meta data for the given table name using camel-sql component.
Though it uses spring-jdbc behind the scenes i do not see a way to get the ResultSetMetaData.
I couldn't find a direct way to get the column details from camel-sql component, For now managed to get the information using spring jdbc template and data source.
public List<String> getColumnNamesFromTable(final TableData tableData) throws MetaDataAccessException {
final List<String> columnNames = new ArrayList<String>();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
StringBuilder query = new StringBuilder();
query.append("SELECT * FROM ").append(SINGLE_BLANK_SPACE);
query.append(tableData.getSchemaName());
query.append(tableData.getTableName()).append(SINGLE_BLANK_SPACE);
query.append("WHERE rownum < 0");
jdbcTemplate.query(query.toString(), new ResultSetExtractor<Integer>() {
#Override
public Integer extractData(ResultSet rs) throws SQLException, DataAccessException {
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
columnNames.add(rsmd.getColumnName(i).toUpperCase());
}
return columnCount;
}
});
return columnNames;
}

Passing database data to another activity

how can I pass an array of strings and integers which contains the database data into another activity...I don't know how to put the database data on the string array
You can use this method to retrieve data from the table
public List<String[]> getData(String query) throws SQLException{
Cursor c = mdb.rawQuery(query, null);
c.moveToFirst();
List<String[]> data = new ArrayList<String[]>();
if (c != null) {
for (int i = 0; i < c.getCount(); i++) { // iterate rows
String[] row = new String[c.getColumnCount()];
for(int j = 0; j < c.getColumnCount(); j++){//iterate columns
row[j] = c.getString(j);
}
data.add(row);
c.moveToNext();
}
}
c.close();
Log.i("getData", data.size() + " rows returned");
return data;
}
Each row of the result will be contained in each String[] of the list.
Say, intentData is a String[] and you want to pass the data to the next activity.For this, you have to add the data to the Intent like this
Intent intent = new Intent(CurrentAct.this, NextAct.class);
intent.putExtra("dbData", intentData);
startActivity(intent);
You can retrieve the data from NextAct's onCreate() method like this :
String[] passedData = getIntent().getExtras().getStringArray("dbData");

How can I avoid converting an empty HashMap to null in morphia?

We are using org.mongodb.morphia to convert objects BasicDBObjects before persistence. One issue encountered is that in some cases the object to convert contains an empty HashMap whose size is 0, after conversion, the HashMap is converted to null. So NullPointerException throw in later accessing. I want to ask experts for help, Is there any way to avoid this? I mean, after conversion, it's still an HashMap with size 0.
Part of the class to be converted:
public class ProjectServiceAdapterConfig {
#NotNull
private String serviceAdapterId;
#NotNull
private String projectId;
#Embedded
#Flatten
private Map<String, Map<String, String>> connections = new HashMap<>();
//...... setter and getter skipped here
}
code for conversion:
// create a mapper with default MapperOptions
private Mapper createMapper() {
return new Mapper();
}
ReplaceableItem objectToItem(final ProjectServiceAdapterConfig obj) {
final Mapper mapper = createMapper();
final MappedClass mc = mapper.getMappedClass(obj.getClass());
final Map<String, Object> map = mapper.toDBObject(obj).toMap();
}
the obj is created in other place. After some debug, I found that, the obj contains an empty Map(following data copied from IntelliJ IDEA debugger):
connections = {java.util.LinkedHashMap#8890} size = 1
[0] = {java.util.LinkedHashMap$Entry#8894}"accounts" -> size = 0
key: java.lang.String = {java.lang.String#8895}"accounts"
value: java.util.LinkedHashMap = {java.util.LinkedHashMap#8896} size = 0
and the one after converted:
[2] = {java.util.LinkedHashMap$Entry#8910}"connections" -> size = 1
key: java.lang.String = {java.lang.String#8911}"connections"
value: com.mongodb.BasicDBObject = {com.mongodb.BasicDBObject#8912} size = 1
[0] = {java.util.LinkedHashMap$Entry#8923}"accounts" -> null
key: java.lang.String = {java.lang.String#8895}"accounts"
value: = null
As you can see , it's converted to null which we try to avoid.
Thanks
Before you call morphia.mapPackage(), do this:
morphia.getMapper().getOptions().storeEmpties = true;
That should map back probably to an empty map for you.
I assume I cannot avoid it without customizing the MapOfValuesConverter. See from the source code that the empty map will be always converted to null:
#Override
public Object encode(Object value, MappedField mf) {
if (value == null)
return null
Map<Object, Object> map = (Map<Object, Object>) value;
if ((map != null) && (map.size() > 0)) {
Map mapForDb = new HashMap();
for (Map.Entry<Object, Object> entry : map.entrySet()) {
String strKey = converters.encode(entry.getKey()).toString();
mapForDb.put(strKey, converters.encode(entry.getValue()));
}
return mapForDb;
}
return null;
}
In case morphia.getMapper().getOptions().setStoreEmpties(true); doesn't work for you another solution would be to use the #PostLoad annotation to check whether you have a null collection and create an empty one if necessary.
import java.util.*;
import org.mongodb.morphia.annotations.*;
import org.bson.types.ObjectId;
#Entity
public class Model {
#Id
private ObjectId id;
private Map<String, String> map;
protected Model() {}
public Model(HashMap<String, String> map) {
super();
setMap(map);
}
public void setMap(HashMap<String, String> map) {
this.map = map;
checkForNullMap();
}
#PostLoad
private void checkForNullMap() {
if (map == null) {
map = new HashMap<String, String>();
}
}
}