Android: Cannot show more than one image
I have the following main class and thread-TCP Client. The client runs in a loop, receives messages and passes it to main class. In the main class, I parse the message and try to show different images based on the name and value of the message received. Ex: shiftDirection1 name: shiftDirection & value: 1
But I can show only the image corresponding to the first received message and the images corresponding to the remaining received messages cannot be displayed.
Please go through the code below and kindly suggest the mistake/problem and alternative ways.
Thank you for your time and efforts.
Madhu
main class:
public class TCPListen extends Activity implements TCPListener {
private TextView mTitle;
public String recData[] = new String[2];
String PresentGear = "0";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TcpServiceHandler handler = new TcpServiceHandler(this,this);
Thread th = new Thread(handler);
th.start();
}
public String[] callCompleted(String source){
//Log.d("TCP", "Std parser " + source);
//mTitle.setText(source);
//String data[] = new String[2];
//if (source.matches("<MSG><N>.*</N><V>.*</V></MSG>")) {
Document doc = null;
try{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = (Document) db.parse(new ByteArrayInputStream(source.getBytes()));
NodeList n = doc.getElementsByTagName("N");
Node nd = n.item(0);
String msgName = nd.getFirstChild().getNodeValue();
NodeList n1 = doc.getElementsByTagName("V");
Node nd1 = n1.item(0);
String tmpVal = nd1.getFirstChild().getNodeValue();
recData[0] = msgName; 开发者_如何转开发
recData[1] = tmpVal;
if (recData[0].equals("currGear")) PresentGear = recData[1];
Log.d("TCP", "Inside Std parser " + recData[0] + " " + recData[1]);
actionOnData(recData[0], recData[1]);
}
catch(Exception e){
e.printStackTrace();
}
Log.d("TCP", "Just outside Std parser " + recData[0] + " " + recData[1]);
return recData;
//} else Log.d("TCP", "Message in wrong format " + source);
//mTitle.setText("Message in wrong format " + source);
//return data;
}
//Function to display driver messages/images based on individual messages
public void actionOnData(String name, String value) {
String tempName = name;
String tempVal = value;
setContentView(R.layout.image);
ImageView showImage = (ImageView) findViewById(R.id.imageView1);
//Log.d("TCP", "------------>" + tempName + " " + tempVal);
if (tempName.equals("shiftDirection") && tempVal.equals("1")) {
//setContentView(R.layout.image);
//TextView text_top = (TextView) findViewById(R.id.textView1);
//showImage = (ImageView) findViewById(R.id.imageView1);
//text_bottom.setText(Info[1]);
showImage.setImageResource(R.drawable.shift_up);
Log.d("TCP", "1------------>" + showImage);
} else if (tempName.equals("shiftDirection") && tempVal.equals("-1")) {
//setContentView(R.layout.image);
//TextView text_bottom = (TextView) findViewById(R.id.textView2);
//Resources res = getResources();
//Drawable drawable = res.getDrawable(R.drawable.shift_down);
//showImage = (ImageView) findViewById(R.id.imageView1);
//text_bottom.setText(Info[1]);
showImage.setImageResource(R.drawable.shift_down);
} else if (tempName.equals("recomGear") && tempVal != null) {
Log.d("TCP", "3------------>" + tempName + " " + tempVal);
Integer msgValue = Integer.parseInt(recData[1]);
//Integer CurrentGear = (msgValue) - 1;
//Log.d("TCP","in DA Images. Current gear: " + CurrentGear);
//String Gear = Integer.toString(CurrentGear);
setContentView(R.layout.image);
TextView text_top = (TextView) findViewById(R.id.textView1);
TextView text_bottom = (TextView) findViewById(R.id.textView2);
showImage = (ImageView) findViewById(R.id.imageView1);
showImage.setImageResource(R.drawable.shift_up);
text_bottom.setText(PresentGear);
text_top.setText(tempVal);
} else if (tempName.equals("currGear") && tempVal != null) {
Log.d("TCP", "4------------>" + tempName + " " + tempVal);
PresentGear = tempVal;
//Log.d("TCP","in DA Images. Present gear1: " + PresentGear);
setContentView(R.layout.image);
TextView text_bottom = (TextView) findViewById(R.id.textView2);
text_bottom.setText(PresentGear);
} else if (tempName.equals("shiftDirection") && tempVal.equals("0")) {
Log.d("TCP", "5------------>" + tempName + " " + tempVal);
Log.d("TCP","in DA Images. Present gear: " + PresentGear);
setContentView(R.layout.image);
TextView text_bottom = (TextView) findViewById(R.id.textView2);
//TextView text_top = (TextView) findViewById(R.id.textView1);
//text_top.setText("Go on");
text_bottom.setText(PresentGear);
}
}
}
Only the image corresponding to the first if case is displayed. The program control enters the second if loop but does not show the image there.
Interface:
public interface TCPListener {
public String[] callCompleted(String msg);
}
Thread (TCP Client):
public class TcpServiceHandler implements Runnable {
TCPListener _listener;
private Activity _act;
public BufferedReader in;
public TcpServiceHandler(TCPListener listener, Activity act){
_listener = listener;
_act = act;
}
public synchronized void run() {
// TODO Auto-generated method stub
//if(socket==null){
try {
//InetAddress serverAddr = InetAddress.getByName("192.168.178.25");
Socket socket = new Socket("192.168.62.23", 1200, true);
//
//while(true){
try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
final int delay = 100;
final Timer _timer = new Timer();
_timer.scheduleAtFixedRate(new TimerTask() {
public void run(){
String str;
try {
str = in.readLine();
_listener.callCompleted(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, 0, delay);
//final String str = in.readLine();
//this._act.runOnUiThread(new Runnable(){
//public void run() {
// _listener.callCompleted(str);
// }
//});
}
catch(Exception e){
e.printStackTrace();
}
//}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
You might check 2 things:
Your
TcpServiceHandler
is aRunnable
of a thread but there is no loop inrun()
. The _timer you define inside this method might be dead and gone before it's work is done.Are you changing the UI from a background thread? This is not a good idea in general.
Check
AsyncTask
which is a handy tool to run operations in the background.
精彩评论