Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating global variables in listview (Android)
    primarykey
    data
    text
    <p>I have an issue that I am trying to resolve. I have created a list with three items in it that also allows you to add images to the list by following this tutorial... </p> <p>Here, in order, is what I did. 1. I began by creating a class called 'Item' to serve as a model for the list items.</p> <pre><code>public class Item { public int Id; public String IconFile; public String Name; public Item(int id, String iconFile, String name { Id = id; IconFile = iconFile; Name = name; } } </code></pre> <p>.2. I then created a class called 'Model' to provide an ArrayList for my items. However, instead of simply putting text in the list names I included global variables.</p> <p><a href="http://www.debugrelease.com/2013/06/24/android-listview-tutorial-with-images-and-text/" rel="nofollow">http://www.debugrelease.com/2013/06/24/android-listview-tutorial-with-images-and-text/</a></p> <pre><code>public class Model extends Globals { public static ArrayList&lt;Item&gt; Items; public static void LoadModel() { Items = new ArrayList&lt;Item&gt;(); Items.add(new Item(1, "temperature_icon.png", "Temperature/Humidity" + temperature + "°C / " + humidity + "%")); Items.add(new Item(2, "gas_icon.png", "LPG" + lpg +" ppm")); Items.add(new Item(3, "alcohol_icon.png", "Alcohol" + alcohol + " ppm")); } public static Item GetbyId(int id) { for(Item item : Items) { if (item.Id == id) { return item; } } return null; } } </code></pre> <p>.3. (Extra step) I added a global class to have access to the global variables.</p> <pre><code>public abstract class Globals extends Activity { static int temperature; static int humidity; static int lpg; static int alcohol; } </code></pre> <p>.4. Add listview widget to main activity layout.</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/listView" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>.5. Add custom layout since I am displaying more than text.</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" android:minHeight="64dp"&gt; &lt;ImageView android:id="@+id/imageView" android:layout_width="32dp" android:layout_height="32dp" android:layout_alignParentLeft="true" android:layout_marginLeft="9dp" android:layout_alignParentTop="true"/&gt; &lt;TextView android:layout_alignBottom="@+id/imageView" android:layout_width="97dp" android:layout_height="32dp" android:id="@+id/textView" android:layout_alignParentLeft="true" android:layout_marginLeft="66dp" android:layout_alignParentRight="true" android:gravity="center_vertical"/&gt; &lt;/RelativeLayout&gt; </code></pre> <p>.6. Create custom adapter to display data</p> <pre><code>public class ItemAdapter extends ArrayAdapter&lt;String&gt; { private final Context context; private final String[] Ids; private final int rowResourceId; public ItemAdapter(Context context, int textViewResourceId, String[] objects) { super(context, textViewResourceId, objects); this.context = context; this.Ids = objects; this.rowResourceId = textViewResourceId; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View rowView = inflater.inflate(rowResourceId, parent, false); ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView); TextView textView = (TextView) rowView.findViewById(R.id.textView); int id = Integer.parseInt(Ids[position]); String imageFile = Model.GetbyId(id).IconFile; textView.setText(Model.GetbyId(id).Name); // get input stream InputStream ims = null; try { ims = context.getAssets().open(imageFile); } catch (IOException e) { e.printStackTrace(); } // load image as Drawable Drawable d = Drawable.createFromStream(ims, null); // set image to ImageView imageView.setImageDrawable(d); return rowView; } } </code></pre> <p>.7. Load model, find listview and assign adapter in the main activity.</p> <pre><code>public class MainActivity extends Globals { ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Model.LoadModel(); listView = (ListView) findViewById(R.id.listView); String[] ids = new String[Model.Items.size()]; for (int i= 0; i &lt; ids.length; i++) { ids[i] = Integer.toString(i+1); } ItemAdapter adapter = new ItemAdapter(this,R.layout.row, ids); listView.setAdapter(adapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } </code></pre> <p>.Okay, so now that you hopefully understand what I have done, I can explain the problem. I would like to update the global variables in the listview based on data that I will be receiving over USB. However, I cannot seem to figure out how to do this. I tried the following code within the main activity but it did not update the data...</p> <pre><code>temperature = 5; adapter.notifyDataSetChanged(); </code></pre> <p>Any help would be greatly appreciated. Thank you for your time.</p> <h2>Update</h2> <p>So I have created a new ArrayList with the new data that I want to display in the ListView. I then populate the ListView, set the ListView to the adapter and then call notifyDataSetChanged. However, my code still does not seem to update. Any advice is greatly welcomed!</p> <pre><code>public class MainActivity extends Activity { public static int temperature; public static int humidity; public static int lpg; public static int alcohol; ListView listView; ListView listView1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Model.LoadModel(); listView = (ListView) findViewById(R.id.listView); String[] ids = new String[Model.Items.size()]; for (int i= 0; i &lt; ids.length; i++) { ids[i] = Integer.toString(i+1); } ItemAdapter adapter = new ItemAdapter(this,R.layout.row, ids); listView.setAdapter(adapter); //Simulate updated data that will come from USB temperature = 1; humidity = 60; lpg = 5000; alcohol = 500; //Create new ArrayList with updated data ArrayList&lt;Item&gt; Items1; Items1 = new ArrayList&lt;Item&gt;(); Items1.add(new Item(1, "temperature_icon.png", "Temperature/Humidity" + temperature + "°C / " + humidity + "%")); Items1.add(new Item(2, "gas_icon.png", "LPG" + lpg +" ppm")); Items1.add(new Item(3, "alcohol_icon.png", "Alcohol" + alcohol + " ppm")); //Populate ListView listView1 = (ListView) findViewById(R.id.listView); String[] ids1 = new String[Items1.size()]; for (int i= 0; i &lt; ids1.length; i++) { ids1[i] = Integer.toString(i+1); } //Bind new ListView to Adapter listView1.setAdapter(adapter); adapter.notifyDataSetChanged(); listView1.invalidateViews(); } </code></pre> <h2>Update # 2</h2> <p>I have now both created a new listview to display the updated data, as well as created a new adapter to which I can attach the listview. However, when I call notifyDataSetChanged(); the new data is not displayed; I still see the original data.</p> <pre><code>public class MainActivity extends Activity { public static int temperature; public static int humidity; public static int lpg; public static int alcohol; ListView listView; ListView listView_new; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Model.LoadModel(); listView = (ListView) findViewById(R.id.listView); String[] ids = new String[Model.Items.size()]; for (int i= 0; i &lt; ids.length; i++) { ids[i] = Integer.toString(i+1); } ItemAdapter adapter = new ItemAdapter(this,R.layout.row, ids); listView.setAdapter(adapter); //--Simulate new data--// temperature = 1; humidity = 60; lpg = 5000; alcohol = 500; //Create new ArrayList with updated data ArrayList&lt;Item&gt; Items_new; Items_new = new ArrayList&lt;Item&gt;(); Items_new.add(new Item(1, "temperature_icon.png", "Temperature/Humidity " + temperature + "°C / " + humidity + "%")); Items_new.add(new Item(2, "gas_icon.png", "LPG " + lpg +" ppm")); Items_new.add(new Item(3, "alcohol_icon.png", "Alcohol " + alcohol + " ppm")); //Populate ListView listView_new = (ListView) findViewById(R.id.listView); String[] ids_new = new String[Items_new.size()]; for (int j= 0; j &lt; ids_new.length; j++) { ids_new[j] = Integer.toString(j+1); } //Create new adapter ItemAdapter adapter_new = new ItemAdapter (this,R.layout.row, ids_new); //Bind new ListView to Adapter listView_new.setAdapter(adapter_new); //listView_new.invalidateViews(); adapter_new.notifyDataSetChanged(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload