Listview with Subitems:Array Adapter Overriding不起作用

时间:2016-12-09 16:08:49

标签: android listview android-arrayadapter

我只想要一个包含子项目的列表视图。所以我决定像这样覆盖arrayadapter:

ArrayAdapter adapter = new ArrayAdapter(context, android.R.layout.simple_list_item_2, android.R.id.text1, list) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                TextView text1 = (TextView) view.findViewById(android.R.id.text1);
                TextView text2 = (TextView) view.findViewById(android.R.id.text2);

                text1.setText("TEST1");
                text2.setText("TEST2");
                return view;
            }
        };

问题是android studio总是说:无法解析符号“context”

我可以对此做些什么,或者您是否有其他解决方案来解决我的问题?

完整课程:

public class ChangelogActivity extends AppCompatActivity {

    ArrayAdapter<String> adapter;
    ArrayList<String> items;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_changelog);

        final ListView listviewChangelog = (ListView) findViewById(R.id.listviewChangelog);

        items=new ArrayList<String>();

        adapter=new ArrayAdapter(this, R.layout.menu_item,R.id.lvmenuitems,items);
        listviewChangelog.setAdapter(adapter);

        ArrayAdapter adapter = new ArrayAdapter(context, android.R.layout.simple_list_item_2, android.R.id.text1, list) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                TextView text1 = (TextView) view.findViewById(android.R.id.text1);
                TextView text2 = (TextView) view.findViewById(android.R.id.text2);

                text1.setText("TEST1");
                text2.setText("TEST2");
                return view;
            }
        };

        String url = "PLACEHOLDER";

        JsonArrayRequest getRequest = new JsonArrayRequest(url,
                new Response.Listener<JSONArray>()
                {
                    @Override public void onResponse(JSONArray response) {
                        //Log.d("Response", response.toString());

                        for (int i=0; i < response.length(); i++)
                        {
                            try {
                                JSONObject oneObject = response.getJSONObject(i);

                                // Pulling items from the array
                                String Version = oneObject.getString("version");
                                String change_mission = oneObject.getString("change_mission");
                                String change_map = oneObject.getString("change_map");
                                String change_mod = oneObject.getString("change_mod");
                                String note = oneObject.getString("note");


                                if(Version != "" || Version != ";")
                                {
                                    items.add("Update: " + Version);
                                }

                                if(Version != "" || Version != ";")
                                {
                                    items.add("Mission: " + change_mission);
                                }

                                if(Version != "" || Version != ";")
                                {
                                    items.add("Map:" + change_map);
                                }
                                if(Version != "" || Version != ";")
                                {
                                    items.add( "Mod:" + change_mod);
                                }
                                if(Version != "" || Version != ";")
                                {
                                    items.add("Notiz:" + note);
                                }

                                items.add("----------------------------------------------------");

                                adapter.notifyDataSetChanged();

                            } catch (JSONException e) {
                                //Not used at this time
                            }
                        }
                    }
                },
                new Response.ErrorListener()
                {
                    @Override public void onErrorResponse(VolleyError error) {
                        Log.d("Error.Response", error.toString());
                        //mTextView.setText(error.toString());
                    }
                }
        );
        MySingleton.getInstance(this).addToRequestQueue(getRequest);



    }
}

1 个答案:

答案 0 :(得分:0)

这就是我说的。如果您无法解决此问题,您将把代码发送给我。我会解决它并回复你。 (nurisezgin.sezgin@gmail.com)

ArrayAdapter adapter = new ArrayAdapter(ChangeLogActivity.this,
        android.R.layout.simple_list_item_2, android.R.id.text1, new String[]{}){

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        return view;
    }
};