Create Android layout with buttons from server res

2019-06-09 18:04发布

问题:

I checked around and and I can never quite find something that helps me with what I am trying to accomplish. In my Android app I want to be able to make a request to the server for some data, when that data is returned I want to populate a scrollview because there is too much to fit in one display. The reason I am doing this is because the data will be updated too often for me to do continuous updates of the app (imaginee twitter). I've already built out the XML linearlayout which will populate the scroll view I just need to find the best way of building out the View.

I've brainstormed a few methods which this could be possible and I'm reaching out for which would be most practical if possible.

  1. Passing the data as a XML feed - I feel this would allow me to run through all the data which is fed from the server and using the XML tags pull the content and place into the appropriate location in the layout. Then build the next layout and go from there.

  2. Passing the full layout XML from the server - This could build the layout but I feel would prevent me from making the buttons on the layout which will trigger onclick from being setup as I am passing all the data. I know there are anonymous listeners which could do the job but I'm not quite sure how I could get it to trigger in this case.

I had a third but it has escaped me at this time. Any help is appreciated.

回答1:

try this,

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


        }

I have created a layout xml file in which I have added a linearlayout with id button_list

main.xml file-

<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>


回答2:

Sorry for taking so long on this response, I've been busy with other things and couldn't code. That was really good but only if I am building the button as I go along. Since I am inflating a layout I need to select a pre existing button by selecting it as such.

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

After this I can build out the onclicklistener then add the new view to the layout. Thanks for the help though! Your script pointed in in the correct direction to identify what needed to be done.!