【问题标题】:how to refresh custom listview using baseadapter in android如何在android中使用baseadapter刷新自定义列表视图
【发布时间】:2012-11-20 07:06:18
【问题描述】:

先生,我如何使用 baseadapter 刷新我的自定义列表视图。我不知道该放置什么,或者将其放置在我的代码中的什么位置。请帮我。提前致谢

public class EditDetails extends Activity{
public String nameChanged;
public String numChanged;
public String name;
public String num;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.editdetails);
    final EditText sqlName = (EditText)findViewById(R.id.editName);
    final EditText sqlNumber = (EditText)findViewById(R.id.editNumber);
    name = CustomListView.name;
    num = CustomListView.number;
    Button bUpdate = (Button)findViewById(R.id.editUpdate);
    Button bView = (Button)findViewById(R.id.editView);
    sqlName.setText(name);
    sqlNumber.setText(num);

    bUpdate.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            nameChanged = sqlName.getText().toString();
            numChanged = sqlNumber.getText().toString();
            GroupDb info = new GroupDb(EditDetails.this);
            info.open();
            long rowid = info.getRowId(name, num);
            info.updateNameNumber(rowid, nameChanged, numChanged);
            ArrayList<Contact> searchResults = info.getView();
            MyCustomBaseAdapter mcba = new MyCustomBaseAdapter(EditDetails.this, searchResults);
            Toast.makeText(getApplicationContext(), "Update Successful!", Toast.LENGTH_LONG).show();
            info.close();
            }
        });
    bView.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            Intent intent = new Intent();
            intent.setClass(EditDetails.this, CustomListView.class);

            startActivityForResult(intent, 0);
            }
        });
}

}

这里是我显示我的列表视图的地方

public class CustomListView extends Activity {
final Context context = this;
public static String name;
public static String number;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    GroupDb info = new GroupDb(this);
    info.open();
    ArrayList<Contact> searchResults = info.getView();


    final ListView lv = (ListView) findViewById(R.id.srListView);
    lv.setAdapter(new MyCustomBaseAdapter(this, searchResults));
    info.close();

    lv.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            // TODO Auto-generated method stub
            Object o = lv.getItemAtPosition(position);
            final Contact fullObject = (Contact)o;
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
            alertDialogBuilder
            .setMessage("Select action")
            .setCancelable(false)
            .setPositiveButton("Edit", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    Toast.makeText(getApplicationContext(), "Edit ", Toast.LENGTH_LONG).show();
                    name = fullObject.getName();
                    number = fullObject.getPhoneNumber();
                    Intent contactIntent = new Intent("myfolder.proj.EDITDETAILS");
                    startActivity(contactIntent);
                }
              })

这是我的基础适配器类

public class MyCustomBaseAdapter extends BaseAdapter {
private static ArrayList<Contact> searchArrayList;

private LayoutInflater mInflater;

public MyCustomBaseAdapter(Context context, ArrayList<Contact> results) {
    searchArrayList = results;
    mInflater = LayoutInflater.from(context);
}

public int getCount() {
    return searchArrayList.size();
}

public Object getItem(int position) {
    return searchArrayList.get(position);
}

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

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.custom_row_view, null);
        holder = new ViewHolder();
        holder.txtName = (TextView) convertView.findViewById(R.id.name);

        holder.txtPhone = (TextView) convertView.findViewById(R.id.phone);

        holder.status = (TextView) convertView.findViewById(R.id.status);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txtName.setText(searchArrayList.get(position).getName());

    holder.txtPhone.setText(searchArrayList.get(position).getPhoneNumber());

    holder.status.setText(searchArrayList.get(position).getStatus());

    return convertView;
}

static class ViewHolder {
    TextView txtName;
    TextView txtPhone;
    TextView status;
}
}

【问题讨论】:

  • 为什么不调用adapater的notifyDataSetChanged()方法来更新listview中的数据呢?
  • 我把它放在哪里?我试过把它放在我的 EditDetails 类中,比如 mcba.notifyDataSetChanged() 并且没有任何反应。我不知道我是否做得对,但我认为我没有。
  • 我相信这是因为您需要在CustomListView Activity 中使用它,因为 ListView 是在该 Activity 中定义的。 EditDetails 无权访问 ListView 或其适配器。
  • 我在我的自定义列表视图中这样使用它,mcba.updateResults(searchResults);最终 ListView lv = (ListView) findViewById(R.id.srListView);但是,仍然得到相同的结果。

标签: android listview baseadapter


【解决方案1】:

两个选项:要么保留传递给构造函数的ArrayList 的引用,以便稍后修改实际的列表数据(由于未复制列表,因此修改适配器外部的数据仍会更新指针Adapter 正在引用),或重写 Adapter 以允许将列表重置为另一个对象。

在任何一种情况下,在 ArrayList 更改后,您必须调用 notifyDataSetChanged() 以使用更改更新您的 ListView。这可以在适配器内部或外部完成。所以,例如:

public class MyCustomBaseAdapter extends BaseAdapter {
    //TIP: Don't make this static, that's just a bad idea
    private ArrayList<Contact> searchArrayList;

    private LayoutInflater mInflater;

    public MyCustomBaseAdapter(Context context, ArrayList<Contact> initialResults) {
        searchArrayList = initialResults;
        mInflater = LayoutInflater.from(context);
    }

