Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating data in custom data in custom listview (Android)
    primarykey
    data
    text
    <p>I followed the following tutorial to create a listview that has both an image and text in it. [<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> <p>It looks like this.. [<a href="http://i.stack.imgur.com/l7ukU.png" rel="nofollow">http://i.stack.imgur.com/l7ukU.png</a></p> <p>I modified it slighlty to contain only three items. What I would like to do is change the text in the listview based on data that I will be receiving over USB. How do I go about changing the data from within my main activity?</p> <p>Here is the code for my 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>Here is the code for my arraylist</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, "alarm.png", "Alarm")); Items.add(new Item(2, "calc.png", "Calculator")); Items.add(new Item(3, "play.png", "Play")); } public static Item GetbyId(int id) { for(Item item : Items) { if (item.Id == id) { return item; } } return null; } } </code></pre> <p>Here is the code for the custom adapter...</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>And lastly my main activity code...</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; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_settings: Toast.makeText(this, "Exiting", Toast.LENGTH_SHORT) .show(); //finish(); break; default: break; } return true; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
    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