【发布时间】:2014-06-24 05:51:05
【问题描述】:
我对 Android 和面向对象的代码还很陌生,所以我可能会忽略一些小问题。
我有一个Activity,其中包含Fragments 中的一些自定义ListViews,通过滑动和标签更改。按下特定列表中的特定项目几次后,应该会打开一个新的 Activity 并包含另一个 ListView。
当我运行我的代码时,我可以从我的 Fragment (StatusFragment.java) 打开第二个 Activity (FactoryScreen.java),但是 FactoryScreen.java 显示为空白 Activity。
我一直在网上论坛中搜索类似案例,但没有任何运气。同样,由于我是新手,所以我不完全确定调试方法。但是,我注意到包含 Activity 布局的fragment_factory_screen.xml 完全可以显示TextView 项目(带有基本的“Hello world”),但显然不是ListView。
StatusFragment.java(FactoryScreen.java 通过 Intent 从这里初始化)
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.
}
}
FactoryScreen.java(Activity要打开,ListView不显示)
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.
}
}
fragment_factory_screen.xml(包含 FactoryScreen.xml 的布局)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical" >
<ListView
android:drawSelectorOnTop="false"
android:id="@android:id/list"
android:layout_height="0dip"
android:layout_weight="1"
android:layout_width="match_parent" >
</ListView>
</LinearLayout>
感谢任何帮助/指导!
【问题讨论】:
标签: android android-activity android-listview