我从一个片段中的JSON设置了一个列表视图,我试图在单击一个项目时将视图更改为一个新片段,还将单击的项目信息传递给新片段以发送到PHP服务器以获取响应(生成新的列表视图)。我是片段的新手,还没有很好的解释如何将其集成到onItemClick()函数中。
channelList.java
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
public class ConnectFragment extends Fragment {
ListView listView;
ArrayList <Channels> channelList;
ChannelListAdapter adapter;
public ConnectFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_connect, container, false);
listView = (ListView) rootView.findViewById(R.id.listView);
channelList = new ArrayList<Channels>();
getJSON("http://192.168.43.149/schoolTV/channelList.php");
return rootView;
}
private void getJSON(final String urlWebService) {
String result;
// InputStream is=null;
final ProgressDialog[] dialog = new ProgressDialog[1];
class GetJSON extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
//dialog[0] = new ProgressDialog(getActivity());
//dialog[0].setMessage("Loading, please wait");
//dialog[0].setTitle("Connecting server");
//dialog[0].show();
//dialog[0].setCancelable(false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//Toast.makeText(getContext().getApplicationContext(), s, Toast.LENGTH_LONG).show();
try {
loadIntoListView(s);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected String doInBackground(Void... voids) {
try {
URL url = new URL(urlWebService);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String json;
while ((json = bufferedReader.readLine()) != null) {
sb.append(json + "\n");
}
return sb.toString().trim();
} catch (Exception e) {
return null;
}
}
}
GetJSON getJSON = new GetJSON();
getJSON.execute();
}
private void loadIntoListView(String json) throws JSONException {
JSONArray jsonArray = new JSONArray(json);
//String[] channels = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
//channels[i] = obj.getString("name");
Channels channel = new Channels();
channel.setTitle(obj.getString("channelName"));
channel.setDescription(obj.getString("channelDescriptipn"));
//channel.setDateCreated(obj.get("createdOn"));
channel.setImage(obj.getString("logoLocation"));
channelList.add(channel);
}
//ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1);
ChannelListAdapter adapter = new ChannelListAdapter(getActivity(), R.layout.jsonparsedata_item, channelList);
listView.setAdapter(adapter);
listView.setOnClickListener();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
}
});
}
}
答案 0 :(得分:0)
更改为新片段:
FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
ft.add(R.id.frame_layout,new MainFragment()).commit();
frame_layout是容器视图ID,MainFragment是要添加的片段的名称。
...........................................
在新片段中,创建一个与要获取其信息的项目相同的对象。在您的onClick方法中,引用新片段的对象并将信息放入其中。