【问题标题】:Android adding a custom extended listactivity to layout in codeAndroid在代码中添加自定义扩展列表活动到布局
【发布时间】:2011-06-01 18:44:11
【问题描述】:

我有一个简单的线性布局,带有一个文本框、一个按钮和一个列表视图,当在文本框中输入某些内容并按下按钮时,我正在获取一些 URL 数据我想解析结果并显示在列表视图中。

我无法解决的是如何从我的扩展活动类中实例化列表视图并将其添加到布局中?我想我找错树了!

public class HelloAndroid extends Activity{
    /** Called when the activity is first created. */
    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        MyList L;
        L = new MyList();

        //setContentView(L.getListView());

        EditText edittext = (EditText)findViewById(R.id.editText1);
        edittext.setText("");


        Button button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    EditText edittext = (EditText)findViewById(R.id.editText1);
                    executeHttpGet(edittext.getText().toString());
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 
            }
        });  
    }

    public void executeHttpGet(String vrm) throws Exception {
        BufferedReader in = null;
        try {
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet();
            request.setURI(new URI("http://xxx/vrmtest.asp?vrm="+vrm));
            HttpResponse response = client.execute(request);
            in = new BufferedReader
            (new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) {
                sb.append(line + NL);
            }
            in.close();
            String page = sb.toString();
            System.out.println(page);

            Context context = getApplicationContext();
            CharSequence text = page;
            int duration = Toast.LENGTH_LONG;


            Toast toast = Toast.makeText(context, text, duration);
            toast.show();            

            } finally {
            if (in != null) {
                try {
                    in.close();
                    } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public class MyList extends ListActivity {

        /** Called when the activity is first created. */
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            // Create an array of Strings, that will be put to our ListActivity
            String[] names = new String[] { "Linux", "Windows7", "Eclipse", "Suse",
                    "Ubuntu", "Solaris", "Android", "iPhone"};
            // Create an ArrayAdapter, that will actually make the Strings above
            // appear in the ListView
            this.setListAdapter(new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, names));
        }

        @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
            super.onListItemClick(l, v, position, id);
            // Get the item that was clicked
            Object o = this.getListAdapter().getItem(position);
            String keyword = o.toString();
            Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG)
                    .show();
        }
    }    
}

一个解释就足够了,而不是代码,但一个例子会很好。

【问题讨论】:

    标签: android class listview


    【解决方案1】:

    只要你的 main.xml 布局文件中有一个 ListView,你就走在了正确的道路上。将其@+id 设置为任意值并在您的代码中引用它:

    ListView lv = (ListView) findViewById(R.id.arbitrary_id);
    

    您不一定需要 ListActivity 才能使用 ListView。只需解析您从服务器接收回来的数据,将其放入一个数组中,然后使用lv.setAdapter(your_array_adapter) 将您的数据填充到 ListView。

    您可以通过以下方式进一步指定您的 ItemClickListener:

    lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
        }
    };
    

    或者,您可以使用编程方式创建 ListView

    ListView lv = new ListView(this);
    

    并将其添加到布局中的视图中

    some_container_view_in_main.addView(lv);
    

    然后您将 ArrayAdapter 和 OnItemClickListener 设置为与上述相同。

    【讨论】:

      【解决方案2】:

      您不应该自己创建Activity 对象。安卓会为你做这件事。阅读这篇文章,对你有帮助:http://developer.android.com/guide/topics/intents/intents-filters.html

      如果您想在布局中添加ListView,只需将其添加到布局 xml 文件中即可。在父活动扩展ActivityGroup 之前,您不能将一个活动添加到另一个活动。但那不是你的情况。

      【讨论】:

        【解决方案3】:

        如果您想要将列表添加到布局中,请使用 ListView 而不是 ListActivity。

        然后您可以将列表添加为this.addView (myList);

        请按照此处的示例进行操作:http://developer.android.com/resources/tutorials/views/hello-listview.html

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-03-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-06-17
          相关资源
          最近更新 更多