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

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

问题描述

编辑:需要知道如何创建由一个TextView填充的ArrayList

我想创建一个 ArrayAdapter 多维(2D) Arrayist

 最后ArrayAdapter&LT; ArrayAdapter&LT; ArrayList的&LT;串GT;&GT;&GT; adapterNames =新ArrayAdapter<ArrayAdapter<ArrayList<String>>>(this,android.R.layout.simple_list_item_1, android.R.id.text1,arrayNamesOne);

这是日志的错误我得到:

错误:(28 76)错误:发现ArrayAdapter没有合适的构造(MainActivity,INT,INT,ArrayList的>)
构造ArrayAdapter.ArrayAdapter(背景下,INT,INT,ArrayAdapter> [])不适用
(参数不匹配; ArrayList的>不能转换到ArrayAdapter> [])
构造ArrayAdapter.ArrayAdapter(背景下,INT,INT,列表>>)不适用
(参数不匹配; ArrayList的>不能转换到列表>>)

如果您preFER采取在整个code有兴趣的更好看,在这里你去:

 公共类MainActivity扩展ActionBarActivity {
// 1ST声明
ArrayList的&LT; ArrayList的&LT;串GT;&GT; arrayNamesOne =新的ArrayList&LT; ArrayList的&LT;串GT;&GT;();
ListView的listNamesOne;
TextView的namesTextOne;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    // 1ST SETUP
    listNamesOne =(ListView控件)findViewById(R.id.listNamesId);
    namesTextOne =(的TextView)findViewById(R.id.namesTexter);    最后ArrayAdapter&LT; ArrayAdapter&LT; ArrayList的&LT;串GT;&GT;&GT; adapterNames =新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'+'
    按钮buttonPlus =(按钮)findViewById(R.id.buttonPlus);
    buttonPlus.setOnClickListener(
            新Button.OnClickListener(){
                // Click事件
                @覆盖
                公共无效的onClick(视图v){
                    //如果为空
                    如果(namesTextOne.getText()。的toString()。的isEmpty()){
                        上下文的背景下= getApplicationContext();
                        CharSequence的文字=第一添加项目;
                        INT持续时间= Toast.LENGTH_SHORT;
                        Toast.makeText(上下文,文本,持续时间).show();
                    //如果仅空间
                    }否则如果(namesTextOne.getText()。的toString()。修剪()的isEmpty()){
                        上下文的背景下= getApplicationContext();
                        CharSequence的文字=您不仅可以使用空格;
                        INT持续时间= Toast.LENGTH_SHORT;
                        namesTextOne.setText();
                        Toast.makeText(上下文,文本,持续时间).show();
                    //好了,它添加!
                    }否则如果(!namesTextOne.getText()。的toString()。的isEmpty()){
                        arrayNamesOne.add(0,(ArrayList的&所述;字符串&GT)namesTextOne.getText());
                        adapterNames.notifyDataSetChanged();
                        namesTextOne.setText();
                    }                }
            }
    );
}


解决方案

如果你为一个字符串列表创建适配器,将是这样的:

 的ArrayList&LT;串GT;阵列=新的ArrayList&LT;串GT;();
ArrayAdapter&LT;串GT;适配器=新ArrayAdapter&LT;串GT;(这一点,android.R.layout.simple_list_item_1,android.R.id.text1,数组);

因此​​,对于字符串列表清单适配器看起来像:

 的ArrayList&LT; ArrayList的&LT;串GT;&GT;阵列=新的ArrayList&LT; ArrayList的&LT;串GT;&GT;();
ArrayAdapter&LT; ArrayList的&LT;串GT;&GT;适配器=新ArrayAdapter&LT; ArrayList的&LT;串GT;&GT;(这一点,android.R.layout.simple_list_item_1,android.R.id.text1,数组);

EDIT: Need to know how to create an arrayList filled by a Textview

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

        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:

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);

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

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