我正在使用ListView中具有多个TextView的应用程序,但我似乎遇到了问题。该列表似乎没有被修改。 我现在附上我的代码
public class MainActivity extends ActionBarActivity {
private List<CustomItem> myItem = new ArrayList<>();
ListView listView;
EditText edit;
Button insert;
String getText;
StringTokenizer st;
private String text1,text2;
List<CustomItem> customItem = new ArrayList<CustomItem>();
ArrayAdapter<CustomItem> adapter_List;
CompleteAdapter completeAdapter;
CustomItem obj;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText) findViewById(R.id.editText);
insert = (Button)findViewById(R.id.addBtn);
populate();
listView = (ListView) findViewById(R.id.listView);
completeAdapter = new CompleteAdapter(customItem, this);
listView.setAdapter(completeAdapter);
}
private void populate() {
customItem.add(new CustomItem("Testing","Application"));
}
public void addToList(View view) {
getText = edit.getText().toString();
st = new StringTokenizer(getText," ");
text1 = st.nextToken().toString();
text2 = st.nextToken().toString();
MainActivity.this.customItem.add(new CustomItem(text1,text2));
MainActivity.this.completeAdapter.notifyDataSetChanged();
}
/*
private class MyListAdapter extends ArrayAdapter<CustomItem> {
private MyListAdapter() {
super(MainActivity.this, R.layout.item_row,myItem);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if(itemView== null){
itemView = getLayoutInflater().inflate(R.layout.item_row,parent,false);
}
CustomItem customItem = myItem.get(position);
TextView t1 = (TextView) itemView.findViewById(R.id.text1);
TextView t2 = (TextView) itemView.findViewById(R.id.text2);
t1.setText(customItem.getName());
t2.setText(customItem.getAddress());
return itemView;
}
}*/
}
这是我的适配器类
class CompleteAdapter extends ArrayAdapter<CustomItem>{
private List<CustomItem> items;
private Context context;
CompleteAdapter(List<CustomItem> items, Context context) {
super (context,R.layout.item_row,items);
this.items = items;
this.context = context;
}
@Override
public int getCount() {
return 0;
}
@Override
public CustomItem getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v= convertView;
Holder holder = new Holder();
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.item_row, null);
TextView name_text = (TextView)v.findViewById(R.id.name);
TextView address_text = (TextView)v.findViewById(R.id.address);
holder.nameView = name_text;
holder.addView = address_text;
v.setTag(holder);
}
else
holder = (Holder)v.getTag();
CustomItem custom = items.get(position);
holder.nameView.setText(custom.getName());
holder.addView.setText(custom.getAddress());
return v;
}
private class Holder {
public TextView nameView;
public TextView addView;
}
}
这是我的CustomIte.java
class CustomItem{
private String name;
private String address;
CustomItem(String name, String address) {
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}}
正如您所看到的,我尝试使用2个具有相同结果的适配器。字符串中包含的数据被传递,所以我认为问题出在适配器上。 (所有组件的ID都很完美)我应该做些什么改变?
答案 0 :(得分:2)
应更改Adapter
@Override
public int getCount() {
return 0;
}
@Override
public CustomItem getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
到
@Override
public int getCount() {
return items.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public CustomItem getItem(int position) {
return items.get(position);
}
答案 1 :(得分:0)
问题出在您的适配器中,它返回一个空视图(v)
@Override
public int getCount() {
return items.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public CustomItem getItem(int position) {
return items.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
if(convertView == null){
convertView =LayoutInflater.from(context).inflate(R.layout.item_row, null);
TextView name_text = (TextView)v.findViewById(R.id.name);
TextView address_text = (TextView)v.findViewById(R.id.address);
holder = new Holder();
holder.nameView = name_text;
holder.addView = address_text;
v.setTag(holder);
}
else
holder = (Holder)v.getTag();
CustomItem custom = items.get(position);
holder.nameView.setText(custom.getName());
holder.addView.setText(custom.getAddress());
return convertView ;
}
跳吧帮助;)