从JsonTask显示片段中的ListView

时间:2016-12-28 05:38:55

标签: android listview

我想在按钮点击片段后显示列表视图。

public class Price extends Fragment{
Button submit;
submit = (Button) v.findViewById(R.id.submit);
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ListView lvShareSummary;
            lvShareSummary = (ListView)v.findViewById(R.id.listviewStatement);
            new JSONTaskShare(v.getContext(), lvShareSummary).execute("http://localhost:8080/price/PriceList");

        }
    });
}
}

XML布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
    android:id="@+id/relativeLayout4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="200dp">
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#eed369"
    android:text="Submit"
    android:textColor="#182237"
    android:textSize="30sp"
    android:id="@+id/submit"
    android:layout_marginTop="30dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />
</RelativeLayout>
<ListView
    android:id="@+id/listviewStatement"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/relativeLayout4"
    android:padding="5dp" />
</RelativeLayout>

没有按钮单击列表视图工作正常。但是我想在点击按钮后显示列表视图。

3 个答案:

答案 0 :(得分:1)

感谢雷德曼的回答

public class Price extends Fragment{
Button submit;
ListView lvShareSummary;
lvShareSummary = (ListView)v.findViewById(R.id.listviewStatement);
submit = (Button) v.findViewById(R.id.submit);
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
submit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        new JSONTaskShare(v.getContext(), lvShareSummary).execute("http://localhost:8080/price/PriceList");

    }
});
}
}

答案 1 :(得分:0)

好像你是android中的新手。片段代码不能这样写。片段内的子视图必须使用我们所拥有的视图对象进行初始化 onCreateView所以你的代码是这样的:

public class Price extends Fragment{
Button submit;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        View fragmentView= inflater.inflate(R.layout.layout_fragment, container, false);

submit = (Button) fragmentView.findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ListView lvShareSummary;
            lvShareSummary = (ListView)v.findViewById(R.id.listviewStatement);
            new JSONTaskShare(v.getContext(), lvShareSummary).execute("http://localhost:8080/price/PriceList");
        return fragmentView;

        }
    });
}
}

答案 2 :(得分:0)

    ArrayList<> listdata;

public class JSONTaskShare extends AsyncTask<String, String, JSONArray> {


        @Override
        protected JSONArray doInBackground(String... params) {
            return Utility.getJSONArray(url);
        }

        @Override
        protected void onPostExecute(JSONArray result) {


            for (int i = 0; i < result.length(); i++) {
                try {
                    JSONObject row = result.getJSONObject(i);
                    // add here your parsing result in arraylist

                   listdata.add();


                } catch (JSONException e) {
                    e.printStackTrace();

                }

            }
        }
    }


Create an adapter here.

public class PricelistfragmentAdapter extends BaseAdapter {


    Activity activity
    ArrayList<> arrayList;

    private static LayoutInflater inflater = null;

    public TvAdapter(Activity activity, ArrayList<> list) {
        // TODO Auto-generated constructor stub

        this.activity = activity;
        this.arrayList = list;
        inflater = (LayoutInflater) context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public class Holder {
     // set Your view here.
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub


        Holder holder = new Holder();
        View rowView;
        rowView = inflater.inflate(R.layout.list_data, null);
       // set your value here  ...

        return rowView;
    }

}


// Now in your Price Fragment set adapter value.

PricelistfragmentAdapter pricelistfragmentAdapter=new PricelistfragmentAdapter(getActivity,arrayList);
lvShareSummary.setAdapter(pricelistfragmentAdapter);