2013-07-17 41 views
0

我正在研究一個android應用程序,並且我設計了使用片段進行聯繫人,消息和其他一些事情的Home佈局。所以在聯繫人片段上,我想顯示聯繫人列表。我可以得到一個列表視圖在listactivity類下工作,但是這個fragmentactivity我無法讓它工作。我有一個錯誤,說:「方法setListAdapter(ListAdapter)是未定義的類型新的Runnable(){}」我只是不知道我需要修改才能得到這個工作。讓setlistadapter正常工作

這是我到目前爲止所做的。

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Locale; 

import org.apache.http.NameValuePair; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.content.Context; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 


public class FragTest extends FragmentActivity { 
    ArrayList<HashMap<String, String>> productsList; 
    JSONParser jsonparser=new JSONParser(); 
    String userName; 
    public static String url_friends="..."; 
    public static String url_id="..."; 

    /** 
    * The {@link android.support.v4.view.PagerAdapter} that will provide 
    * fragments for each of the sections. We use a 
    * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which 
    * will keep every loaded fragment in memory. If this becomes too memory 
    * intensive, it may be best to switch to a 
    * {@link android.support.v4.app.FragmentStatePagerAdapter}. 
    */ 
    SectionsPagerAdapter mSectionsPagerAdapter; 

    /** 
    * The {@link ViewPager} that will host the section contents. 
    */ 
    ViewPager mViewPager; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_frag_test); 

     Intent intent=getIntent(); 
     userName=SaveSharedPreference.getUserName(FragTest.this); 
     productsList = new ArrayList<HashMap<String, String>>(); 
     new LoadAllProducts().execute(); 
     // Create the adapter that will return a fragment for each of the three 
     // primary sections of the app. 
     mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.pager); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    /** 
    * A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
    * one of the sections/tabs/pages. 
    */ 
    public class SectionsPagerAdapter extends FragmentPagerAdapter { 

     public SectionsPagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      // getItem is called to instantiate the fragment for the given page. 
      // Return a DummySectionFragment (defined as a static inner class 
      // below) with the page number as its lone argument. 
      Fragment fragment = new DummySectionFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1); 
      fragment.setArguments(args); 
      return fragment; 
     } 

     @Override 
     public int getCount() { 
      // Show 3 total pages. 
      return 3; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      Locale l = Locale.getDefault(); 
      switch (position) { 
      case 0: 
       return getString(R.string.title_section1).toUpperCase(l); 
      case 1: 
       return getString(R.string.title_section2).toUpperCase(l); 
      case 2: 
       return getString(R.string.title_section3).toUpperCase(l); 
      } 
      return null; 
     } 
    } 

    /** 
    * A dummy fragment representing a section of the app, but that simply 
    * displays dummy text. 
    */ 
    public static class DummySectionFragment extends Fragment { 
     /** 
     * The fragment argument representing the section number for this 
     * fragment. 
     */ 
     public static final String ARG_SECTION_NUMBER = "section_number"; 

     public DummySectionFragment() { 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false); 
      TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label); 
      if(getArguments().getInt(ARG_SECTION_NUMBER)==1){ 
       dummyTextView.setText("Contacts"); 
      } 
      if(getArguments().getInt(ARG_SECTION_NUMBER)==2){ 
       dummyTextView.setText("Chat"); 
      } 
      if(getArguments().getInt(ARG_SECTION_NUMBER)==3){ 
       dummyTextView.setText("Messages"); 
      } 
      //dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER))); 
      return rootView; 
     } 
    } 

    class LoadAllProducts extends AsyncTask<String, String, String>{ 

     public LoadAllProducts(/*Context context, View rootView, ArrayList<HashMap<String, String>> llistatEntrades, ListView lv*/){ 

     } 
     @Override 
     protected String doInBackground(String... params) { 
      List<NameValuePair> param=new ArrayList<NameValuePair>(); 
      param.add(new BasicNameValuePair("username",userName)); 
      JSONObject json=jsonparser.makeHttpRequest(url_id, "POST", param); 
      Log.d("Ids", json.toString()); 

      List<NameValuePair> prm=new ArrayList<NameValuePair>(); 
      try { 
       prm.add(new BasicNameValuePair("user_id",json.getString("user_id"))); 
      } catch (JSONException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
      prm.add(new BasicNameValuePair("query","friends")); 

      JSONObject jsn=jsonparser.makeHttpRequest(url_friends, "POST", prm); 
      //Log.d("Friends", jsn.toString()); 
      try { 

       JSONArray array = jsn.getJSONArray("options"); 

       for (int i = 0; i < array.length(); i++) { 
        // Log.i("name", array.getJSONObject(i).getString("name")); 
        JSONObject c = array.getJSONObject(i); 

        // Storing each json item in variable 
        String id = c.getString("contact_id"); 
        String name = c.getString("name"); 
        String picture=c.getString("picture"); 
        Log.i("picture_url", picture); 
        // creating new HashMap 
        HashMap<String, String> map = new HashMap<String, String>(); 

        // adding each child node to HashMap key => value 
        map.put("contact_id", id); 
        map.put("name", name); 
        map.put("picture", picture); 

        // adding HashList to ArrayList 
        productsList.add(map); 
       } 



      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 
     protected void onPostExecute(String file_url) { 
      // dismiss the dialog after getting all products 

      // updating UI from Background Thread 
      runOnUiThread(new Runnable() { 
       public void run() { 

        ListAdapter adapter = new SimpleAdapter(
          FragTest.this, productsList, 
          R.layout.list_item, new String[] { "contact_id","name", "picture"}, 
          new int[] { R.id.pid, R.id.name, R.id.profilepicture }); 
        // updating listview 
        setListAdapter(adapter); 
       } 
      }); 

     } 
    } 

} 

回答

0

你有沒有在你的XML列表視圖?

這可能幫助:

final ArrayList<HashMap<String, String>> productsList = new ArrayList<HashMap<String, String>>(); 
    final ListAdapter adapter; 
    final ListView myList= getView().findViewById(android.R.id.list); 


    runOnUiThread(new Runnable() { 
     public void run() { 

      adapter = new SimpleAdapter(
        FragTest.this, productsList, 
        R.layout.list_item, new String[] { "contact_id","name", "picture"}, 
        new int[] { R.id.pid, R.id.name, R.id.profilepicture }); 
      // updating listview 
      myList.setAdapter(adapter); 
       } 
      }); 
     } 
    }); 
+0

嘿,感謝您的答覆。是的,我在我的xml中有一個listview。當我嘗試你的代碼時,我得到了getView的錯誤,「對於類型fragtest未定義」。 –

+0

嘗試在oncreate上添加以上內容:static Fragment testFragment; 然後在你的onCreateView中加入:testFragment = this; 然後像這樣使用它: final ListView myList = testFrag.getView()。findViewById(android.R.id.list); – LordMarty