I am working on an Android application and I am designing a Home layout that uses snippets for contacts, messages and some other things. Therefore, in the contact fragment, I want to display a list of contacts with people. I can get a listview working in the listactivity class, but with this fragmentation I cannot get it to work. I have an error that says: "The setListAdapter (ListAdapter) method is undefined for the type new Runnable () {}" I just don't know what I need to change to make this work.
Here is what I have done so far.
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="...";
SectionsPagerAdapter mSectionsPagerAdapter;
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();
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
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() {
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;
}
}
public static class DummySectionFragment extends 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");
}
return rootView;
}
}
class LoadAllProducts extends AsyncTask<String, String, String>{
public LoadAllProducts(){
}
@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) {
e1.printStackTrace();
}
prm.add(new BasicNameValuePair("query","friends"));
JSONObject jsn=jsonparser.makeHttpRequest(url_friends, "POST", prm);
try {
JSONArray array = jsn.getJSONArray("options");
for (int i = 0; i < array.length(); i++) {
JSONObject c = array.getJSONObject(i);
String id = c.getString("contact_id");
String name = c.getString("name");
String picture=c.getString("picture");
Log.i("picture_url", picture);
HashMap<String, String> map = new HashMap<String, String>();
map.put("contact_id", id);
map.put("name", name);
map.put("picture", picture);
productsList.add(map);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
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 });
setListAdapter(adapter);
}
});
}
}
}
source
share