Android在代码中添加自定义扩展listactivity到布局

时间:2011-06-01 18:44:12

标签: android class listview

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

我无法解决的是如何从我的extende活动类中实例化listview并将其添加到布局中?我想我正在咆哮错误的树!

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();
        }
    }    
}

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

3 个答案:

答案 0 :(得分:0)

您不应自行创建Activity对象。 Android会为你做这件事。阅读本文,它将对您有所帮助:http://developer.android.com/guide/topics/intents/intents-filters.html

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

答案 1 :(得分:0)

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

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

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

答案 2 :(得分:0)

只要你的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。