带有片段的Android BaseAdapter

时间:2013-11-24 16:44:59

标签: android android-listview baseadapter

无法编译此代码并运行。在lv.setAdapter(new VcAdapter (this));中遇到问题 善意的帮助。如果我尝试不传递(this),那么代码编译正常,但运行时会收到错误,说明内容需要listview

import java.util.ArrayList;

import com.vaishnavismeclass.tiruppavai.tab.R;
import com.vaishnavismeclass.tiruppavai.tab.SingleRow;

import android.support.v4.app.Fragment;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class EnglishFragment extends Fragment {

    Context context = null;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_english, container, false);

        ListView lv = (ListView) rootView.findViewById(R.id.list); 
        lv.setAdapter(new VcAdapter (this));

        return rootView;
    }
}

class SingleRow
{
    String pasuram;
    int img;

    SingleRow(String pasuram, int img)
    {
        this.pasuram=pasuram;
        this.img=img;
    }
}
class VcAdapter extends BaseAdapter
{
    ArrayList<SingleRow> list;
    Context context;
    VcAdapter(Context c)
    {
        context = c;
        list = new ArrayList<SingleRow>();
        //get resources using context
        Resources res=c.getResources();
        String[] pasuram_en = res.getStringArray(R.array.pasuram_en);
        //String[] pasuram_ta = res.getStringArray(R.array.pasurams_ta);
//      String[] pasuram_te = res.getStringArray(R.array.pasurams_te);
        int[] imgs = {R.drawable.p1,R.drawable.p1,R.drawable.p1,R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4,R.drawable.p5,R.drawable.p6,R.drawable.p7,R.drawable.p8,R.drawable.p9,R.drawable.p10,R.drawable.p11,R.drawable.p12,R.drawable.p13,R.drawable.p14,R.drawable.p15,R.drawable.p16,R.drawable.p17,R.drawable.p18,R.drawable.p19,R.drawable.p20,R.drawable.p21,R.drawable.p22,R.drawable.p23,R.drawable.p24,R.drawable.p25,R.drawable.p26,R.drawable.p27,R.drawable.p28,R.drawable.p29,R.drawable.p30,R.drawable.p1,R.drawable.p1};

        for (int i=0;i<pasuram_en.length;i++)
        {
            //list.add(new SingleRow(pasuram_en[i], imgs[i]));
            list.add(new SingleRow(pasuram_en[i], imgs[i]));
        }
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        // TODO Auto-generated method stub
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        // TODO Auto-generated method stub
        LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.single_row, viewGroup, false);

        TextView pasuram = (TextView) row.findViewById(R.id.textView1);
        ImageView img = (ImageView) row.findViewById(R.id.imageView1);

        SingleRow temp=list.get(i);
        pasuram.setText(temp.pasuram);
        img.setImageResource(temp.img);

        return row;
    }

}

3 个答案:

答案 0 :(得分:5)

更改

lv.setAdapter(new VcAdapter (this));

lv.setAdapter(new VcAdapter(getActivity()));

您需要传递活动上下文。

getActivity()

返回此片段当前与之关联的活动。

答案 1 :(得分:0)

你还需要改变这个

@Override
    public long getItemId(int i) {
        // TODO Auto-generated method stub
        return 0;
    }

@Override
    public long getItemId(int i) {
        // TODO Auto-generated method stub
        return i;
    }

答案 2 :(得分:0)

您的片段列表可能没有被填充,因为您没有将正确的ID传递到片段布局文件中。

在片段布局文件中,您应该使用

android:id="@android:id/list"

不是

android:id="+@id/list"

尝试使用第二个选项手动进行操作将对“活动”有效,但对“片段”无效。

因此,您还必须扩展ListFragment而不是Fragment,这样BaseAdapter才能确定您实际上在自己的计算机中包含ListView可以设置为自定义适配器的片段。这也消除了您显式定义ListView的需要,这意味着您可以安全地删除检索它的行,并且仍然允许该片段生成其适当的视图。

尽管如此,您将需要呼叫setListAdapter(new VcAdapter (this));而不是lv.setAdapter(new VcAdapter (this));