使用服务器响应中的按钮创建Android布局

问题描述:

我检查了周围环境,并且我无法完全找到可以帮助我完成自己想要完成的任务的东西。在我的Android应用程序中,我希望能够向服务器请求某些数据,当返回数据时,我想填充滚动视图,因为在一个显示中有太多内容。我这样做的原因是因为数据会经常更新以使我不断更新应用程序(想像中的twitter)。我已经构建了XML linearlayout,它将填充滚动视图,我只需要找到构建视图的最佳方式。使用服务器响应中的按钮创建Android布局

我已经头脑风暴了几种方法,这可能是可能的,并且如果可能的话,我正在接触这些方法。

  1. 传递数据作为XML饲料 - 我觉得这将使我通过所有从服务器反馈的数据运行,并使用XML标记将数据内容并放入布局中的适当位置。然后建立下一个布局并从那里开始。

  2. 从服务器传递完整的布局XML - 这可能会构建布局,但我觉得会阻止我在布局上触发onclick上的按钮,因为我正在传递所有数据。我知道有些匿名听众可以完成这项工作,但我不太清楚在这种情况下我该如何触发它。

我有第三个,但它在这个时候逃脱了我。任何帮助表示赞赏。

对不起,我走在布局文件很长时间以来,我一直忙于其他事情,无法编码。这是非常好的,但只有当我在建设按钮,我一直沿着。由于我正在充气布局,所以我需要选择一个预先存在的按钮。

Button thisbutton =(Button)findViewById(R.id.buttonid);

之后,我可以建立onclicklistener然后添加新的视图到布局。感谢您的帮助!你的脚本指出了正确的方向,以确定需要完成的工作。

试试这个,

public void listButtons() { 
        LinearLayout layout = (LinearLayout)findViewById(R.id.button_list); 
      String http_url = "http://example.com/demo"; 
      try { 

       URL url = new URL(http_url); 
       HttpURLConnection con = HttpURLConnection)url.openConnection(); 

       if(con!=null){ 
        BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream())); 
        String data=br.readLine(); 
         JSONArray jsonArray=null; 
         jsonArray = new JSONArray(data); 
         Button tv[] = new Button[jsonArray.length()]; 
         for(int i=0;i<jsonArray.length();i++) 
         { 
          tv[i] = new Button(this); 

          tv[i].setText(jsonArray.getJSONObject(i).getString("name")); 

          tv[i].setGravity(Gravity.CENTER_HORIZONTAL); 

          tv[i].setOnClickListener(new View.OnClickListener() { 

            @Override 
            public void onClick(View v) { 
          //add your code here 
            } 
           }); 
          layout.addView(tv[i]); 

         } 
       } 


       } catch (MalformedURLException e) { 
       e.printStackTrace(); 
       } catch (IOException e) { 
       e.printStackTrace(); 

       } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 


     } 

我已经创建了我已经加了LinearLayout中与ID button_list

的main.xml文件 -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 

    tools:context=".ResearchActivity" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginTop="55dp" 
     android:orientation="vertical" 
     android:id="@+id/button_list" 
     > 

    </LinearLayout> 

</LinearLayout> 
+0

我在哪里使用电视[i] .setText(jsonArray.getJSONObject(i).getString(“name”));是从XML提要获取结果? – TheHamstring 2013-02-28 11:24:55

+0

在我的示例中,我将名称设置为从上面的url返回的JSON数据的按钮......您可以解析您的XML提要,并且可以设置该按钮的任何文本。 – Ajit 2013-03-01 05:18:15

+0

当我放入第一个按钮创建时,警告“参数的左侧必须是变量” Button purch [] = new Button [tokens.length();我++]; 但令牌是我的变量,我从稍后读取数据填充我的列表视图。 另外我想定位一个已经存在于我的布局中的按钮,这个按钮会自动定位该按钮,还是我需要以某种方式指定它。 – TheHamstring 2013-03-29 11:25:07