android dictionary int, Imageview - java

I am failing to put objects in dictionary and get them, I want to put imageViews against int but failing without any error
dictionary declared as:
Dictionary<Integer, ImageView> dictPlayerAll = new Dictionary<Integer, ImageView>(){
#Override
public int size() {
// TODO Auto-generated method stub
return 0;
}
#Override
public ImageView remove(Object key) {
// TODO Auto-generated method stub
return null;
}
#Override
public ImageView put(Integer key, ImageView value) {
// TODO Auto-generated method stub
return null;
}
#Override
public Enumeration<Integer> keys() {
// TODO Auto-generated method stub
return null;
}
#Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
#Override
public ImageView get(Object key) {
// TODO Auto-generated method stub
return null;
}
#Override
public Enumeration<ImageView> elements() {
// TODO Auto-generated method stub
return null;
}
};;
here putting values in dictionary
int mTag = iv.getTag();//its my imageview
dictPlayerAll.put(mTag, iv);
but it shows zero size

Your update makes more sense, but the Dictionary class is still obselete. From the documentation:
java.util
Class Dictionary
...
Note: Do not use this class since it is obsolete.
Since you want to create a table indexed by Integers (new Dictionary<Integer, ImageView>()) we should use a SparseArray.
Also when you use setTag() you convert your Integer to an Object and every time you use getTag() you are converting your Integer back from an Object. This works, but if you can use getId() it will be faster.
I recommend this:
SparseArray<ImageView> allPlayers = new SparseArray<ImageView>();
...
allPlayers.put(iv.getId(), iv);

Related

JRHtmlExporter is deprecated now. How to define the path for saving images?

