如何将数据添加到列表并在活动中显示它

时间:2014-03-31 06:44:23

标签: android

我正在尝试在android中创建一个简单的列表。但我的应用程序没有运行。我没有任何错误。我不知道什么是错的。你可以帮帮我.. 这是代码:

package com.example.mylistactivity;

import android.app.ListActivity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;


**MainActivity.java**
public class MainActivity extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListAdapter adapter = createAdapter();
        setListAdapter(adapter);
        }
        /**
         * Creates and returns a list adapter for the current list activity
         * @return
         */
        protected ListAdapter createAdapter()
        {
            // Create some mock data
            String[] testValues = new String[] { "Test1","Test2","Test3" };

            // Create a simple array adapter (of type string) with the test values
            ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, testValues);

            return adapter;
        }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }
}

这段代码出了什么问题..我的应用程序没有运行?

2 个答案:

答案 0 :(得分:0)

您应该在更改列表项后调用adapter.notifyDataSetChanged();

您的代码有点混乱

ListView listView = (ListView)findViewById(R.id.listview);
listView.setAdapter(createAdapter());

答案 1 :(得分:0)

我为你制作了这个列表视图,你可以用这个作为例子并使用它如果你对这个listview有任何疑问,请评论我..

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

这是活动的主要布局......

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    android:weightSum="10" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

    </ListView>

</LinearLayout>