首页 > 解决方案 > 设置 onItemClick 将 ListView 项目数据传递给片段

问题描述

我在片段中设置了 JSON 的列表视图,我试图在单击项目时将视图更改为新片段,并将单击的项目信息传递给新片段以发送到 PHP 服务器以获取响应(生成一个新的列表视图)。我是 Fragment 的新手,还没有找到一个很好的解释来说明如何将它集成到我的 onItemClick() 函数中。

频道列表.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) {


        }
    });
}

}

标签: javaandroidandroid-listviewfragment

解决方案


更改为新片段:

FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
        ft.add(R.id.frame_layout,new MainFragment()).commit();

frame_layout 是容器视图 ID,MainFragment 是您要添加的片段的名称。

………………………………………………………………………………………………………………………………………………

在您的新片段中,创建一个与您想要获取信息的项目相同的对象。在您的 onClick 方法中,引用新片段的该对象并将信息放入其中。


推荐阅读