2016-08-23 3 views
-1

私は、アクティビティがサーバーからList1とList2を取得する非同期タスクで構成されている1つのアプリケーションで作業しています...しかし、そのアプリケーションを開いたときにList2はList1の下に読み込まれ、List2は、だから私はList1を取得するためにスクロールする必要があります...どのようにこの問題を解決するには?事前のおかげでandroid listview layout error

xmlファイル

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 

    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:fitsSystemWindows="true"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingTop="2dp" 
     android:paddingLeft="2dp" 
     android:paddingRight="2dp" 
     android:weightSum="1" 
     > 

      <ListView 
      android:layout_width="match_parent" 
      android:layout_height="1550dp" 
      android:id="@+id/list1" 
      />    

      <ListView 
      android:layout_width="match_parent" 
      android:layout_height="1550dp" 
      android:id="@+id/list2" 
      />   

    </LinearLayout> 
</ScrollView> 

</LinearLayout> 

javaファイル

public class Mysubscription extends AppCompatActivity { 

    static Mysubscription INSTANCE; 
    public static String name2 = "name"; 
    public static String books2 = "books"; 
    public static String registration_fee = "registration_fee"; 

    private Toolbar topToolBar1; 
    LinearLayout a; 
    String Subsribed; 
    Button Renew; 
    JSONObject jsonobject; 
    JSONArray jsonarray12; 
    private Toolbar topToolBar; 
    ListView listview3; 
    mysub1 adapter; 
    mysub2 adapter2; 
    ProgressDialog mProgressDialog; 
    ArrayList<HashMap<String, String>> arraylist; 
    private JSONArray gg; 
    private JSONObject obj; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // Get the view from listview_main.xml 
     setContentView(R.layout.subscribe1); 
     INSTANCE=this;    

     new DownloadJSON1().execute(); 
     new DownloadJSON2().execute(); 
    } 

    // DownloadJSON AsyncTask 

    public class DownloadJSON1 extends AsyncTask<Void, Void, Void> { 
     API o9=new API();   

     @Override 
     protected Void doInBackground(Void... params) { 

      // Create an array 
      arraylist = new ArrayList<HashMap<String, String>>(); 
      // Retrieve JSON Objects from the given URL address 

      Servicehandler json = new Servicehandler(); 
      jsonarray12 = json.getJSONFromUrl(o9.subscription1); 

      try { 
       // Locate the array name in JSON 

       for (int i = 0; i < jsonarray12.length(); i++) { 
        HashMap<String, String> map = new HashMap<String, String>(); 
        jsonobject = jsonarray12.getJSONObject(i); 
        // Retrive JSON Objects 
        map.put("name", jsonobject.getString("name")); 
        map.put("books", jsonobject.getString("books")); 
        map.put("registration_fee", jsonobject.getString("registration_fee")); 

        // Set the JSON Objects into the array 
        arraylist.add(map); 
       } 

      } catch (JSONException e) { 
       Log.e("Error", e.getMessage()); 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void args) { 
      // Locate the listview in listview_main.xml 
      listview3 = (ListView) findViewById(R.id.list1); 
      // Pass the results into ListViewAdapter.java 
      adapter = new mysub1(Mysubscription.this, arraylist); 
      // Set the adapter to the ListView 
      listview3.setAdapter(adapter); 
      // Close the progressdialog 
     } 
    } 

    public class DownloadJSON2 extends AsyncTask<Void, Void, Void> { 
     API o9=new API(); 
     @Override 
     protected void onPreExecute() { 
     } 

     @Override 
     protected Void doInBackground(Void... params) { 

      // Create an array 
      arraylist = new ArrayList<HashMap<String, String>>(); 
      // Retrieve JSON Objects from the given URL address 

      Servicehandler json = new Servicehandler(); 
      jsonarray12 = json.getJSONFromUrl(o9.subscription1); 

      try { 
       // Locate the array name in JSON 

       for (int i = 0; i < jsonarray12.length(); i++) { 
        HashMap<String, String> map = new HashMap<String, String>(); 
        jsonobject = jsonarray12.getJSONObject(i); 
        // Retrive JSON Objects 
        map.put("name", jsonobject.getString("name")); 
        map.put("books", jsonobject.getString("books")); 
        map.put("registration_fee", jsonobject.getString("registration_fee")); 

        // Set the JSON Objects into the array 
        arraylist.add(map); 
       } 

      } catch (JSONException e) { 
       Log.e("Error", e.getMessage()); 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void args) { 
      // Locate the listview in listview_main.xml 
      listview3 = (ListView) findViewById(R.id.list2); 
      // Pass the results into ListViewAdapter.java 
      adapter2 = new mysub2(Mysubscription.this, arraylist); 
      // Set the adapter to the ListView 
      listview3.setAdapter(adapter2); 
      // Close the progressdialog 
     } 
    } 

} 
+0

シェアも、あなたのソースコードを、どのように – ddb

+0

ソースコードが追加され、それらのリストを読み込むか... –

+0

は、Activityクラスに異なるListViewの変数を宣言し、OnCreateの方法で –

答えて

0

両方のリストがロードされたときに、追跡するコールバックを行います。

これが実行されたら、あなたに適したものに応じて、scrollView.fullScroll(ScrollView.FOCUS_UP);またはscrollView.smoothScrollTo(0,0)を呼び出します。もちろん

はあなたScrollviewにIDを与えるとscrollView = (ScrollView)getViewById(R.id.myScrollView);

0

変更レイアウト・ファイル

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 
<ListView 
    android:id="@+id/list1" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" /> 
<ListView 
    android:id="@+id/list2" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" /></LinearLayout> 

リストビューでそれを呼び出す前に、あなたのscrollView変数にそれを得るので、必要はScrollViewに宣言していないために、スクロール可能なデフォルトであります。これがあなたに役立つことを願っています。すべてのベスト