我是Android的新手,迫切需要帮助。 我已经成功创建了一个带有images的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;
}
}
Fragment2.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);
}
}
答案 0 :(得分:0)
活动不是片段。您的活动需要包含片段。考虑使用包含listview片段和图像显示片段的主活动。
答案 1 :(得分:0)
尝试将类扩展为ListFragment
答案 2 :(得分:0)
您基本上想知道如何Build a Dynamic UI with Fragments或如何Design for Multiple Screens。
您的ListActivity应该成为FragmentActivity中的ListFragment以及在两种布局之间切换的所有逻辑。