这是我的 ItemAdapter 的代码,我以这种方式将其用作列表的适配器。
list.setAdapter(new ItemAdapter(thisContext,imageUrl));
我正在尝试为将项目添加到 ListFragment 的按钮实现 onclick 侦听器。当我编译时,它说“无法解析符号 getSupportFragmentManager”。请参见下面的粗体字。但我很确定我导入了 android.support.v4.app.FragmentActivity(见下文)。我在主要活动中使用了相同的 getSupportFragmentManager 并且它有效。我不明白为什么它不在这里。如果不是,我应该如何获取 HeadlinesFragment 以便我可以添加到该片段中的列表中?
感谢我能在这里得到的任何帮助。
码头。
package com.suite.android.menu;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
**import android.support.v4.app.FragmentActivity;**
public class ItemAdapter extends BaseAdapter {
private Context itemPageContext;
private String imagesUrl[];
private static final String TAG = "ItemAdapter" ;
public ItemAdapter(Context c, String[] imageUrl)
{
super();
itemPageContext = c;
imagesUrl = imageUrl.clone(); // copy over
}
private LayoutInflater getLayoutInflater()
{
return (LayoutInflater) itemPageContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(final int position, View convertView, ViewGroup parent)
{
// all this is for 1 item in the list
View theView = convertView;
final ViewHolder holder;
if (convertView == null) {
theView = getLayoutInflater().inflate(R.layout.item_list_image, null);
holder = new ViewHolder();
holder.text = (TextView) theView.findViewById(R.id.foodTitle);
holder.image = (ImageView) theView.findViewById(R.id.foodImage);
holder.button = (Button) theView.findViewById(R.id.addOrderButton);
theView.setTag(holder);
} else
holder = (ViewHolder) theView.getTag();
holder.text.setText("Item " + position);
holder.image.setImageResource(R.drawable.ic_launcher);
holder.button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
**HeadlinesFragment headFragment = (HeadlinesFragment) getSupportFragmentManager().findFragmentById(R.id.headlines_fragment);**
headFragment.AddItem("One");
Log.d(TAG, "Added Item!");
}
});
return theView;
}
private class ViewHolder {
public TextView text;
public ImageView image;
public Button button;
}
@Override
public int getCount() {
return imagesUrl.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
}