Well, this is it gets interesting. This part of code is almost completely from list14.java from sdk, and it is working. I am disconnecting connection to server to simulate network error, which should appear in textview (to let my user know that last update try was at time, but unsuccessful). I am getting error at
stext.setText(sdf.format(cal.getTime()).toString()+ " chyba"); where it ends on null pointer exception. I tried to add: setContentView(R.layout.main); which required to add a listview named android:list. Now...it works same way as before, it ends on same null pointer exception :) On Oct 3, 11:00 pm, Streets Of Boston <flyingdutc...@gmail.com> wrote: > It's quite a bit of code, but one thing's missing that you have to do: > > In your onCreate, you're not calling setContentView(...). You have to > do this to show at least something :-) > > On Oct 3, 4:39 pm, vorcigernix <vorciger...@gmail.com> wrote: > > > Hello, > > > I didn't found clear answer for why my code does not work. I assume it > > is because I am trying to update textview from runnable. Anybody know > > some workaround? Code isn't clean off course, I am happy to get that > > working so far as I am totally new in java. > > > public class MainScreenActivity extends ListActivity { > > private Handler mHandler; > > private String url = "http://sql08.globus.cz/turnovers/ > > turnoversuma.ashx"; > > static JSONArray valArray2; > > public static final String DATE_FORMAT_NOW = "HH:mm:ss"; > > TextView stext; > > > private static class EfficientAdapter extends BaseAdapter { > > private LayoutInflater mInflater; > > private Bitmap mIcon1; > > private Bitmap mIcon2; > > > public EfficientAdapter(Context context) { > > try{ > > // Cache the LayoutInflate to avoid asking for a new one > > each time. > > mInflater = LayoutInflater.from(context); > > > // Icons bound to the rows. > > mIcon1 = BitmapFactory.decodeResource(context.getResources > > (), R.drawable.green); > > mIcon2 = BitmapFactory.decodeResource(context.getResources > > (), R.drawable.yellow); > > } > > catch (Exception e) { > > Log.e("ERROR", "ERROR IN CODE:"+e.toString()); > > } > > > } > > > /** > > * The number of items in the list is determined by the number > > of speeches > > * in our array. > > * > > * @see android.widget.ListAdapter#getCount() > > */ > > public int getCount() { > > > try { > > return valArray2.length(); > > } catch (Exception e) { > > // TODO Auto-generated catch block > > e.printStackTrace(); > > return 0; > > } > > } > > > /** > > * Since the data comes from an array, just returning the > > index is > > * sufficent to get at the data. If we were using a more > > complex data > > * structure, we would return whatever object represents one > > row in the > > * list. > > * > > * @see android.widget.ListAdapter#getItem(int) > > */ > > public JSONArray getItem(int position) { > > JSONArray retobj = null; > > try { > > retobj = valArray2.getJSONArray(position); > > } catch (JSONException e) { > > // TODO Auto-generated catch block > > e.printStackTrace(); > > } > > return retobj; > > } > > > /** > > * Use the array index as a unique id. > > * > > * @see android.widget.ListAdapter#getItemId(int) > > */ > > public long getItemId(int position) { > > return position; > > } > > > /** > > * Make a view to hold each row. > > * > > * @see android.widget.ListAdapter#getView(int, > > android.view.View, > > * android.view.ViewGroup) > > */ > > public View getView(int position, View convertView, ViewGroup > > parent) { > > // A ViewHolder keeps references to children views to > > avoid unneccessary calls > > // to findViewById() on each row. > > ViewHolder holder; > > > // When convertView is not null, we can reuse it directly, > > there is no need > > // to reinflate it. We only inflate a new View when the > > convertView supplied > > // by ListView is null. > > holder = new ViewHolder(); > > > try { > > if (convertView == null) { > > convertView = > > mInflater.inflate(R.layout.list_item_icon_text, > > null); > > > // Creates a ViewHolder and store > > references to the two > > children views > > // we want to bind data to. > > > holder.text = (TextView) > > convertView.findViewById(R.id.text); > > holder.text2 = (TextView) > > convertView.findViewById > > (R.id.text2); > > holder.icon = (ImageView) > > convertView.findViewById(R.id.icon); > > > convertView.setTag(holder); > > } else { > > // Get the ViewHolder back to get fast > > access to the TextView > > // and the ImageView. > > holder = (ViewHolder) > > convertView.getTag(); > > } > > } catch (Exception e1) { > > // TODO Auto-generated catch block > > e1.printStackTrace(); > > } > > > // Bind the data efficiently with the holder. > > > try { > > String rowString = "[" + valArray2.getString(position) + > > "]"; > > JSONArray rowArr = new JSONArray(rowString); > > JSONObject rowobj = rowArr.getJSONObject(0); > > > holder.text.setText(rowobj.getString("BranchName")); > > > > holder.text2.setText(rowobj.getString("BranchValue")); > > holder.icon.setImageBitmap((position & 1) > > == 1 ? mIcon1 : mIcon2); > > } catch (JSONException e) { > > // TODO Auto-generated catch block > > e.printStackTrace(); > > } > > > notifyDataSetChanged(); > > > return convertView; > > > } > > > static class ViewHolder { > > TextView text; > > TextView text2; > > ImageView icon; > > } > > } > > > @Override > > public void onCreate(Bundle savedInstanceState) { > > super.onCreate(savedInstanceState); > > stext = (TextView)findViewById(R.id.statustext); > > mHandler = new Handler(); > > checkUpdate.start(); > > > } > > > private Runnable showUpdate = new Runnable(){ > > public void run(){ > > Calendar cal = Calendar.getInstance(); > > SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); > > > try { > > setListAdapter(new > > EfficientAdapter(MainScreenActivity.this)); > > > > stext.setText(sdf.format(cal.getTime()).toString()+ " ok"); > > > } catch (Exception e) { > > > > stext.setText(sdf.format(cal.getTime()).toString()+ " chyba"); > > e.printStackTrace(); > > > } > > > } > > }; > > private static String convertStreamToString(InputStream is) { > > /* > > * To convert the InputStream to String we use the > > BufferedReader.readLine() > > * method. We iterate until the BufferedReader return null > > which means > > * there's no more data to read. Each line will appended to a > > StringBuilder > > * and returned as String. > > */ > > BufferedReader reader = new BufferedReader(new > > InputStreamReader(is)); > > StringBuilder sb = new StringBuilder(); > > > String line = null; > > try { > > while ((line = reader.readLine()) != null) { > > sb.append(line + "\n"); > > } > > } catch (IOException e) { > > e.printStackTrace(); > > } finally { > > try { > > is.close(); > > } catch (IOException e) { > > e.printStackTrace(); > > } > > } > > return sb.toString(); > > } > > > private Thread checkUpdate = new Thread() { > > > > ... > > read more » --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---