当我将其放在onCreate其工作台上时,我想使用数组创建listview。
String nama [] = {"Olala","bolabola","yolooo","fighter"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
但是当我将其放在onCreate下方时,“ nama”无法读取我的数组
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String nama [] = {"Olala","bolabola","yolooo","fighter"};
ListView listView = (ListView)findViewById(R.id.ListView);
CostumAdapter costumAdapter = new CostumAdapter();
listView.setAdapter(costumAdapter);
}
public class CostumAdapter extends BaseAdapter{
@Override
public int getCount() {
return nama.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = getLayoutInflater().inflate(R.layout.arablayout,null);
TextView textViewName = (TextView)convertView.findViewById(R.id.textview);
textViewName.setText(nama[position]);
return convertView;
}
}
}
无法解析符号纳马 我必须这样做,因为此后我将创建spannableString 有什么建议吗?
答案 0 :(得分:2)
您应该为customView
类创建一个构造函数,该构造函数包括一个参数以使数组通过:-
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String nama [] = {"Olala","bolabola","yolooo","fighter"};
ListView listView = (ListView)findViewById(R.id.ListView);
CostumAdapter costumAdapter = new CostumAdapter(nama);
listView.setAdapter(costumAdapter);
}
public class CostumAdapter extends BaseAdapter{
String[] nama;
public CostumAdapter(String[] nama){
this.nama = nama;
}
@Override
public int getCount() {
return nama.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = getLayoutInflater().inflate(R.layout.arablayout,null);
TextView textViewName = (TextView)convertView.findViewById(R.id.textview);
textViewName.setText(nama[position]);
return convertView;
}
}
}
答案 1 :(得分:0)
您必须声明一个函数,该函数可以更新适配器类中的数据,可以在onCreate
方法中初始化列表,或者是否需要,然后调用该函数来更新适配器的数据。
这是一个例子:
public class MyActivity extends AppCompactActivity {
List<String> myList = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myList.add("firstString");
myList.add("secondString");
myList.add("thirdString");
myList.add("fourthString");
ListView listView = (ListView)findViewById(R.id.ListView);
CostumAdapter costumAdapter = new CostumAdapter();
customAdapter.updateList(myList);
listView.setAdapter(costumAdapter);
}
public class CostumAdapter extends BaseAdapter{
private List<String> myList = new ArrayList();
public void updateList(List<String> newList) {
this.myList = newList;
notifyDataSetChanged();
}
@Override
public int getCount() {
return myList.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = getLayoutInflater().inflate(R.layout.arablayout,null);
TextView textViewName = (TextView)convertView.findViewById(R.id.textview);
textViewName.setText(myList.get(position));
return convertView;
}
}
}