ArrayList适配器构造函数失败(?) [英] ArrayList Adapter constructor fails (?)

查看:248
本文介绍了ArrayList适配器构造函数失败(?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要知道如何创建由Textview填充的ArrayList

Need to know how to create an ArrayList filled by a Textview

我正在尝试为多维(2D)ArrayList创建一个ArrayAdapter:

I am trying to create an ArrayAdapter for a Multidimensional (2D) ArrayList:

        final ArrayAdapter<ArrayAdapter<ArrayList<String>>> adapterNames = new ArrayAdapter<ArrayAdapter<ArrayList<String>>>(this,android.R.layout.simple_list_item_1, android.R.id.text1, arrayNamesOne);

这是我收到的日志错误:

This is the log error I get:

错误:(28,76)错误:找不到适合的构造函数 ArrayAdapter(MainActivity,int,int,ArrayList>) 构造函数 ArrayAdapter.ArrayAdapter(Context,int,int,ArrayAdapter> []) 不适用(参数不匹配; ArrayList> 无法转换为ArrayAdapter> [])构造函数 ArrayAdapter.ArrayAdapter(Context,int,int,List >>) 不适用(参数不匹配; ArrayList> 无法转换为列表>>)

Error:(28, 76) error: no suitable constructor found for ArrayAdapter(MainActivity,int,int,ArrayList>) constructor ArrayAdapter.ArrayAdapter(Context,int,int,ArrayAdapter>[]) is not applicable (argument mismatch; ArrayList> cannot be converted to ArrayAdapter>[]) constructor ArrayAdapter.ArrayAdapter(Context,int,int,List>>) is not applicable (argument mismatch; ArrayList> cannot be converted to List>>)

如果您希望对感兴趣的整个代码有更好的了解,请执行以下操作:

If you prefer to take a better look at the whole code interested, here you go:

public class MainActivity extends ActionBarActivity {
//1ST DECLARATION
ArrayList<ArrayList<String>> arrayNamesOne = new ArrayList<ArrayList<String>>();
ListView listNamesOne;
TextView namesTextOne;

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


    //1ST SETUP
    listNamesOne = (ListView) findViewById(R.id.listNamesId);
    namesTextOne = (TextView) findViewById(R.id.namesTexter);

    final ArrayAdapter<ArrayAdapter<ArrayList<String>>> adapterNames = new ArrayAdapter<ArrayAdapter<ArrayList<String>>>(this,android.R.layout.simple_list_item_1, android.R.id.text1, arrayNamesOne);
    listNamesOne.setAdapter(adapterNames);
    //END 1ST SETUP

    //1ST BUTTON '+'
    Button buttonPlus = (Button)findViewById(R.id.buttonPlus);
    buttonPlus.setOnClickListener(
            new Button.OnClickListener() {
                //CLICK EVENT
                @Override
                public void onClick(View v) {
                    //IF EMPTY
                    if (namesTextOne.getText().toString().isEmpty()){
                        Context context = getApplicationContext();
                        CharSequence text = "Add an item first";
                        int duration = Toast.LENGTH_SHORT;
                        Toast.makeText(context, text, duration).show();
                    //IF SPACES ONLY
                    }else if(namesTextOne.getText().toString().trim().isEmpty()){
                        Context context = getApplicationContext();
                        CharSequence text = "You can't use spaces only";
                        int duration = Toast.LENGTH_SHORT;
                        namesTextOne.setText("");
                        Toast.makeText(context, text, duration).show();
                    //ALRIGHT, ADD IT!
                    } else if(!namesTextOne.getText().toString().isEmpty()) {
                        arrayNamesOne.add(0, (ArrayList<String>) namesTextOne.getText());
                        adapterNames.notifyDataSetChanged();
                        namesTextOne.setText("");
                    }

                }
            }
    );
}

推荐答案

如果您要为字符串列表创建适配器,则需要这样:

If you were creating an adapter for a list of strings it would like this:

ArrayList<String> array = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, array);

因此,用于字符串列表的适配器看起来像:

So an adapter for a list of lists of strings would look like:

ArrayList<ArrayList<String>> array = new ArrayList<ArrayList<String>>();
ArrayAdapter<ArrayList<String>> adapter = new ArrayAdapter<ArrayList<String>>(this,android.R.layout.simple_list_item_1, android.R.id.text1, array);

这篇关于ArrayList适配器构造函数失败(?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