2016-12-28 10 views
-1

フラグメントをクリックした後にリストビューを表示したい。JsonTaskのフラグメントのリストビューを表示する

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> 

ボタンなしのリストビューが正常に動作してクリックします。しかし、私はボタンをクリックした後にリストビューを表示したい。答え

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

'v'とonCreateView''からビューを返すとは何ですか?:ので、あなたのコードは、このなりますonCreateViewフラグメント内部の子ビューは、私たちが で持っているビューオブジェクトでinitilizeする必要がありますか –

+2

リストビューに関連するすべてのことをonclick側に残してください。ボタン内のリストビューにアダプタを設定してくださいonclick – Redman

+0

リストのアダプタはどこに設定していますか? – Ezio

答えて

1

おかげであなたがアンドロイドで初心者ですように思えます。このようにフラグメントコードを書くことはできません。

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

のためのレッドマンへ

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); 
関連する問題