在listview之外调用数据

时间:2014-05-24 05:20:08

标签: android listview

main_layout.xml

......     
<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

<LinearLayout 
  android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      android:id="@+id/linearLayout3" 
      android:weightSum="1">

    <TextView 
           android:layout_width="180dp" 
           android:text="TOTAL" 
           android:layout_height="wrap_content" 

           android:paddingBottom="5dp" 
           android:paddingRight="5dp" 
           android:paddingTop="5dp" 
           android:layout_gravity="left" 
           android:layout_weight="0.32" 
           android:gravity="left|center" 
           android:paddingLeft="10dp" 
           android:textColor="#ab330b"></TextView>
    <TextView 
           android:layout_width="220dp" 

           android:layout_height="wrap_content" 
           android:id="@+id/total" 
           android:paddingBottom="5dp" 
           android:paddingRight="5dp" 
           android:paddingTop="5dp" 
           android:layout_gravity="left" 
           android:layout_weight="0.32" 
           android:gravity="left|center" 
           android:paddingLeft="10dp" 
           android:textColor="#ab330b"></TextView>
    </LinearLayout>
.......

GetTotal.class

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
........

  sksmkTv = (TextView)findViewById(R.id.TOTSKS);
}

........

........

........
try {
                    int success = json.getInt(TAG_SUCCESS);


                    if (success == 1) 
                    {

                        // products found
                        // Getting Array of Products
                        products = json.getJSONArray(TAG_PRODUCTS);

                        // looping through All Products
                        for (int i = 0; i < products.length(); i++) {
                            JSONObject c = products.getJSONObject(i);

                            // Storing each json item in variable
                            String kode = c.getString(KDKMK);
                            String sks = c.getString(SKSMK);
                            String nil = c.getString(NILMK);
                            String nmmk = c.getString(NMKMK);
                            String totalsks = c.getString(TOTSKS);

                            // creating new HashMap
                            HashMap<String, String> map = new HashMap<String, String>();

                            // adding each child node to HashMap key => value
                            map.put(KDKMK, kode);
                            map.put(SKSMK, sks);
                            map.put(NILMK, nil);
                            map.put(NMKMK, nmmk);
                            map.put(TOTSKS, totalsks );

                            // adding HashList to ArrayList
                            productsList.add(map);



                        }
                    }

                protected void onPostExecute(String file_url) {
                // dismiss the dialog after getting all products
                pDialog.dismiss();
                // updating UI from Background Thread
                runOnUiThread(new Runnable() {
                    public void run() {
                        /**
                         * Updating parsed JSON data into ListView
                         * */
                        ListAdapter adapter = new SimpleAdapter(
                                GetTotal.this, productsList,
                                R.layout.customgrid_khs, new String[] { NMKMK, 
                                        SKSMK, NILMK, TOTSKS  },
                                new int[] { R.id.NMKMK, R.id.SKSMK, R.id.NILMK, R.id.total });
                        // updating listview
                        setListAdapter(adapter);

                        sksmkTv.setText(TOTSKS);
                    }
                });

            }

这是我的代码示例。 我的列表视图正常工作,数据库中的数据显示在列表视图中。 但我想在main_layout.xml上打印值SKSMK,而不是在listview。

我应该添加什么代码,在android上打印SKSMK值:id =“@ + id / sksmk”

谢谢。

1 个答案:

答案 0 :(得分:0)

首先,您应该在Activity中处理TextView,如下所示:

private TextView sksmkTv;
sksmkTv = (TextView)findViewById(R.id.sksmk);

然后,使用String Object将SKSMK值保存在“try”代码块中。

最后,在“runOnUiThread”中设置文本

sksmkTv.setText(sksmkValue);