基于字符串的列表视图



我正在 Eclipse 中做一个项目,在谷歌搜索了一些资源后,我找不到一个基本的教程来实现我想做的事情。

我需要制作一个列表视图,其中包含数组列表中的字符串。没什么好看的 - 我希望能够将项目滑出屏幕或按按钮将它们从列表中删除。

有没有简单的方法可以做到这一点?如果您知道任何可以帮助我解决此特定问题的教程,请分享教程。

我为您制作了此列表视图,您可以使用此列表视图作为示例并使用此视图,如果您对此列表视图有任何疑问,请评论我..

public class MainActivity extends Activity {
    private ArrayList<String> mListItems;
    private ListView listView;
    private ArrayAdapter<String> adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        listView = (ListView)findViewById(R.id.listView1);
        mListItems = new ArrayList<String>();
        for(String name : mStrings)
        {
            mListItems.add(name);
        }
        adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, mListItems);
        listView.setAdapter(adapter);   

        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapter, View view, int position,
                    long i) {
                String name = (String) adapter.getItemAtPosition(position);
                MainActivity.this.adapter.remove(name);         
            }
        });
    }

    private String[] mStrings = { "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
            "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
            "Allgauer Emmentaler", "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
            "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
            "Allgauer Emmentaler","Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
            "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
            "Allgauer Emmentaler", "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
            "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
            "Allgauer Emmentaler" };
}

GitHub上有很好的"滑动关闭"项目:Android-SwipeToDismiss,你可以与此线程中包含的示例列表视图一起使用。祝你好运:-)

假设您的activity_main.xml中有一个 id listview的 ListView,则以下代码应该可以工作。

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ArrayList<String> values = new ArrayList<String>();
        int rows = 10;
        for(int i = 0; i < rows; i ++) {
            values.add("row " + i);
        }
        ListView listView = (ListView) findViewById(R.id.listview);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
        listView.setAdapter(adapter);
    }
}

您必须研究适配器才能完全了解 ListView 的工作原理。这是一个非常好的视频:https://www.youtube.com/watch?v=N6YdwzAvwOA。

public class YourActivity extends Activity {
private ListView lv;
public void onCreate(Bundle saveInstanceState) {
     super.onCreate(saveInstanceState);
     setContentView(R.layout.your_layout);
     lv = (ListView) findViewById(R.id.your_list_view_id);
     // Instanciating an array list (you don't need to do this, 
     // you already have yours).
     List<String> your_array_list = new ArrayList<String>();
     your_array_list.add("foo");
     your_array_list.add("bar");
     // This is the array adapter, it takes the context of the activity as a 
     // first parameter, the type of list view as a second parameter and your 
     // array as a third parameter.
     ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
             this, 
             android.R.layout.simple_list_item_1,
             your_array_list );
     lv.setAdapter(arrayAdapter); 
}
}

相关内容

  • 没有找到相关文章