listview与片段内的基本适配器

时间:2015-04-06 16:19:38

标签: android listview android-intent android-fragments android-activity

我有一个带有bade适配器的列表视图它实际上是一个静态的每一行包含2个文本视图和一个图像问题是我试图把它放在一个片段中的活动它工作得很好但是确实把它放在一个片段类它给我错误行包含对另一个活动的意图但意图给出错误并且还将adpater设置为列表视图会给出另一个错误  这是代码:

public class consulterfragment extends Fragment implements AdapterView.OnItemClickListener {
ListView list;
@Nullable    
@Override    
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
   View view= inflater.inflate(R.layout.consulter_fragment_layout,container,false) ;
    list= (ListView) view.findViewById(R.id.listView);
    list.setAdapter(new SanaAdapter(this));
    list.setOnItemClickListener(this);
    return view;

}
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Intent intent = new Intent(this, note_details.class);
    startActivity(intent);
    MyViewHolder holder = (MyViewHolder) view.getTag();
    SingleRow temp = (SingleRow) holder.myImage.getTag();
    intent.putExtra("titles", temp.titles);
    startActivity(intent);
}

class SingleRow {
    String titles;
    String description;
    int image;

    SingleRow(String titles, String description, int image) {
        this.titles = titles;
        this.description = description;
        this.image = image;
    }
}
class MyViewHolder {
    ImageView myImage;
    TextView myTitle;
    TextView myDescription;

    MyViewHolder(View v) {
        myImage = (ImageView) v.findViewById(R.id.imageView2);
        myTitle = (TextView) v.findViewById(R.id.textView4);
        myDescription = (TextView) v.findViewById(R.id.textView5);
    }
}

class SanaAdapter extends BaseAdapter {
    ArrayList<SingleRow> list;
    Context context;

    SanaAdapter(Context c) {
        context = c;
        list = new ArrayList<SingleRow>();
        Resources res = c.getResources();
        String[] titles = res.getStringArray(R.array.titles);
        String[] descriptions = res.getStringArray(R.array.decriptions);
        int[] images = {R.drawable.ico1, R.drawable.ico2, R.drawable.ico3, R.drawable.ico4, R.drawable.ico5, R.drawable.ico6, R.drawable.ico7, R.drawable.ico8,
                R.drawable.ico9, R.drawable.ico10, R.drawable.ico1, R.drawable.ico2, R.drawable.ico3, R.drawable.ico4, R.drawable.ico5, R.drawable.ico6, R.drawable.ico7, R.drawable.ico8,
                R.drawable.ico9, R.drawable.ico10};
        for (int i = 0; i < 10; i++) {
            list.add(new SingleRow(titles[i], descriptions[i], images[i]));
        }
    }

    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertview, ViewGroup viewGroup) {
        View row = convertview;
        MyViewHolder holder = null;
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.notelist_row, viewGroup, false);
            holder = new MyViewHolder(row);
            row.setTag(holder);
            Log.d("SANA", "Creating a new row");
        } else {
            holder = (MyViewHolder) row.getTag();
            Log.d("SANA", "Recycling shit hhhhhh");
        }

        SingleRow temp = list.get(i);

        holder.myTitle.setText(temp.titles);
        holder.myDescription.setText(temp.description);
        holder.myImage.setImageResource(temp.image);
        return row;
        }
    }
}

第一个错误是设置适配器 它说SanaAdapter(android.context.Context)中的SanaAdapter无法应用

第二个错误在于它所说的意图 无法解析构造函数Intent(com.example.hp.memorybackup.consulterfragment,java.lang.class<com.example.hp.memorybackup.notedetails>)

我真的很感激你的帮助

1 个答案:

答案 0 :(得分:3)

在onCreateView中,您正在调用

list.setAdapter(new SanaAdapter(this)); 

但是正确的将是

list.setAdapter(new SanaAdapter(getActivity()); 

也应该高亮一点,是吗? Fragment扩展了Object,它不是Context的子代。