ArrayList 适配器构造函数失败(?)
ArrayList Adapter constructor fails (?)
编辑:需要知道如何创建由 Textview
填充的 ArrayList
我正在尝试为 多维 (2D) ArrayList
:
创建一个 ArrayAdapter
final ArrayAdapter<ArrayAdapter<ArrayList<String>>> adapterNames = new ArrayAdapter<ArrayAdapter<ArrayList<String>>>(this,android.R.layout.simple_list_item_1, android.R.id.text1, arrayNamesOne);
这是我得到的日志错误:
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>>)
如果您想更好地查看整个代码感兴趣,请看这里:
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("");
}
}
}
);
}
如果您要为字符串列表创建适配器,它会像这样:
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);
因此,字符串列表的适配器看起来像:
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);
编辑:需要知道如何创建由 Textview
填充的ArrayList
我正在尝试为 多维 (2D) ArrayList
:
ArrayAdapter
final ArrayAdapter<ArrayAdapter<ArrayList<String>>> adapterNames = new ArrayAdapter<ArrayAdapter<ArrayList<String>>>(this,android.R.layout.simple_list_item_1, android.R.id.text1, arrayNamesOne);
这是我得到的日志错误:
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>>)
如果您想更好地查看整个代码感兴趣,请看这里:
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("");
}
}
}
);
}
如果您要为字符串列表创建适配器,它会像这样:
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);
因此,字符串列表的适配器看起来像:
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);