2016-12-28 117 views
-1

我想在按鈕單擊片段後顯示列表視圖。從JsonTask中顯示片段中的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> 

沒有點擊該按鈕列表視圖工作正常。但是我想在點擊按鈕後顯示列表視圖。

+0

什麼是'v'並在返回從'onCreateView'看法? –

+2

保持與列表視圖有關的每一件事onclick,只需將適配器設置爲listview按鈕onclick – Redman

+0

你在哪裏設置適配器的列表? – Ezio

回答

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"); 

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

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