我无法在可扩展列表适配器中使用openFileInput()方法

时间:2015-07-01 20:46:01

标签: java android listview expandablelistview expandablelistadapter

我一直在尝试一种方法来读取我的文件subs.txt。我知道它存在,因为它适用于我的其他课程但不适用于此课程。在我的缓冲区读取器上,我得到一个无文件异常,当我使用openFileinput时,我得到"找不到符号方法openFileInput()"。

     public class ExpandableAdapter extends BaseExpandableListAdapter {

    private Activity activity;
    private ArrayList<Object> childtems;
    private LayoutInflater inflater;
    private ArrayList<String> parentItems, child;

    public ExpandableAdapter(ArrayList<String> parents, ArrayList<Object> childern) {
        this.parentItems = parents;
        this.childtems = childern;
    }

    public void setInflater(LayoutInflater inflater, Activity activity) {
        this.inflater = inflater;
        this.activity = activity;
    }




    @Override
    public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        child = (ArrayList<String>) childtems.get(groupPosition);

        TextView textView = null;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.group, null);
        }

        textView = (TextView) convertView.findViewById(R.id.textView1);
        textView.setText(child.get(childPosition));
        //textView.setText(Html.fromHtml("<a href=http://www.stackoverflow.com> STACK OVERFLOW "));
        // textView.setMovementMethod(LinkMovementMethod.getInstance());
        convertView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                try {
                    String link="";
                    ArrayList<String>a=child;
                    FileInputStream fin = openFileInput("subs.txt");
                    String test=child.get(childPosition);
                    String objEnc = "";
                    File myFile = new File( "subs.txt");
                    BufferedReader br = new BufferedReader(new FileReader(myFile));

                    StringBuilder sbuilder = new StringBuilder();
                    objEnc = br.readLine();
                    while (objEnc != null) {
                        sbuilder.append(objEnc);
                        objEnc = br.readLine();
                        if (objEnc != null) {

                            // sbuilder.append("\n");

                        }
                    }
                    br.close();
                    objEnc=sbuilder.toString();
                    objEnc=sbuilder.toString();
                    Gson gson = new Gson();
                    ManySubs obj = gson.fromJson(objEnc, ManySubs.class);
                    for(int i=0;i<obj.newPosts.size();i++){
                        for(int j=0;j<obj.newPosts.get(i).list.size();j++){
                            if(obj.newPosts.get(j).list.get(j).equals(test)){
                                link=obj.newPosts.get(j).list.get(j).url;
                            }

                        }
                    }
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
                    activity.startActivity(browserIntent);
                }catch (Exception e){
                    String q="";
                }
            }
        });

        return convertView;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.child, null);
        }

        ((CheckedTextView) convertView).setText(parentItems.get(groupPosition));
        ((CheckedTextView) convertView).setChecked(isExpanded);


        return convertView;
    }


    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return ((ArrayList<String>) childtems.get(groupPosition)).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public int getGroupCount() {
        return parentItems.size();
    }

    @Override
    public void onGroupCollapsed(int groupPosition) {
        super.onGroupCollapsed(groupPosition);
    }

    @Override
    public void onGroupExpanded(int groupPosition) {
        super.onGroupExpanded(groupPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


}

1 个答案:

答案 0 :(得分:0)

将Context发送到适配器并使用它:

  class MainActivity extends Activity {

        ExpandableAdapter adapter = new ExpandableAdapter(activity,parent,children);

 }

class ExpandableAdapter ...
{
   public ExpandableAdapter(Context context...)
   {
    mContext = context;

   }


 }

Context mContext;
mContext.openFileOutput

您还可以使用发送到适配器类的activity