我正在构建一个使用Fragments
来显示UI的Master / Detail应用程序。我ListFragment
显示List
,我希望在其上方EditText
输入将从网络API中提取的数据关键字。我的问题是EditText
没有显示在List
ListFragment
上方<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="@+id/myEditText"
android:inputType="text"
android:hint="@string/hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginTop="20dp"/>
</LinearLayout>
</LinearLayout>
。以下是我的一些代码:
Display_Fragment.xml
package com.brianstacks.fragmentandfilefundamentals.fragments;
import android.app.AlertDialog;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.brianstacks.fragmentandfilefundamentals.R;
/**
* Created by Brian Stacks
* on 11/24/14
* for FullSail.edu.
*/
public class MyListFragment extends ListFragment {
public static final String TAG = "MyListFragment.TAG";
public static MyListFragment newInstance() {
MyListFragment frag = new MyListFragment();
return frag;
}
@Override
public void onActivityCreated(Bundle _savedInstanceState) {
super.onActivityCreated(_savedInstanceState);
String[] teams = getResources().getStringArray(R.array.teamList);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, teams);
setListAdapter(adapter);
}
public void onListItemClick(ListView _l, View _v, int _position, long _id) {
String team = (String)_l.getItemAtPosition(_position);
new AlertDialog.Builder(getActivity())
.setTitle(R.string.team)
.setMessage(getString(R.string.selected, team))
.setPositiveButton(R.string.ok, null)
.show();
}
}
MyListFragment.java
/*
* Brian Stacks
* Java 2
* 11-24-2014
* */
package com.brianstacks.fragmentandfilefundamentals;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import com.brianstacks.fragmentandfilefundamentals.fragments.DetailFragment;
import com.brianstacks.fragmentandfilefundamentals.fragments.MyListFragment;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager mgr = getFragmentManager();
FragmentTransaction trans = mgr.beginTransaction();
MyListFragment frag = MyListFragment.newInstance();
trans.replace(R.id.fragment_container, frag, MyListFragment.TAG);
DetailFragment dFrag = DetailFragment.newInstance();
trans.replace(R.id.fragment_container2,dFrag,DetailFragment.TAG);
trans.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
// Async task method to do network action in
private class MyTask extends AsyncTask<String ,String ,String> {
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... params) {
return "";
}
@Override
protected void onPostExecute(String result) {
}
@Override
protected void onProgressUpdate(String... values) {
}
}
// method to get the data from ASYNC task
private void requestData(String uri) {
MyTask task = new MyTask();
task.execute(uri);
}
// method to check internet connectivity
protected boolean isOnline(){
ConnectivityManager cm =(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
return netInfo != null && netInfo.isConnectedOrConnecting();
}
}
MainActivity.java
List
它显示的是EditText
,但没有显示我的{{1}}
答案 0 :(得分:1)
你在哪里调用了display fragment.xml ???
答案 1 :(得分:0)
我在ListFragment
public class MyListFragment extends ListFragment {
public static final String TAG = "MyListFragment.TAG";
public static MyListFragment newInstance() {
MyListFragment frag = new MyListFragment();
return frag;
}
@Override
public void onActivityCreated(Bundle _savedInstanceState) {
super.onActivityCreated(_savedInstanceState);
String[] teams = getResources().getStringArray(R.array.teamList);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, teams);
setListAdapter(adapter);
}
public void onListItemClick(ListView _l, View _v, int _position, long _id) {
String team = (String)_l.getItemAtPosition(_position);
new AlertDialog.Builder(getActivity())
.setTitle(R.string.team)
.setMessage(getString(R.string.selected, team))
.setPositiveButton(R.string.ok, null)
.show();
}
}
所以我改变了这一点并添加了onCreateView
t:
package com.brianstacks.fragmentandfilefundamentals.fragments;
import android.app.AlertDialog;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.brianstacks.fragmentandfilefundamentals.R;
/**
* Created by Brian Stacks
* on 11/24/14
* for FullSail.edu.
*/
public class MyListFragment extends ListFragment {
public static final String TAG = "MyListFragment.TAG";
public static MyListFragment newInstance() {
MyListFragment frag = new MyListFragment();
return frag;
}
@Override
public View onCreateView(LayoutInflater _inflater, ViewGroup _container,
Bundle _savedInstanceState) {
// Create and return view for this fragment.
View view = _inflater.inflate(R.layout.display_fragment, _container, false);
return view;
}
@Override
public void onActivityCreated(Bundle _savedInstanceState) {
super.onActivityCreated(_savedInstanceState);
String[] teams = getResources().getStringArray(R.array.teamList);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, teams);
setListAdapter(adapter);
}
public void onListItemClick(ListView _l, View _v, int _position, long _id) {
String team = (String)_l.getItemAtPosition(_position);
new AlertDialog.Builder(getActivity())
.setTitle(R.string.team)
.setMessage(getString(R.string.selected, team))
.setPositiveButton(R.string.ok, null)
.show();
}
}
修正了一切!