0

我是安卓新手,急需帮助。我已经成功创建了一个带有图像的 ListView。我的任务是当我单击任何列表项时,它的描述应该出现在平板电脑中 ListView 的右侧。毫无疑问,这可以在片段的帮助下完成。但是要使用片段 Main 类应该扩展 Fragment 类,但我的问题是我的类已经在扩展 ListActivity 类。我现在该怎么办?

我的项目中有三个 java 文件:
MainActivity.java [这应该是第一个片段]、
CustomArrayAdapter.java 和
Fragment2.java [这应该是第二个片段]

这是代码

MainActivity.java

package com.example.mypractice;
import android.app.ListActivity;
import android.os.Bundle; 
public class MainActivity extends ListActivity {
String[] menu = {
"Internet",
"Movies",
"Songs",
"Games",
"News",
"Menu",
"Bill",
"Call Waiter"
 };
Integer[] imageIDs = {
R.drawable.pancake,
};
@Override
public void onCreate(Bundle savedInstanceState) {    
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomArrayAdapter adapter = new
CustomArrayAdapter(this, menu, imageIDs);
setListAdapter(adapter);
}
}

CustomArrayAdapter.java

package com.example.mypractice;
import android.app.Activity;    
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomArrayAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] menu;
private final Integer[] imageIds;
public CustomArrayAdapter(Activity context,
String[] presidents, Integer[] imageIds) {
super(context, R.layout.lvrowlayout2, presidents);
this.context = context;
this.menu = presidents;
this.imageIds = imageIds;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
Log.d("CustomArrayAdapter",String.valueOf(position));
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.lvrowlayout2, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txtPresidentName);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
txtTitle.setText(menu[position]);
imageView.setImageResource(imageIds[position]);
return rowView;
}
}

片段2.java

package com.example.mypractice;
import android.app.ListActivity;
 import android.os.Bundle;
public class Fragment2 extends ListActivity {
String[] menu = {
"PanCakes with Butter and Honey",
"PanCakes with Butter and Honey",
};
Integer[] imageIDs = {
R.drawable.pancake,
R.drawable.pancake,
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment2);

this.setListAdapter(new ArrayAdapter<String>(
this,
R.layout.lvrowlayout,
R.id.txtPresidentName,
presidents));
CustomArrayAdapter adapter = new
CustomArrayAdapter(this, menu, imageIDs);
setListAdapter(adapter);
}
}
4

3 回答 3

0

活动不是片段。您的活动需要包含片段。考虑有一个包含列表视图片段和图像显示片段的主要活动。

于 2013-04-29T14:05:49.133 回答
0

尝试将类扩展为 ListFragment

于 2013-04-29T14:07:39.757 回答
0

您基本上想知道如何使用 Fragments 构建动态 UI或如何设计多个屏幕

您的 ListActivity 应该成为 FragmentActivity 内的 ListFragment 加上在两种布局之间切换的所有逻辑。

于 2013-04-29T14:09:32.423 回答