Php android:如何在从数据库填充的listview中插入按钮?
我是开发android的新手。我想在listview中由mysql填充的每一行中插入按钮。我一直在遵循这个教程从 我渴望学习Php android:如何在从数据库填充的listview中插入按钮?,php,android,mysql,listview,Php,Android,Mysql,Listview,我是开发android的新手。我想在listview中由mysql填充的每一行中插入按钮。我一直在遵循这个教程从 我渴望学习 public class RollCall extends Activity { private String jsonResult; private String url = "http://rektitmate.com/webservices/student_details.php"; private ListView listView;
public class RollCall extends Activity {
private String jsonResult;
private String url = "http://rektitmate.com/webservices/student_details.php";
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_roll_call);
listView = (ListView) findViewById(R.id.listView1);
listView.setItemsCanFocus(false);
accessWebService();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_roll_call, menu);
return true;
}
// Async Task to access the web
private class JsonReadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
try {
HttpResponse response = httpclient.execute(httppost);
jsonResult = inputStreamToString(
response.getEntity().getContent()).toString();
}
catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}
catch (IOException e) {
// e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error..." + e.toString(), Toast.LENGTH_LONG).show();
}
return answer;
}
@Override
protected void onPostExecute(String result) {
Listout();
}
}// end async task
public void accessWebService() {
JsonReadTask task = new JsonReadTask();
// passes values for the urls string array
task.execute(new String[] { url });
}
// build hash set for list view
public void Listout() {
List<Map<String, String>> StudentList = new ArrayList<Map<String, String>>();
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("student");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String name = jsonChildNode.optString("student name");
String number = jsonChildNode.optString("stuid");
String outPut = number + "." + name;
StudentList.add(createStudent("student", outPut));
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error" + e.toString(),
Toast.LENGTH_SHORT).show();
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this, StudentList,
android.R.layout.simple_list_item_1,
new String[] { "student" }, new int[] { android.R.id.text1 });
listView.setAdapter(simpleAdapter);
}
private HashMap<String, String> createStudent(String name, String number) {
HashMap<String, String> studentNameNo = new HashMap<String, String>();
studentNameNo.put(name, number);
return studentNameNo;
}
}
公共类RollCall扩展活动{
私有字符串jsonResult;
专用字符串url=”http://rektitmate.com/webservices/student_details.php";
私有列表视图列表视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u roll\u call);
listView=(listView)findViewById(R.id.listView1);
setItemsCanFocus(false);
accessWebService();
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.menu\u roll\u call,menu);
返回true;
}
//访问web的异步任务
私有类JsonReadTask扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…参数){
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(参数[0]);
试一试{
HttpResponse response=httpclient.execute(httppost);
jsonResult=inputStreamToString(
response.getEntity().getContent()).toString();
}
捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回null;
}
私有StringBuilder inputStreamToString(InputStream为){
字符串rLine=“”;
StringBuilder answer=新建StringBuilder();
BufferedReader rd=新的BufferedReader(新的InputStreamReader(is));
试一试{
而((rLine=rd.readLine())!=null){
答:追加(rLine);
}
}
捕获(IOE异常){
//e.printStackTrace();
Toast.makeText(getApplicationContext(),
“错误…”+e.toString(),Toast.LENGTH_LONG).show();
}
返回答案;
}
@凌驾
受保护的void onPostExecute(字符串结果){
Listout();
}
}//结束异步任务
public void accessWebService(){
JsonReadTask=新建JsonReadTask();
//传递URL字符串数组的值
执行(新字符串[]{url});
}
//为列表视图生成哈希集
公开作废列表(){
List StudentList=new ArrayList();
试一试{
JSONObject jsonResponse=新的JSONObject(jsonResult);
JSONArray jsonMainNode=jsonResponse.optJSONArray(“学生”);
for(int i=0;i
您希望在每个列表项布局中都有该按钮还是动态添加它?我希望在列表的每一行中都有该按钮,然后您必须构建一个自定义XML布局,而不是使用本机android布局。哦,好的,我想我知道了。我现在就试试。谢谢你,冠军。我会尽力的你还在做这个吗?我也这样做了,在ListView中添加按钮是很常见的。