0

我按照以下教程创建了一个包含图像和文本的列表视图。[ http://www.debugrelease.com/2013/06/24/android-listview-tutorial-with-images-and-text/

它看起来像这样.. [ http://i.stack.imgur.com/l7ukU.png

我稍微修改了一下,只包含三个项目。我想做的是根据我将通过 USB 接收的数据更改列表视图中的文本。如何从我的主要活动中更改数据?

这是我的列表项模型的代码...

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;
    }
}

这是我的数组列表的代码

public class Model extends Globals
{
    public static ArrayList<Item> Items; 
    public static void LoadModel() 
    {
        Items = new ArrayList<Item>();
        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;
    } 
}

这是自定义适配器的代码...

public class ItemAdapter extends ArrayAdapter<String> {

    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;

    }

最后是我的主要活动代码......

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 < 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;
    }
4

1 回答 1

0

当您收到数据时,您需要重新设置adapter并调用notifitydatachange

于 2013-11-13T03:45:05.330 回答