Android在代码中向布局添加自定义扩展listactivity
我有一个简单的线性布局,带有一个文本框、一个按钮和一个listview,我正在做一些URL数据获取,当在文本框中输入一些内容并按下按钮时,我想解析结果并显示在listview中 我无法解决的是如何从extende活动类中建立listview并将其添加到布局中?我想我找错人了Android在代码中向布局添加自定义扩展listactivity,android,class,listview,Android,Class,Listview,我有一个简单的线性布局,带有一个文本框、一个按钮和一个listview,我正在做一些URL数据获取,当在文本框中输入一些内容并按下按钮时,我想解析结果并显示在listview中 我无法解决的是如何从extende活动类中建立listview并将其添加到布局中?我想我找错人了 public class HelloAndroid extends Activity{ /** Called when the activity is first created. */ @Override
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会帮你做的。阅读本文,它将帮助您:
如果要将ListView添加到布局中,只需将其添加到布局xml文件中即可。在父活动扩展ActivityGroup之前,无法将一个活动添加到另一个活动。但这不是您的情况。如果您想将列表添加到布局中,请使用而不是ListActivity 然后可以将列表添加为this.addView myList
请按照这里的示例进行操作:只要main.xml布局文件中有一个ListView,您的路径就是正确的。将其@+id设置为任意值,并在代码中使用以下内容引用它:
ListView lv = (ListView) findViewById(R.id.arbitrary_id);
使用ListView不一定需要ListActivity。只需解析从服务器接收回来的数据,将其放入一个数组中,然后使用lv.setAdapteryour_array_adapter用数据填充ListView
您可以通过以下方式进一步指定ItemClickListener:
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
}
};
并使用将其添加到布局中的视图中
some_container_view_in_main.addView(lv);
然后将ArrayAdapter和OnItemClickListener的设置与上面相同
some_container_view_in_main.addView(lv);