【问题标题】:How to change/update a item from a Drawer layout?如何从抽屉布局更改/更新项目?
【发布时间】:2016-07-16 00:45:01
【问题描述】:

这是我将物品放入 DrawerList 并查看的地方

tagTitles = getResources().getStringArray(R.array.Tags);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerList = (ListView) findViewById(R.id.left_drawer);
    items = new ArrayList<DrawerItem>();
    items.add(new DrawerItem(tagTitles[0], R.drawable.slider2));
    items.add(new DrawerItem(tagTitles[1], R.drawable.empty));

    items.add(new DrawerItem( String.format(getResources().getString(R.string.levelshow),nBases), R.drawable.empty));
    drawerList.setAdapter(new DrawerListAdapter(this, items));
    drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch (position) {
                case 0://more for here

这是抽屉项目

public class DrawerItem {
private String name;
private int iconId;

public DrawerItem(String name, int iconId) {
    this.name = name;
    this.iconId = iconId;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getIconId() {
    return iconId;
}

public void setIconId(int iconId) {
    this.iconId = iconId;
}
}

抽屉列表适配器

public DrawerListAdapter(Context context, List objets) {
    super(context, 0, objets);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {

        LayoutInflater inflater = (LayoutInflater) parent.getContext().
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list, null);

    }




    ImageView icon = (ImageView) convertView.findViewById(R.id.list_pngg);
    TextView name = (TextView) convertView.findViewById(R.id.list_itemm);
    //FrameLayout foter = (FrameLayout) convertView.findViewById(R.id.fot);
    boolean footer;
    DrawerItem item = (DrawerItem) getItem(position);
    Log.d("help", String.valueOf(name));
    name.setText(item.getName());

    icon.setImageResource(item.getIconId());

情况是我想修改最后一项的文本,因为您可以看到该项目有一个带有占位符的该项目的 string.xml 文本,我需要在需要时更新该字符串。 我没有找到正确的方法,请帮助

edit:XML 的相关部分 //这里有很多视图 android:layout_alignParentBottom="true">

    </RelativeLayout>

</LinearLayout>


<ListView
    android:id="@+id/left_drawer"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"

    android:divider="#4a4a4a"
    android:dividerHeight="0.56dp"
    android:background="#111"
    android:footerDividersEnabled="false"
    android:drawSelectorOnTop="false"
    android:headerDividersEnabled="false"

    android:stackFromBottom="false"

     />

【问题讨论】:

  • 把你的xml也放上去。

标签: android android-layout listview


【解决方案1】:

只有当您在适配器中触发 notifyDataSetChanged() 时,视图才会更新。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多