我想创建一个与Gmail应用程序构思相似的应用程序(导航抽屉选择类别,如收件箱或草稿和列表碎片,以显示列表中包含图像的电子邮件)。
现在我做了以下事情:
一个MainActivity.java:
public class MainActivity extends ActionBarActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] sections_array;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
........................
.............................
........a bunch of code...............
.............................
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
ListFragment lfragment = null;
switch (position) {
case 0:
lfragment = new NewsFragment();
break;
case 1:
fragment = new TvFragment();
...............................
.............................
.................a bunch of code..............
.............................
public static class NewsFragment extends ListFragment {
private ListView listView;
private NewsAdapter newsAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceStatee){
View rootView = inflater.inflate(R.layout.fragment_news, container, false);
listView = (ListView) rootView.findViewById(android.R.id.list);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// URL to the JSON data
String strUrl = "http://wptrafficanalyzer.in/p/demo1/first.php/countries";
// Creating a new non-ui thread task to download json data
GetJSON downloadTask = new GetJSON();
// Starting the download process
downloadTask.execute(strUrl);
}
}
GetJson:
/** AsyncTask to download json data */
public class GetJSON extends AsyncTask<String, Integer, String>{
String data = null;
private ListView listView;
private NewsAdapter newsAdapter;
@Override
protected String doInBackground(String... url) {
try{
data = downloadUrl(url[0]);
}catch(Exception e){
Log.d("Background Task",e.toString());
}
return data;
}
@Override
protected void onPostExecute(String result) {
try {
JSONObject jObject;
jObject = new JSONObject(result);
JSONArray jCountries = jObject.getJSONArray("countries");
ArrayList<String> stringArrayList = new ArrayList<String>();
for (int i=0; i < jCountries.length(); i++)
{
try {
JSONObject oneObject = jCountries.getJSONObject(i);
// Pulling items from the array
stringArrayList.add(oneObject.getString("countryname"));
/*
**How to use NewsAdapter here with stringArrayList as data to be used**
*/
} catch (JSONException e) {
// Oops
}
}
String [] stringArray = stringArrayList.toArray(new String[stringArrayList.size()]);
} catch (JSONException e) {
e.printStackTrace();
}
}
/** A method to download json data from url */
private String downloadUrl(String strUrl) throws IOException{
String data = "";
InputStream iStream = null;
try{
URL url = new URL(strUrl);
// Creating an http connection to communicate with url
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
// Connecting to url
urlConnection.connect();
// Reading data from url
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while( ( line = br.readLine()) != null){
sb.append(line);
}
data = sb.toString();
br.close();
}catch(Exception e){
Log.d("Exception while downloading url", e.toString());
}finally{
iStream.close();
}
return data;
}
}
NewsAdapter.java :
public class NewsAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public NewsAdapter(Context context, String[] values) {
super(context, R.layout.drawer_list_item, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
................................
...............
...............
...............
onPostExecute方法中的stringArrayList 现在包含我想要使用NewsAdapter在listfragment中填充的数据,但问题是如何在GetJSON类中使用NewsAdapter,因为我尝试了几种方法但没有工作?
答案 0 :(得分:1)
试试这个:
界面:
public interface ILoadDataListener {
public void onComplete(ArrayList<String> data);
}
您的片段:
public static class NewsFragment extends ListFragment implements ILoadDataListener {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// URL to the JSON data
String strUrl = "http://wptrafficanalyzer.in/p/demo1/first.php/countries";
// Creating a new non-ui thread task to download json data
GetJSON downloadTask = new GetJSON(this); //
// Starting the download process
downloadTask.execute(strUrl);
}
@Override
public void onComplete(ArrayList<String> result) {
YourAdapter adapter = new YourAdapter(..., result);
yourListView.setAdapter(adapter);
}
}
您的 AsyncTask
public class GetJSON extends AsyncTask<String, Integer, ArrayList<String>>{ // return an ArrayList of String
//.... Your declaration
private ILoadDataListener mListener;
public GetJSON(ILoadDataListener listener) {
this.mListener = listener;
}
@Override
public void onPostExecute(ArrayList<String> result) {
if (mListener != null) {
mListener.onComplete(result);
}
}
}
您的适配器
使用ArrayList<String> values;
代替String[] values;