自定义BaseAdapter不会添加新数据 - Android

时间:2013-08-21 22:02:39

标签: android android-listview baseadapter

我有一个自定义baseadapter,可以创建注释框。在我想要添加数据之前,一切都很好用。当我尝试添加数据时,它会删除以前的数据并添加新数据。如何制作它以保存所有数据?我的Add方法不正确吗?这是我的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;
  }
}  

设置适配器:

final CreateCommentLists mycmlist = new CreateCommentLists(comments, usernames, numbers, DashboardActivity.this);

lstComments = (ListView)findViewById(android.R.id.list);

lstComments.setAdapter(mycmlist);

这就是我如何调用add方法

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

2 个答案:

答案 0 :(得分:1)

在你的添加方法中,你将数组设置为新值listComments = comments;那就是用新数据替换旧数据。

您可以使用System.arrayCopy()将listArrays的大小调整为新大小并附加新项目。然而,一种不那么繁琐的方法是将数组存储为List<String>,允许您添加更多项而无需担心调整列表大小。

结果看起来像这样......

public class CommentsAdapter extends BaseAdapter
{
private LayoutInflater inflater;

private List<String> comments;
private List<String> numbers;
private List<String> usernames;

public CommentsAdapter(Context context)
{
    inflater = LayoutInflater.from(context);

    comments = new ArrayList<String>();
    numbers = new ArrayList<String>();
    usernames = new ArrayList<String>();
}

public void add(String[] comments, String[] numbers, String[] usernames)
{
    this.comments.addAll(Arrays.asList(comments));
    this.numbers.addAll(Arrays.asList(numbers));
    this.usernames.addAll(Arrays.asList(usernames));

    notifyDataSetChanged();
}

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

    return comments.size();
}

@Override
public String getItem(int position)
{
    return comments.get(position);
}

@Override
public long getItemId(int position)
{
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    if (convertView == null)
    {
        convertView = inflater.inflate(R.layout.list_item, parent, false);
        convertView.setTag(new ViewHolder(convertView));
    }

    ViewHolder holder = (ViewHolder) convertView.getTag();
    holder.commentView.setText(comments.get(position));
    //Other view bind logic here...

    return convertView;
}

private static class ViewHolder
{
    public TextView commentView;
    public TextView numbersView;
    public TextView usernamesView;
    public Button usernameButton;
    public Button numberButton;

    public ViewHolder(View v)
    {
        commentView = (TextView) v.findViewById(R.id.listComment);
        numbersView = (TextView) v.findViewById(R.id.listNumber);
        usernamesView = (TextView) v.findViewById(R.id.listPostedBy);
        usernameButton = (Button) v.findViewById(R.id.listUsernameButton);
        numberButton = (Button) v.findViewById(R.id.listNumberButton);
    }
}
}

我还强烈建议您在Android开发者网站上阅读此页面:http://developer.android.com/training/improving-layouts/smooth-scrolling.html

您当前的适配器实现效率非常低,该页面可以帮助您解决一些问题。

答案 1 :(得分:0)

您可能需要将String[]数组添加到现有数组,而不是替换它。

添加这个连接两个数组的函数(遗憾的是,没有已经实现的Java方法):

String[] concat(String[] A, String[] B) {
   String[] C= new String[A.length + B.length];
   System.arraycopy(A, 0, C, 0, A.length);
   System.arraycopy(B, 0, C, A.length, B.length);
   return C;
}

致谢:Sun论坛

然后将add方法更改为:

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

你的代码中有一个拼写错误。在add方法中,我应该交换listUsernameslistNumbers ..我已经为你解决了这个问题。