The JRHtmlExporter class is deprecated now (JasperReports 6.x).
I replaced the usage of this class with HtmlExporter. But I can not find equivalent function to replace exporter.setParameter (JRHtmlExporterParameter.IMAGES_URI, imageURI);. I need to set path for storing images for generated report (html file).
My old code:
JRHtmlExporter exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, filedReport);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outputFileName);
exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, "");
exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.TRUE);
String imageURI = "q?srvAction=ReportImage&img="+returnFileName.substring(3).replace("/", "%2F")+"_files"+"%2F";
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,imageURI);
What will be the actual code for JasperReports 6.x for defining path to images?
As we can see from the javadoc the JRHtmlExporterParameter.IMAGES_URI parameter is really deprecated and the HtmlExporterOutput.getImageHandler() method should be used instead of it.
Define the path where images are storing
We can use the implementation of HtmlResourceHandler interface, for example WebHtmlResourceHandler.
The example of using:
JRExporter exporter = new HtmlExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
SimpleHtmlExporterOutput output = new SimpleHtmlExporterOutput(out);
output.setImageHandler(new WebHtmlResourceHandler("/reports/image?image={0}"));
exporter.setExporterOutput(output);
exporter.exportReport();
Define where to save images during export
With help of FileHtmlResourceHandler handler we can set the path for images for generated html
The example of using:
JRExporter exporter = new HtmlExporter();
// output file for generated html report
File file = new File(String.format("./out/%1$s_%2$s.html", report.getTemplateName(), new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())));
ExporterConfiguration configuration = new SimpleHtmlExporterConfiguration();
exporter.setConfiguration(configuration);
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
SimpleHtmlExporterOutput exporterOutput = new SimpleHtmlExporterOutput(file);
// the folder for storing images. It will be subfolder with name starting like generated html and ended with postfix "_images"
File resourcesDir = new File(file.getParent(), file.getName() + "_images");
// argument ({0}) will be replaced with the real image name
String pathPattern = resourcesDir.getName() + "/{0}";
exporterOutput.setImageHandler(new FileHtmlResourceHandler(resourcesDir, pathPattern));
exporter.setExporterOutput(exporterOutput);
exporter.exportReport();
The generated files and folders will be like this:
.. [Folder]
image-test_20170504232649.html [File]
image-test_20170504232649.html_images [Folder]
img_0_0_0.png [File]
Notes:
The sample of using HtmlResourceHandler can be found here
You could try this for Jasper Report 6.9.0, this is the default method
JasperExportManager.exportReportToHtmlFile(jasperPrint, jasperPrint.getName() + ".html");
But if you want to configure image resource handler and any other configuration, try this
HtmlExporter exporter = new HtmlExporter();
File destFile = new File(jasperPrint.getName() + ".html");
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
HtmlReportConfiguration reportConfig = getHTMLReportConfig();
exporter.setConfiguration(reportConfig);
HtmlExporterConfiguration exporterConfig = getHTMLExporterConfig();
exporter.setConfiguration(exporterConfig);
SimpleHtmlExporterOutput output = new SimpleHtmlExporterOutput(destFile);
HtmlResourceHandler imageHandler = new WebHtmlResourceHandler(
request.getContextPath() + "/jasper/image?image={0}");
output.setImageHandler(imageHandler);
exporter.setExporterOutput(output);
exporter.exportReport();
And this is configuration for HTMLExporter and HTMLReport
private HtmlExporterConfiguration getHTMLExporterConfig() {
return new HtmlExporterConfiguration() {
#Override
public Boolean isOverrideHints() {
// TODO Auto-generated method stub
return null;
}
#Override
public Boolean isFlushOutput() {
// TODO Auto-generated method stub
return null;
}
#Override
public String getHtmlHeader() {
// TODO Auto-generated method stub
return null;
}
#Override
public String getHtmlFooter() {
// TODO Auto-generated method stub
return null;
}
// biar tidak ada paging (khusus html)
#Override
public String getBetweenPagesHtml() {
return "";
}
};
}
private HtmlReportConfiguration getHTMLReportConfig() {
return new HtmlReportConfiguration() {
#Override
public Boolean isOverrideHints() {
// TODO Auto-generated method stub
return null;
}
#Override
public Integer getStartPageIndex() {
// TODO Auto-generated method stub
return null;
}
#Override
public JRExportProgressMonitor getProgressMonitor() {
// TODO Auto-generated method stub
return null;
}
#Override
public Integer getPageIndex() {
// TODO Auto-generated method stub
return null;
}
#Override
public Integer getOffsetY() {
// TODO Auto-generated method stub
return null;
}
#Override
public Integer getOffsetX() {
// TODO Auto-generated method stub
return null;
}
#Override
public JRHyperlinkProducerFactory getHyperlinkProducerFactory() {
// TODO Auto-generated method stub
return null;
}
#Override
public ExporterFilter getExporterFilter() {
// TODO Auto-generated method stub
return null;
}
#Override
public Integer getEndPageIndex() {
// TODO Auto-generated method stub
return null;
}
#Override
public Boolean isWrapBreakWord() {
// TODO Auto-generated method stub
return null;
}
#Override
public Boolean isWhitePageBackground() {
// TODO Auto-generated method stub
return null;
}
#Override
public Boolean isUseBackgroundImageToAlign() {
// TODO Auto-generated method stub
return null;
}
// biar gak terlalu banyak white space
#Override
public Boolean isRemoveEmptySpaceBetweenRows() {
return true;
}
#Override
public Boolean isIgnorePageMargins() {
return true;
}
#Override
public Boolean isIgnoreHyperlink() {
// TODO Auto-generated method stub
return null;
}
#Override
public Boolean isEmbeddedSvgUseFonts() {
// TODO Auto-generated method stub
return null;
}
#Override
public Boolean isEmbedImage() {
// TODO Auto-generated method stub
return null;
}
#Override
public Boolean isConvertSvgToImage() {
// TODO Auto-generated method stub
return null;
}
#Override
public Boolean isAccessibleHtml() {
// TODO Auto-generated method stub
return null;
}
#Override
public Float getZoomRatio() {
// TODO Auto-generated method stub
return null;
}
#Override
public HtmlSizeUnitEnum getSizeUnit() {
// TODO Auto-generated method stub
return null;
}
#Override
public HtmlBorderCollapseEnum getBorderCollapseValue() {
// TODO Auto-generated method stub
return null;
}
#Override
public String getBorderCollapse() {
// TODO Auto-generated method stub
return null;
}
};
}
See this http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/export/JRHtmlExporterParameter.html

