向列表视图行中的每个项添加按钮时出错

时间:2016-03-07 17:24:02

标签: java android listview android-fragments

OneFragment.java

package com.pixalstudio.musicadda;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class OneFragment extends android.support.v4.app.Fragment {


    public OneFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_one, container, false);


        ArrayList<String> songs = new ArrayList<String>();

        List<String> Songs = Arrays.asList(getResources().getStringArray(R.array.songs));

//        list.add("item1");
//        list.add("item2");

        MyCustomAdapter adapter = new MyCustomAdapter(songs, getActivity());

        ListView listView = (ListView) view.findViewById(R.id.listView_fragone);
        listView.setAdapter(adapter);
        return view;
    }



    /*@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_one, container, false);

        ArrayList<String> list = new ArrayList<String>();
        list.add("item1");
        list.add("item2");

        MyCustomAdapter adapter = new MyCustomAdapter(list, this);

        ListView listView = (ListView) getView().findViewById(R.id.listView_fragone);
        listView.setAdapter(adapter);
        return view;
    }*/


}

我收到此文件的错误。

1

Error:(43, 13) error: <identifier> expected
Error:(43, 14) error: illegal start of type
Error:(44, 13) error: <identifier> expected
Error:(44, 14) error: illegal start of type
Error:(49, 24) error: <identifier> expected
Error:(49, 32) error: <identifier> expected

[这是我收到错误的FragmentOne.java。]

MyCustomAdapter.java

    package com.pixalstudio.musicadda;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by akkie on 3/7/2016.
 */
public class MyCustomAdapter extends BaseAdapter implements ListAdapter {

    private ArrayList<String> list = new ArrayList<String>();
    private Context context;

    public MyCustomAdapter(ArrayList<String> list, Context context) {

        this.list = list;
        this.context = context;

    }

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

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

    @Override
    public long getItemId(int pos) {
        return 0;
        //just return 0 if your list items do not have an Id variable.
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.cust_layout, null);
        }

        //Handle TextView and display string from your list
        TextView listItemText = (TextView)view.findViewById(R.id.list_item_string);
        listItemText.setText(list.get(position));

        //Handle buttons and add onClickListeners
        Button deleteBtn = (Button)view.findViewById(R.id.delete_btn);
        Button addBtn = (Button)view.findViewById(R.id.add_btn);

        deleteBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                //do something
                list.remove(position); //or some other task
                notifyDataSetChanged();
            }
        });
        addBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                //do something
                notifyDataSetChanged();
            }
        });

        return view;
    }
}

添加了MyCustomAdapter.java,请检查一下,如果需要更改,请告诉我。

Updated FragmentOne.java

2 个答案:

答案 0 :(得分:2)

尝试将代码放在with open("/var/log/monthly.out") as f: months = map(str.strip, f.read().split("-- End of monthly output --")) for sec in filter(None, y): date = sec.splitlines()[0] accs = re.findall("\n\s+(\w+)\s+([\d\.]+)", sec) print(date, accs) 方法中。

onCreateView

答案 1 :(得分:0)

您将此传递给MyCustomerAdapter,这是当前片段而非活动上下文,请将此更改为getActivity()

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_one, container, false);

    ArrayList<String> Songs = Arrays.asList(getResources().getStringArray(R.array.songs));

   //        list.add("item1");
   //        list.add("item2");

    MyCustomAdapter adapter = new MyCustomAdapter(Songs , getActivity());

    ListView listView = (ListView) view.findViewById(R.id.listView_fragone);
    listView.setAdapter(adapter);
    return view;
}