我对Android和面向对象的代码相当新,所以我可能会忽视一些小事。
我有一个Activity
,其中包含ListView
中的一些自定义Fragment
,通过滑动和标签进行更改。在按特定列表中的特定项目几次后,新的Activity
应该会打开并包含另一个ListView
。
当我运行我的代码时,我可以从我的片段(FactoryScreen.java
)获取第二个活动(StatusFragment.java
),但是FactoryScreen.java
显示为空白Activity
我一直在网上搜索类似案例的论坛,但没有运气。再说一遍,由于我是新手,我不完全确定调试方法。但是,我注意到,包含Activity布局的fragment_factory_screen.xml
完全正常,显示TextView
项目(基本" Hello world"),但显然不是{ {1}}。
StatusFragment.java(FactoryScreen.java从此处通过Intent初始化)
ListView
FactoryScreen.java(要打开的活动,ListView不显示)
import java.util.ArrayList;
import com.example.path.R;
import android.content.Context;
import android.content.Intent;
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.ListView;
import android.widget.Toast;
public class StatusFragment extends Fragment
{
private Context mContext;
private ArrayList<DataItem> data = new ArrayList<DataItem>(); // DataItem contains two strings
ListView myList;
int i = 0;
@Override
public void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mContext = getActivity(); // Get the context of the Activity and therefore the fragment
myList = new ListView (mContext); // Create a ListView
// Fill out elements to go into 'data'
...
// data.add each DataItem variable
...
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the view
View rootView = inflater.inflate(R.layout.fragment_status, container, false);
// (?) Identify myList to be a list to be placed in rootView
myList=(ListView) rootView.findViewById(android.R.id.list);
// Create and set the customised adapter to take data (ArrayList<DataItem>) and format
// each element placed as defined in list_row.xml
CustomAdapter adapter = new CustomAdapter(mContext, R.layout.list_row, data);
myList.setAdapter(adapter);
// When a myList item is clicked, show a Toast.
myList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parentAdapter, View view, int position, long id)
{
// If Item 1 is clicked 3 times, open new Activity
if(id == 1)
{
i++;
if(i == 3)
{
Intent intent = new Intent(mContext, FactoryScreen.class);
i = 0;
startActivity(intent);
}
else
{
...
}
}
else
{
...
}
}
});
return rootView; // onCreateView must return a View variable if displaying a UI.
}
}
fragment_factory_screen.xml(包含FactoryScreen.xml的布局)
import java.util.ArrayList;
import com.example.baseappwithcalibration.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
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.ListView;
import android.widget.Toast;
public class FactoryScreen extends Activity {
private Context mContext;
private ArrayList<DataItem> data = new ArrayList<DataItem>();
ListView myList;
@Override
public void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_factory_screen);
mContext = this; // Get the context of the Activity and therefore the fragment
myList = new ListView (mContext); // Create a ListView
// Create elements to be added to the list
...
// Add elements to the list
...
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the view
View rootView = inflater.inflate(R.layout.fragment_factory_screen, container, false);
// (?) Identify myList to be a list to be placed in rootView
myList=(ListView)findViewById(android.R.id.list);
// Create and set the customised adapter to take data (ArrayList<DataItem>) and format
// each element as defined in list_row.xml
CustomAdapter adapter = new CustomAdapter(mContext, R.layout.list_row, data);
myList.setAdapter(adapter);
// When a myList item is clicked, show a Toast.
myList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parentAdapter, View view, int position, long id)
{
// This is used in the form of Toast.makeText(Context, Text, Duration);
Toast.makeText(FactoryScreen.this, "This is the description of the parameter.", Toast.LENGTH_SHORT).show();
}
});
return rootView; // onCreateView must return a View variable if displaying a UI.
}
}
感谢任何帮助/指导!
答案 0 :(得分:0)
在FactoryScreen中,使用onCreate
方法
myList = new ListView (mContext); // Create a ListView
您不会在布局中添加listView。
如果布局R.layout.fragment_factory_screen包含ListView,您可以使用
检索它myList=(ListView)findViewById(myId);
否则您必须使用代码添加到视图中。
答案 1 :(得分:0)
试试这个
onCreateView()
中的
myList=(ListView)rootView.findViewById(android.R.id.list);
答案 2 :(得分:0)
对于一个活动没有名为onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
的方法。因此,在你的情况下不会执行函数。这就是ListView
不可见的原因。因此,在FactoryScreen活动中,将代码从此方法onCreateView()
移至onCreate()
。
@Override
public void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_factory_screen);
mContext = this; // Get the context of the Activity and therefore the fragment
// (?) Identify myList to be a list to be placed in rootView
myList=(ListView)findViewById(android.R.id.list);
// Create and set the customised adapter to take data (ArrayList<DataItem>) and format
// each element as defined in list_row.xml
CustomAdapter adapter = new CustomAdapter(mContext, R.layout.list_row, data);
myList.setAdapter(adapter);
// When a myList item is clicked, show a Toast.
myList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parentAdapter, View view, int position, long id)
{
// This is used in the form of Toast.makeText(Context, Text, Duration);
Toast.makeText(FactoryScreen.this, "This is the description of the parameter.", Toast.LENGTH_SHORT).show();
}
});
}
}