JCombobox using hasmap/ no point model

Hey i am looking for solving a problem i need to make a model for jcombobox.
I have a :
Map<Integer, Pathes_format> profiles =new HashMap<Integer, Pathes_format>();
I wana to show on jcombobox pathes_format.getname will be displayed at index pathes_format.GetiD i never wrote or touch a abstract class or models.
Here what i made
package subDialogs;
import java.util.HashMap;
import java.util.Map;
import javax.swing.ComboBoxModel;
import javax.swing.event.ListDataListener;
import json.Pathes_format;
public class PatheseModel implements ComboBoxModel {
Map<Integer, Pathes_format> profiles =new HashMap<Integer, Pathes_format>();
int index=-1;
#Override
public int getSize() {
// TODO Auto-generated method stub
return profiles.size();
}
#Override
public Object getElementAt(int index) {
// TODO Auto-generated method stub
return profiles.get(index);
}
#Override
public void addListDataListener(ListDataListener l) {
// TODO Auto-generated method stub
}
#Override
public void removeListDataListener(ListDataListener l) {
// TODO Auto-generated method stub
}
#Override
public void setSelectedItem(Object anItem) {
// TODO Auto-generated method stub
}
#Override
public Object getSelectedItem() {
// TODO Auto-generated method stub
return null;
}
//void addElement(Object obj){
//
//}
void insertElementAt(Object obj, int index) {
profiles.put(index, (Pathes_format) obj);
}
void removeElement(Object obj) {
Pathes_format tmp = profiles.get(obj);
tmp=null;
}
void removeElementAt(int index){
profiles.remove(index);
}
}
I don't know is this correct :/. Mby i should make for a format patches single not for map; and add ability to add map??

How to create generic and reusable code with java

I am on new on java.
what I am trying to do is trying to create reusable generic class.
Here is the my codes.
public interface Operation {
Boolean IsConnected();
Boolean ConnectionOpen();
Boolean ConnectionClose();
}
my main class
public class MyConnectionManager extends MyWifi{
private MyWifi _wf;
public MyConnectionManager(MyWifi wf) {
// TODO Auto-generated constructor stub
_wf= wf;
}
public Boolean IsConnected() {
// TODO Auto-generated method stub
return _wf.IsConnected();
}
public Boolean ConnectionOpen() {
// TODO Auto-generated method stub
return _wf.ConnectionOpen();
}
public Boolean ConnectionClose() {
// TODO Auto-generated method stub
return _wf.ConnectionClose();
}
}
public class MyWifi implements Operation {
public Context _context =null;
#Override
public Boolean IsConnected() {
// TODO Auto-generated method stub
ConnectivityManager connManager = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected()) {
return true;
}
return false;
}
but I want it to be generic and reusable,as the type should be changable .For example ,instead of MyWifi,it could be MyBlueTooth (which implement same interface) and so on.
Here is the what trying to achive.
MyWifi wf = new MyWifi();
//MyBlueTooth bl= new MyBlueTooth ();
MyConnectionManager<MyWifi> mn= new MyConnectionManager<MyWifi>(wf);
mn.IsConnected();
You mean somethin like this?
public class MyConnectionManager<E extends Operation>{
private E _wf;
public MyConnectionManager(E wf) {
// TODO Auto-generated constructor stub
_wf= wf;
}
public Boolean IsConnected() {
// TODO Auto-generated method stub
return _wf.IsConnected();
}
public Boolean ConnectionOpen() {
// TODO Auto-generated method stub
return _wf.ConnectionOpen();
}
public Boolean ConnectionClose() {
// TODO Auto-generated method stub
return _wf.ConnectionClose();
}
}
public class Starter {
public static void main(String[] args) {
MyBlueTooth bt = new MyBlueTooth();
MyWifi wf = new MyWifi();
MyConnectionManager<MyBlueTooth> test = new MyConnectionManager<MyBlueTooth>(bt);
MyConnectionManager<MyWifi> test2 = new MyConnectionManager<MyWifi>(wf);
}
}
Change your MyConnectionManager as follows:
public class MyConnectionManager<T extends Operation> implements Operation {
private T _op;
public MyConnectionManager(T op) {
// TODO Auto-generated constructor stub
_op = op;
}
public Boolean isConnected() {
// TODO Auto-generated method stub
return _op.isConnected();
}
public Boolean connectionOpen() {
// TODO Auto-generated method stub
return _op.connectionOpen();
}
public Boolean connectionClose() {
// TODO Auto-generated method stub
return _op.connectionClose();
}
public T getOperation() {
return _op;
}
}

