我是新來的android和急需幫助。 我成功創建了一個帶有圖像的ListView。我的任務是當我點擊任何列表項時,它的描述應該出現在平板電腦ListView的右側。毫無疑問,這可以在碎片的幫助下完成。但是要使用片段,Main類應該擴展Fragment類,但是我的問題是我的類已經在擴展ListActivity類。現在我該怎麼做?如何用我的Mainclass擴展ListActivity類和Fragment類?
我在我的項目三個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);
}
}