android - 如何正确实现 BaseAdapter 的 ADD 方法 - Android

标签 android android-listview baseadapter

我有一个自定义的 BaseAdapter,它创建一个包含评论、用户名和数字的 ListView。问题是我不知道如何正确实现 add 方法以便更新 BaseAdapter。这是我的 Current BaseAdapter。我的添加方法是空的,因为我不知道从哪里开始。

class CreateCommentLists extends BaseAdapter{
  Context ctx_invitation;
  String[] listComments;
  String[] listNumbers;
  String[] listUsernames;

  public CreateCommentLists(String[] comments, String[] usernames, String[] numbers, DashboardActivity context)
  {
    super();
    ctx_invitation = context;
    listComments = comments;
    listNumbers = usernames;
    listUsernames = numbers;
  }

  @Override
  public int getCount() {
    if(null == listComments)
    {
      return 0;
    }   

    // TODO Auto-generated method stub
    return listComments.length;
  }

  @Override
  public Object getItem(int position) {
    // TODO Auto-generated method stub
    return listComments[position];
  }

  @Override
  public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
  }

  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View v = null;
    try
    {
      String inflater = Context.LAYOUT_INFLATER_SERVICE;
      LayoutInflater li = (LayoutInflater)ctx_invitation.getSystemService(inflater);
      v = li.inflate(R.layout.list_item, null);

      TextView commentView = (TextView)v.findViewById(R.id.listComment);
      TextView NumbersView = (TextView)v.findViewById(R.id.listNumber);
      TextView usernamesView = (TextView)v.findViewById(R.id.listPostedBy);
      Button usernameButton = (Button)v.findViewById(R.id.listUsernameButton);
      Button numberButton = (Button)v.findViewById(R.id.listNumberButton);

      commentView.setText(listComments[position]);
      NumbersView.setText(listNumbers[position]);
      usernamesView.setText(listUsernames[position]);
      usernameButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
          Intent i = new Intent(getApplicationContext(), ProfileActivity.class);
          i.putExtra("usernameOfProfile",listUsernames[position]);
          startActivity(i);
          finish();
        }
      });

      numberButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
          Intent i = new Intent(getApplicationContext(), ProfileActivity.class);
          i.putExtra("NumberProfile",listNumbers[position]);
          startActivity(i);
          finish();
        }
      });   
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    return v;
  }

  public void add(String[] comments, String[] usernames,
                  String[] numbers) {
    listComments = comments;
    listNumbers = usernames;
    listUsernames = numbers;
  }

  public int getCount1() {
    if(null == listComments)
    {
      return 0;
    }   

    // TODO Auto-generated method stub
    return listComments.length;
  }

  public Object getItem1(int position) {
    // TODO Auto-generated method stub
    return listComments[position];
  }

  public long getItemId1(int position) {
    // TODO Auto-generated method stub
    return 0;
  }

  public View getView1(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View v = null;
    try
    {
      String inflater = Context.LAYOUT_INFLATER_SERVICE;
      LayoutInflater li = (LayoutInflater)ctx_invitation.getSystemService(inflater);
      v = li.inflate(R.layout.list_item, null);


      TextView commentView = (TextView)v.findViewById(R.id.listComment);
      TextView NumbersView = (TextView)v.findViewById(R.id.listNumber);
      TextView usernamesView = (TextView)v.findViewById(R.id.listPostedBy);
      Button usernameButton = (Button)v.findViewById(R.id.listUsernameButton);
      Button numberButton = (Button)v.findViewById(R.id.listNumberButton);

      commentView.setText(listComments[position]);
      NumbersView.setText(listNumbers[position]);
      usernamesView.setText(listUsernames[position]);

      usernameButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
          Intent i = new Intent(getApplicationContext(), ProfileActivity.class);
          i.putExtra("usernameOfProfile",listUsernames[position]);
          startActivity(i);
          finish();
        }
      });

      numberButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
          Intent i = new Intent(getApplicationContext(), ProfileActivity.class);
          i.putExtra("NumberProfile",listNumbers[position]);
          startActivity(i);
          finish();
        }
      });
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    return v;
  }
}  

我认为这就是调用 add 方法的方式。

mycmlist.add(评论、用户名、号码); mycmlist.notifyDataSetChanged();

最佳答案

使用列表而不是数组来动态添加项目。在您的适配器类中创建一个方法。

例子:

void addToList(List<String> list)
{
    this.list.addAll(list);
    this.notifyDataSetChanged();
}

关于android - 如何正确实现 BaseAdapter 的 ADD 方法 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18370726/

相关文章:

Android:帮助设计人员经理

android - 在 ListView 上双指缩放

android - 如何使用点击列表项将值从一个 Activity 传递到另一个 Activity

android - 如何删除 AlertDialog 中的分隔线

java - 数组副本,第二个没有最后一个位置

android - 在 Kotlin 中打开新 fragment 时如何对当前 fragment 应用更改?

java - 如何在 RecyclerView.ViewHolder 中引用 fragment ?请查看详情

Android convertView,用还是不用?

android - 使用自定义 BaseAdapter 检查 GridView 中的复选框

android - Xamarin (Android) - 在覆盖 GetItemID() 时,BaseAdapter 能否返回与 Long 不同的数据类型?