    public void updateResults(ArrayList<Contact> results) {
        searchArrayList = results;
        //Triggers the list update
        notifyDataSetChanged();
    }

    /* ...The rest of your code that I failed to copy over... */
}

HTH

【讨论】:

  • 这就是我使用 notifyDataSetChanged() 的方式,谢谢。但它仍然没有刷新我的列表,我想知道这是否是我的问题所在
  • 哦,我明白了。似乎 sqlite rowid 从 0 开始,列表视图从 1 开始,所以它不会改变。现在工作正常。谢谢顺便说一句
  • @UsuiTakumi:我没有得到你最后的评论,是什么改变让你成功了???还没有为我工作。
【解决方案2】:

在 BaseAdapter 中创建一个自定义方法

喜欢:

 public void updateAdapter(ArrayList<Contact> arrylst) {
        this.arrylst= arrylst;

        //and call notifyDataSetChanged
        notifyDataSetChanged();
    }

这个函数调用你想调用的地方:例如

adapterObject.updateAdapter(这里传ArrayList);

完成。

【讨论】:

    【解决方案3】:

    只需BaseAdapter,无需使用上下文

    listsOfNotes.remove(listsOfNotes.get(position));
    notifyDataSetChanged();
    

    只需将此代码放在setOnClickListner

    【讨论】:

    • 这项工作对我来说,不是喜欢,而是给我一个想法!当我使用 kotlin 时,我使用了 list.removeAt() 我不记得在 java 中是否有 removeAt。
    【解决方案4】:

    感谢以上解决方案为我工作的人。我在每个事件中都调用 listupdate 方法

      public void updateResults(List<TalebeDataUser> results) {
        talebeList = results;
        //Triggers the list update
        notifyDataSetChanged();
    }
    

    在更新列表之后,我还会在每次触摸时刷新我的按钮操作。例如,我有很多按钮可以在我的列表视图项目中单击,所以每次触摸都会改变其他人的风格

        private void setColor(TalebeDataUser talebeDataUser) {
        if (talebeDataUser.isVar()) {
            holder.mVar.setBackgroundResource(R.drawable.aw_secili);
            holder.mGorevli.setBackgroundResource(R.drawable.aw_shadow);
            holder.mYok.setBackgroundResource(R.drawable.aw_shadow);
            holder.mIzinli.setBackgroundResource(R.drawable.aw_shadow);
            holder.mHatimde.setBackgroundResource(R.drawable.aw_shadow);
        } else if (talebeDataUser.isGorevli()) {
            holder.mVar.setBackgroundResource(R.drawable.aw_shadow);
            holder.mGorevli.setBackgroundResource(R.drawable.aw_secili);
            holder.mYok.setBackgroundResource(R.drawable.aw_shadow);
            holder.mIzinli.setBackgroundResource(R.drawable.aw_shadow);
            holder.mHatimde.setBackgroundResource(R.drawable.aw_shadow);
        } else if (talebeDataUser.isYok()) {
            holder.mVar.setBackgroundResource(R.drawable.aw_shadow);
            holder.mGorevli.setBackgroundResource(R.drawable.aw_shadow);
            holder.mYok.setBackgroundResource(R.drawable.aw_secili);
            holder.mIzinli.setBackgroundResource(R.drawable.aw_shadow);
            holder.mHatimde.setBackgroundResource(R.drawable.aw_shadow);
        } else if (talebeDataUser.isIzinli()) {
            holder.mVar.setBackgroundResource(R.drawable.aw_shadow);
            holder.mGorevli.setBackgroundResource(R.drawable.aw_shadow);
            holder.mYok.setBackgroundResource(R.drawable.aw_shadow);
            holder.mIzinli.setBackgroundResource(R.drawable.aw_secili);
            holder.mHatimde.setBackgroundResource(R.drawable.aw_shadow);
        } else if (talebeDataUser.isHatimde()) {
            holder.mVar.setBackgroundResource(R.drawable.aw_shadow);
            holder.mGorevli.setBackgroundResource(R.drawable.aw_shadow);
            holder.mYok.setBackgroundResource(R.drawable.aw_shadow);
            holder.mIzinli.setBackgroundResource(R.drawable.aw_shadow);
            holder.mHatimde.setBackgroundResource(R.drawable.aw_secili);
        }
    
    }
    

    只是我的一个按钮中的一个示例

      holder.mYok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //talebeList.remove(currentTalebe);
                setOgrenciNameByDurum(talebeList.get(i));
                talebeList.get(i).setYok(true);
                //setOgrenciNameByDurum(currentTalebe);
                talebeList.get(i).setVar(false);
                talebeList.get(i).setGorevli(false);
                talebeList.get(i).setIzinli(false);
                talebeList.get(i).setHatimde(false);
                updateResults(talebeList);
                setColor(talebeList.get(i));
                //saveCurrentTalebeOnShare(currentTalebe);
            }
        });
    

    talebeList 只是List&lt;MyModel&gt; talebeList

    【讨论】:

      【解决方案5】:

      我解决了这个问题,将此功能添加到我的自定义适配器

      public void newCursor(Cursor cursor)
       {
        this.cursor=cursor;
        this.notifyDataSetChanged();
       }
      

      从我的主类中,我创建了一个新游标,对数据库进行重新查询,然后发送到我的 通过此函数自定义适配器。

      祝你好运

      【讨论】:

        猜你喜欢
        • 2013-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多