Getting null value

I am using com.android.internal.telephony API's. In that I called two abstract classes they are Call.java and Connection.java. You can find these classes here http://hi-android.info/src/com/android/internal/telephony/Call.java.html and http://hi-android.info/src/com/android/internal/telephony/Connection.java.html for these created subclasses like
Call myCall = new MyCall();
Connection myConn = new MyConnection();
I need to use getDisconnectCause method from connection class which is an abstract method, I used like this:
myConn = myCall.getEarliestConnection();
if(myConn == null){
System.out.println("myConn is null ******");
}else
{
Connection.DisconnectCause cause = myConn.getDisconnectCause();
System.out.println("value of cause ******"+cause);
}
The subclass of Call.java is:
1. CallManager cm = CallManager.getInstance();
2. Phone.State state;
3.
4. public List<Connection> getConnections() {
5. state = cm.getState();
6. ringingCall = cm.getForegroundCalls();
7. System.out.println("**inside getConnections="+state);
8. System.out.println("**inside getConnections="+ringingCall);
9. if ( ringingCall == null) {
10. System.out.println("**call is null***");
11. return emptyConnections;
12. }
13. else
14. {
15. System.out.println("**call is not null***");
16. return ((Call) ringingCall).getConnections();
17. }
18. #Override
19. public Phone getPhone() {
20. return null;
}
#Override
public void hangup() throws CallStateException {
}
#Override
public boolean isMultiparty() {
return false;
}
public Connection
getEarliestConnection() {
List l;
long time = Long.MAX_VALUE;
Connection c;
Connection earliest = null;
68. l = getConnections();
if (l == null) {
return null;
}else if ( l.size() == 0)
{
return null;
}
for (int i = 0, s = l.size() ; i < s ; i++) {
c = (Connection) l.get(i);
long t;
t = c.getCreateTime();
if (t < time) {
earliest = c;
time = t;
}
}
return earliest;
}
}
AND the Connection.java subclass is:
class MyConnection extends Connection{
#Override
public void cancelPostDial() {
// TODO Auto-generated method stub
}
#Override
public String getAddress() {
// TODO Auto-generated method stub
return null;
}
#Override
public Call getCall() {
// TODO Auto-generated method stub
return null;
}
#Override
public long getConnectTime() {
// TODO Auto-generated method stub
return 0;
}
#Override
public long getCreateTime() {
// TODO Auto-generated method stub
return 0;
}
#Override
public DisconnectCause getDisconnectCause() {
// TODO Auto-generated method stub
return null;
}
#Override
public long getDisconnectTime() {
// TODO Auto-generated method stub
return 0;
}
#Override
public long getDurationMillis() {
// TODO Auto-generated method stub
return 0;
}
#Override
public long getHoldDurationMillis() {
// TODO Auto-generated method stub
return 0;
}
#Override
public int getNumberPresentation() {
// TODO Auto-generated method stub
return 0;
}
#Override
public PostDialState getPostDialState() {
// TODO Auto-generated method stub
return null;
}
#Override
public String getRemainingPostDialString() {
// TODO Auto-generated method stub
return null;
}
#Override
public UUSInfo getUUSInfo() {
// TODO Auto-generated method stub
return null;
}
#Override
public void hangup() throws CallStateException {
// TODO Auto-generated method stub
}
#Override
public boolean isIncoming() {
// TODO Auto-generated method stub
return false;
}
#Override
public void proceedAfterWaitChar() {
// TODO Auto-generated method stub
}
#Override
public void proceedAfterWildChar(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void separate() throws CallStateException {
// TODO Auto-generated method stub
}
}
EDIT 2 : I have edited line number 1 to 17. Plz check this. I am getting java.lang.ClassCastException: java.util.Collections error on Line No: 16 and Line No:68. Can anybody help me to resolve this. And also I am getting only one state of call i.e IDLE always even though the call is not-null . I am getting inside else part. plz help me.
#Override
public List<Connection> getConnections() {
return null;
}
This method on your MyCall class returns null and in your code for MyCall.getEarliestConnection(); it returns null if getConnections() returns null.
Your implementation of the getConnections() method has it returning null.
As you can see from the code inside getEarliestConnection(), if getConnections() returns null, then getEarliestConnection() will also return null.
Here is the relevant code:
#Override
public List<Connection> getConnections() {
return null;
}
...
l = getConnections();
if (l == null) {
return null;
}
I don't know what this code is supposed to do, but it's clear why that is null at that point.
In "MyCall.getEarliestConnection()", it calls it's own "getConnections()" method, which just returns null.

How to pass data between two activities

I have a NetworkList class which has a arraylist of type string. I am using parcelable interface in it.but i am getting only the first element of arraylist in my new activity.
how can i get all the elements?
here is NetworkList class
public class NetworkList implements Parcelable{
private ArrayList<String> name=new ArrayList<String>();
private ArrayList<String> id=new ArrayList<String>();
#SuppressWarnings("unchecked")
public NetworkList(Parcel in) {
// TODO Auto-generated constructor stub
name=in.readArrayList(null);
id=in.readArrayList(null);
}
public NetworkList() {
// TODO Auto-generated constructor stub
}
public void setName(String tempValue) {
// TODO Auto-generated method stub
this.name.add(tempValue);
}
public void setid(String tempValue) {
// TODO Auto-generated method stub
this.id.add(tempValue);
}
#Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeList(name);
dest.writeList(id);
}
public ArrayList<String> getname() {
return name;
}
public static final Creator<NetworkList> CREATOR = new Parcelable.Creator<NetworkList>() {
public NetworkList createFromParcel(Parcel in) {
return new NetworkList(in);
}
public NetworkList[] newArray(int size) {
return new NetworkList[size];
}
};
}
here is how where i pass it
Intent(AllNetworkActivity.this,WorkActivity.class);
intent.putExtra("name",network);
startActivity(intent);
}});
}
here is how i retrieve it
Bundle extras=getIntent().getExtras();
NetworkList network=(NetworkList)extras.getParcelable("name");
String name[]=new String[network.getname().size()];
for(int i=0;i<network.getname().size();i++){
name[i]=network.getname().get(i);
}
setListAdapter(new ArrayAdapter<String>(this,R.layout.work,name));
hope u are able to help me know
You should also look into Application class in Android. You can define global variables in there which can be accessed by all activities. However Bundle is the most popular and right method for sending data between activities.
Try using appropriate methods to serialize List<String>:
public NetworkList(Parcel in) {
in.readStringList(name);
in.readStringList(id);
}
public void writeToParcel(Parcel dest, int flags) {
// log the number of elements - check that this is actually what you expect it to be
// use only during development
Log.i("NetworkList"," number of elements = "+name.size())
dest.writeStringList(name);
dest.writeStringList(id);
}
first
Intent Jan = new Intent(Months.this,Holiday.class);
Jan.putExtra("ListCount", "Jan");
startActivity(Jan);
then
Bundle extras = getIntent().getExtras();
String data = extras.getString("ListCount");
if(data.equals("Jan")){
TextView txtMonth = (TextView)findViewById(R.id.txtMonth);
txtMonth.setText("January");
TextView txtDetail = (TextView)findViewById(R.id.txtDetail);
txtDetail.setText(R.string.JanHol);
}
check this post I wrote. Hope this will help you.

Categories

Resources