3

I have a custom BaseAdapter that creates a ListView of comments, usernames, and numbers. The issue is I don't know how to correctly implement the add method so I can update the BaseAdapter. Here is my Current BaseAdapter. My Add method is empty because I don't know where to start.

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

This is how I believe to call the add method.

mycmlist.add(comments,usernames,numbers); mycmlist.notifyDataSetChanged();

4

2 回答 2

2

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

例子:

void addToList(List<String> list)
{
    this.list.addAll(list);
    this.notifyDataSetChanged();
}
于 2013-08-22T05:05:53.070 回答
1

创建一个类Comment。根据您的需要修改类。例如使用getter/setter。

class Comment {
    String comment;
    String number;
    String username;

    Comment(String c, String n, String u) {
        comment = c;
        number = n;
        username = u;
    }
}

然后使用ArrayAdapter<>

class CommentsAdapter extends ArrayAdapter<Comment> {
    private final int mResLayout;

    public CommentsAdapter(Context context, int resource, List<Comment> objects) {
        super(context, resource, objects);
        mResLayout = resource;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Comment comment = getItem(position);
        // recycle view / inflate mResLayout to your view. bind values. maybe use ViewHolder pattern.
    }
}

在您的片段/活动中,您只需要保留对List<Comment>您传递给适配器的引用。您可以随心所欲地修改列表。调用adapter.notifyDataSetChanged()以使更改可见。

于 2013-08-22T03:27:15.630 回答