Android的 - 添加和使用自定义多行的ListView删除项ArrayAdapter [英] Android -- Adding and removing items from a multiline ListView with a custom ArrayAdapter

查看:217
本文介绍了Android的 - 添加和使用自定义多行的ListView删除项ArrayAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图构建由ArrayAdapter支持多行的ListView。我想在列表中的每一行有两条线和一个删除按钮旁边。当点击删除按钮,在阵列中的项目应该被删除,并在ListView应该自我刷新。

I am trying to construct a multiline ListView backed by an ArrayAdapter. I would like each row in the list to have two lines and a delete button next to it. When the delete button is clicked, the items in the arrays should be removed and the ListView should refresh itself.

这是code代表在MainActivity,它有一个内部类是定制ArrayAdapter:

This is the code for the MainActivity, which has an inner class that is the custom ArrayAdapter:

public class MainActivity extends Activity {
ArrayList<String> artistsList = new ArrayList<String>();
String[] artists = {"Matt Nathanson", "Green Day", "Three Days Grace", "LMFAO", "One Direction", 
        "Linkin Park", "Neon Trees", "Cee Lo Green", "Plain White T's", "Metallica", "Maroon 5",
        "Sick Puppies", "Bruno Mars", "Billy Joel", "Don McLean", "Adele", "Gary Jules"};
ArrayList<String> songsList = new ArrayList<String>();
String[] songs = {"Come On Get Higher", "Holiday", "I Hate Everything About You", "Sexy and I Know It", "What Makes You Beautiful",
        "What I've Done", "Animal", "Fuck You", "Hey There Delilah", "Sandman", "Moves Like Jagger",
        "Rip Tide", "Talking to the Moon", "Piano Man", "American Pie", "Someone Like You", "Mad World"};

ListView list;
TestArrayAdapter mArrayAdapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    for(String s : artists) {
        artistsList.add(s);
    }
    for(String s : songs) {
        songsList.add(s);
    }

    mArrayAdapter = new TestArrayAdapter();

    list = (ListView)findViewById(R.id.mainlist);

    list.setAdapter(mArrayAdapter);
}


class TestArrayAdapter extends ArrayAdapter<String> {

    public TestArrayAdapter() {
        super(MainActivity.this, R.layout.row, songs);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();
        View row=inflater.inflate(R.layout.row, parent, false);

        TextView primaryTextView = (TextView)row.findViewById(R.id.primaryTextView);
        TextView secondaryTextView = (TextView)row.findViewById(R.id.secondaryTextView);

        primaryTextView.setText(songsList.get(position));
        secondaryTextView.setText(artistsList.get(position));

        ImageView deleteButton = (ImageView)row.findViewById(R.id.deleteRowButton);
        deleteButton.setTag(position);
        deleteButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                artistsList.remove(v.getTag());
                songsList.remove(v.getTag());
                mArrayAdapter.notifyDataSetChanged();
            }
        });

        return row;
    }
}

调用notifyDataSetChanged()似乎并没有做任何事情;但是,在的ArrayList的项都被删除。在ListView似乎是独立的变化,我做了的ArrayList和一个ArrayAdapter的。理想情况下,当我从底层数据删除项目,我应该能够调用notifyDataSetChanged()和ListView控件会自动更新。

Calling notifyDataSetChanged() does not seem to do anything; however, the items in the ArrayLists are being deleted. The ListView seems to be independent of changes that I make in the ArrayLists and the ArrayAdapter. Ideally, when I remove an item from the underlying data, I should be able to call notifyDataSetChanged() and the ListView will update itself.

我已经看过类似这样的其他问题,但他们都只有一个ArrayList中提供数据到ListView,我无法弄清楚如何适应用于多行的解决方案。先谢谢了。

I have looked at the other questions similar to this, but all of them have only a single ArrayList providing data to the ListView, and I can't figure out how to adapt those solutions for multiline rows. Thanks in advance.

推荐答案

我最近就如何创建一个类似的列表视图写的教程。看看这里

I wrote a tutorial recently on how to create a similar listview. Check it out here

http://www.shubhayu.com/android/listview-with-arrayadapter-and-customized-items

这会帮助你创建你想要的,除了删除部分内容。缺失会不会以后很难。你只需要处理一下你的删除按钮的onclick。我会引导你通过它一旦你的清单和运行。

This would help you in creating what you want except the deletion part. The deletion will not be difficult after that. You just need to handle it on the onClick of your delete button. I'll guide you through it once you get the list up and running.

此外,覆盖你的getCount()。这应该做的伎俩。

Also, override you getCount(). That should do the trick.

这篇关于Android的 - 添加和使用自定义多行的ListView删除项ArrayAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