我正在开发一个Android应用程序,我正在设计Home布局,它使用片段进行联系,消息和其他一些事情。因此,在联系人片段上,我想显示联系人列表。我可以在listactivity类下使用listview,但是由于这个片段活动,我无法让它工作。我有一个错误,说“方法setListAdapter(ListAdapter)未定义为新类型Runnable(){}”我只是不知道我需要修改什么来使其工作。
这是我到目前为止所做的。
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 :(得分:0)
你的xml中有listview吗?
这可能会有所帮助:
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);
}
});
}
});