如何在片段textview中显示列表项名称

时间:2016-08-01 10:27:47

标签: java android android-fragments android-recyclerview

我想在片段textview中显示列表项名称。

我该怎么做?

这是我的代码:

  1. Title.java

     public class Title extends AppCompatActivity implements          OnVersionNameSelectionChangeListener{
    private static final String TAG = Title.class.getSimpleName();
    
    String str, arr[];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    
    setContentView(R.layout.activity_title);
    str = getIntent().getExtras().getString("string","defaultValue");
    //Toast.makeText(getApplicationContext(), str , Toast.LENGTH_SHORT).show();
    
    // Check whether the Activity is using the layout verison with the fragment_container
    // FrameLayout and if so we must add the first fragment
    
    if (findViewById(R.id.fragment_container) != null){
    
        // However if we are being restored from a previous state, then we don't
        // need to do anything and should return or we could end up with overlapping Fragments
        if (savedInstanceState != null){
            return;
        }
    
        // Create an Instance of Fragment
        VersionsFragment versionsFragment = new VersionsFragment();
        Bundle bundle = new Bundle();
        bundle.putString("key", str);
        //versionsFragment.setArguments(getIntent().getExtras());
        versionsFragment.setArguments(bundle);
                                                                                        getFragmentManager().beginTransaction().add(R.id.fragment_container, versionsFragment).commit();
    }
    }
    
     @Override
    public void OnSelectionChanged(int versionNameIndex, String[] answer) {
    DescriptionFragment descriptionFragment = (DescriptionFragment) getFragmentManager()
            .findFragmentById(R.id.version_description);
    
    if (descriptionFragment != null){
        // If description is available, we are in two pane layout
        // so we call the method in DescriptionFragment to update its content
        // Bundle args = new Bundle();
        // args.putString("key", str);
        // args.putStringArray("answer", answer );
        //args.putInt(DescriptionFragment.KEY_POSITION,versionNameIndex);
        //descriptionFragment.setArguments(args);
        setArray(answer);
        //setStr(str);
        descriptionFragment.setDescription(versionNameIndex);
    } else {
        DescriptionFragment newDescriptionFragment = new DescriptionFragment();
        Bundle args = new Bundle();
        //args.putString("key", str);
        //args.putStringArray("answer", answer );
        setArray(answer);
        args.putInt(DescriptionFragment.KEY_POSITION,versionNameIndex);
    
        newDescriptionFragment.setArguments(args);
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
    
        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the backStack so the User can navigate back
        fragmentTransaction.replace(R.id.fragment_container,newDescriptionFragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }
        }
       public String getStr(){
       return str;
        }
    
        public void setArray(String[] s)
        {
           arr = s;
    //Log.d(TAG, "setArray: " + arr[0]);
        }
    
       public String[] getArray(){
       return arr;
       }
       }
    
  2. DescriptionFragment.java

    public class DescriptionFragment extends Fragment {
    private static final String TAG = DescriptionFragment.class.getSimpleName();
    final static String KEY_POSITION = "position";
    int mCurrentPosition = -1;
    String[] mVersionTitle;
    String[] mVersionDescriptions;
    TextView mVersionTitleTextView;
    TextView mVersionDescriptionTextView;
    
    public DescriptionFragment() {
    
    }
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Title t = (Title) getActivity();
        mVersionDescriptions = t.getArray();
        //Log.d(TAG , "onActivityCreated: " + mVersionDescriptions.length);
    }
    
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
        // If the Activity is recreated, the savedInstanceStare Bundle isn't empty
        // we restore the previous version name selection set by the Bundle.
        // This is necessary when in two pane layout
    
        String strtext = getArguments().getString("title");
    
        if (savedInstanceState != null) {
            mCurrentPosition = savedInstanceState.getInt(KEY_POSITION);
            //Log.d(TAG, "Show last click position: "+ KEY_POSITION);
        }
                View view = inflater.inflate(R.layout.fragment_description, container, false);
                mVersionTitleTextView = (TextView) view.findViewById(R.id.textView_Title);
                mVersionTitleTextView.setText(strtext);
                mVersionDescriptionTextView = (TextView) view.findViewById(R.id.version_description);
                return view;
    }
    
    
    @Override
    public void onStart() {
        super.onStart();
        Bundle args = new Bundle();
        args = getArguments();
    
    
        if (args != null) {
            // Set description based on argument passed in
          //  setTitle(args.getInt(KEY_POSITION));
            setDescription(args.getInt(KEY_POSITION));
    
        } else if (mCurrentPosition != -1) {
            // Set description based on savedInstanceState defined during onCreateView()
           // setTitle(mCurrentPosition);
            setDescription(mCurrentPosition);
        }
    }
    
    public void setDescription(int descriptionIndex) {
        Title t = (Title) getActivity();
       // mVersionTitle = t.getArray();
        mVersionDescriptions = t.getArray();
        //mVersionTitleTextView.setText(mVersionTitle[descriptionIndex]);
        mVersionDescriptionTextView.setText(mVersionDescriptions[descriptionIndex]);
        mCurrentPosition = descriptionIndex;
        Log.d(TAG, "onSaveInstanceState: " + mCurrentPosition);
    }
    
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    
        // Save the current description selection in case we need to recreate the fragment
        outState.putInt(KEY_POSITION, mCurrentPosition);
        //Log.d(TAG, "onSaveInstanceState: " + mCurrentPosition);
          }
        }
    
  3. versionFragment.java

      public class VersionsFragment extends ListFragment {
      private static final String TAG = VersionsFragment.class.getSimpleName();
      String[] title = new String[0];
      String[] description = new String[0];
      String table=new String();
      public VersionsFragment() {
      }
    
    
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    
    String myValue = this.getArguments().getString("key");
    
    Title q = (Title) getActivity();
    table = q.getStr();
    //Log.d(TAG, "onActivityCreated: " + table);
    
    if(table.equals("Paragraph Writing"))
        table="paragraph";
    else if(table.equals("Story Writing"))
        table="story";
    table = table.toLowerCase();
    new MyTask().execute();
    }
    
    class MyTask extends AsyncTask<String, Integer, String[]> {
    
    @Override
    protected String[] doInBackground(String... params) {
        String[] versionName = new String[0];
        ArrayList<String> scripts = new ArrayList<String>();
        ArrayList<String> scripts2 = new ArrayList<String>();
        try {
            //Log.d(VersionsFragment.class.getSimpleName(),"http://192.168.1.105/android/phpcall.php?table="+table );
            URL url = new URL("http://192.168.1.6/ps/phpcall.php?table="+table);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            String strJson = "";
            String line;
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((line = br.readLine()) != null) {
                strJson += line;
            }
            //Log.d(VersionsFragment.class.getSimpleName(), "JSON string =" + strJson);
            JSONArray jArray = new JSONArray(strJson);
            //JSONObject jsonObj = new JSONObject(strJson);
            // Getting JSON Array node
            //JSONArray jArray = jsonObj.getJSONArray(strJson);
    
            for (int i = 0; i < jArray.length(); i++) {
    
                JSONObject jObject = jArray.getJSONObject(i);
    
                String name = jObject.getInt("id") + ". " + jObject.getString("title");
                scripts.add(name);
    
                Bundle bundle = new Bundle();
                bundle.putString("title", name);
                // set Fragmentclass Arguments
                DescriptionFragment fragobj = new DescriptionFragment();
                fragobj.setArguments(bundle);
    
                String name2 = jObject.getString("description");
                scripts2.add(name2);
            }
            versionName = scripts.toArray(new String[scripts.size()]);
           // title = scripts.toArray(new String[scripts.size()]);
            description = scripts2.toArray(new String[scripts.size()]);
    
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return versionName;
    }
    
    protected void onPostExecute(String[] result){
        //for(int i=0;i<result.length;i++)
            //Log.d(VersionsFragment.class.getSimpleName(), result[i]);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, result);
        setListAdapter(adapter);
    }
    }
    
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
    OnVersionNameSelectionChangeListener listener = (OnVersionNameSelectionChangeListener) getActivity();
    listener.OnSelectionChanged(position,description);
        }
    }
    

0 个答案:

没有答案