如何从SlidingTab Fragment显示ListView项?

时间:2015-07-30 08:44:16

标签: android android-fragments android-listview

这是我想在SlidingTab片段中显示listview项的代码。 先生,我已发布单个post.xml     公共类Tab1扩展了Fragment {

@(RenderLink(x => x.Settings.First().CompanyLink,new HtmlAttributes().CssClass("navbar-brand page-scroll").Render())

这是获取listviewitem

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v =inflater.inflate(R.layout.productlist,container,false);
        return v;

    }

}

productlist.xml

public class productlist extends ListActivity {

    // Progress Dialog
    private ProgressDialog pDialog;


    private static final String READ_COMMENTS_URL = "http://192.168.254.101/productlist.php";


    private static final String TAG_POSTS = "message";

    private static final String TAG_BRAND = "Brand";

    private static final String TAG_CATEGORY = "Category";

    private static final String TAG_DESCRIPTION = "Description";

    private static final String TAG_CODE = "Code";

    private static final String TAG_QUANTITY = "Quantity";

    private static final String TAG_UNIT = "Unit";

    private static final String TAG_UNITPRICE = "Unitprice";

    private JSONArray mComments = null;

    //manages all of our comments in a list.

    private ArrayList<HashMap<String, String>> mCommentList;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);


        setContentView(R.layout.productlist);

    }


    @Override

    protected void onResume() {

        // TODO Auto-generated method stub

        super.onResume();

        //loading the comments via AsyncTask

        new LoadComments().execute();

    }


    public void updateJSONdata() {


        mCommentList = new ArrayList<HashMap<String, String>>();

        JSONParser jParser = new JSONParser();

        JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);


        try {


            mComments = json.getJSONArray(TAG_POSTS);

            for (int i = 0; i < mComments.length(); i++) {

                JSONObject c = mComments.getJSONObject(i);

                //gets the content of each tag

                String brand = c.getString(TAG_BRAND);

                String category = c.getString(TAG_CATEGORY);

                String description = c.getString(TAG_DESCRIPTION);

                String code = c.getString(TAG_CODE);

                String quantity = c.getString(TAG_QUANTITY);

                String unit = c.getString(TAG_UNIT);

                String unitprice = c.getString(TAG_UNITPRICE);


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

                map.put(TAG_BRAND, brand);

                map.put(TAG_CATEGORY, category);

                map.put(TAG_DESCRIPTION, description);

                map.put(TAG_CODE, code);

                map.put(TAG_QUANTITY, quantity);

                map.put(TAG_UNIT, unit);

                map.put(TAG_UNITPRICE, unitprice);

                mCommentList.add(map);

            }

        } catch (JSONException e) {

            e.printStackTrace();

        }

    }

    private void updateList() {

        ListAdapter adapter = new SimpleAdapter(this, mCommentList,

                R.layout.single_post, new String[] { TAG_BRAND, TAG_CATEGORY,

                TAG_DESCRIPTION, TAG_CODE, TAG_QUANTITY, TAG_UNIT, TAG_UNITPRICE}, new int[]{ R.id.Brand, R.id.Category,

                R.id.Description, R.id.Code, R.id.Quantity, R.id.Unit, R.id.Price });

        setListAdapter(adapter);


        ListView lv = getListView();

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override

            public void onItemClick(AdapterView<?> parent, View view,

                                    int position, long id) {



            }

        });

    }

    public class LoadComments extends AsyncTask<Void, Void, Boolean> {

        @Override

        protected void onPreExecute() {

            super.onPreExecute();

            pDialog = new ProgressDialog(productlist.this);

            pDialog.setMessage("Loading Products...");

            pDialog.setIndeterminate(false);

            pDialog.setCancelable(true);

            pDialog.show();

        }

        @Override

        protected Boolean doInBackground(Void... arg0) {

            updateJSONdata();

            return null;

        }

        @Override

        protected void onPostExecute(Boolean result) {

            super.onPostExecute(result);

            pDialog.dismiss();

            updateList();

        }

    }

}

single_post.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#fff" >

<LinearLayout

    android:id="@+id/top_layover"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:layout_alignParentLeft="true"

    android:layout_alignParentTop="true"

    android:orientation="vertical" >

    <TextView

        android:layout_height="wrap_content"

        android:layout_gravity="center"

        android:gravity="center"

        android:text="Products"

        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="wrap_content" />

</LinearLayout>

<ListView

    android:id="@android:id/list"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:layout_above="@+id/bottom_layover"

    android:layout_below="@+id/top_layover"

    android:background="#fff"

    android:divider="@android:color/transparent"

    android:scrollbars="none" />

<LinearLayout

    android:id="@+id/bottom_layover"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:layout_alignParentBottom="true"

    android:layout_alignParentLeft="true"


    android:orientation="horizontal"

    android:weightSum="2" >

    <LinearLayout

        android:layout_width="0dp"

        android:layout_height="wrap_content"

        android:layout_weight="1"

        android:orientation="vertical" >

    </LinearLayout>

</LinearLayout>

机器人:比重=&#34;左&#34;

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="#f0f0f0"

android:orientation="vertical" >

<LinearLayout
    android:id="@+id/box"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_margin="2dp"


    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="vertical"

        android:paddingBottom="5dp"

        android:background="#ffffff">

        <LinearLayout
            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:gravity="left"

                android:paddingLeft="5dp"

                android:text="Brand: ">

            </TextView>

            <TextView

                android:id="@+id/Brand"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:gravity="left"

                android:textColor="#5d5d5d"

                android:textStyle="bold" >

            </TextView>

        </LinearLayout>

        <LinearLayout

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:orientation="horizontal">


            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:gravity="left"

                android:paddingLeft="5dp"

                android:text="Category: " >

            </TextView>

            <TextView

                android:id="@+id/Category"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:gravity="left"

                android:textColor="#5d5d5d"

                android:textStyle="bold" >

            </TextView>

        </LinearLayout>


        <LinearLayout

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:orientation="horizontal">


            <TextView
                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:gravity="left"

                android:paddingLeft="5dp"

                android:text="Description: " >

            </TextView>

            <TextView

                android:id="@+id/Description"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:gravity="left"

                android:textColor="#5d5d5d"

                android:textStyle="bold" >

            </TextView>

        </LinearLayout>

        <LinearLayout

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:orientation="horizontal">


            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:gravity="left"

                android:paddingLeft="5dp"

                android:text="Code: " >

            </TextView>

            <TextView

                android:id="@+id/Code"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:gravity="left"

                android:textColor="#5d5d5d"

                android:textStyle="bold" >

            </TextView>

        </LinearLayout>



        <LinearLayout
            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:orientation="horizontal">

            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

1 个答案:

答案 0 :(得分:0)

您的标签应该扩展ListFragment。

public class Tab extends ListFragment{

  //your existing code...

  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState); 
      new LoadComments().execute();
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v =inflater.inflate(R.layout.productlist,container,false);
    return v;
  }

  //your existing code...
}
  

我没有测试代码但它应该可以工作。