我真的需要一些好的提示....我已经花了几个小时调试这个((...
public class Travel {
public static String from, to;
Travel(String f, String t) {
this.from = f;
this.to = t;
}
}
final class NotesAdapter extendes ArrayAdapter<Travel>{
private final List<Travel> myC;
public NotesAdapter(Context context, int resource, List<Travel> myCt) {
super(context, resource, myCt);
this.myC = myCt;
}
@Override
public int getCount() {
return myC.size();
}
@Override
public Travel getItem(int position) {
return myC.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup adapterView) {
Travel trv = myC.get(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.my_lists, adapterView, false);
}
TextView from = (TextView)convertView.findViewById(R.id.tv_from);
TextView to = (TextView)convertView.findViewById(R.id.tv_to);
from.setText(trv.from);//.getMFrom()
to.setText(trv.to);//.getMTo()
return convertView;
}
在NotesAdapter中,我只实现了您在此处看到的方法。 然后在主活动中我启动startactivityforresult并在onactivityresults中向List对象添加元素。
List<Travel> myCities = new ArrayList<Travel>();
将List对象初始化为ArrayList(... new ArrayList&lt;&gt;()...)。 我也有:
mList = (ListView)findViewById(R.id.for_lists);
na = new NotesAdapter(this,R.layout.my_lists,myCities);
所以:
nCity = data.getStringExtra("content");
收集新字符串
myCities.add(new Travel("Home Town",nCity));
添加新旅行......和:
na.notifyDataSetChanged();
mList.setAdapter(na);
这是所有代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == MY_REQUEST_CODE){
if(resultCode == RESULT_OK){
nCity = data.getStringExtra("content");
if (!myCities.contains(nCity)) {
myCities.add(new Travel("Home Town",nCity));
na.notifyDataSetChanged();
} else {
Toast.makeText(this,"This City has already been added",Toast.LENGTH_LONG).show();
}
}
else {
//do something when user refused to complete action
}
hT.setText("Home Town");
iB.setText(infoBar);
na.notifyDataSetChanged();
mList.setAdapter(na);
mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
}
else {
// not my business another request
}
}
当我只使用一个字符串列表时,一切都很完美。 “旅行列表”的结果是列表视图中的所有视图都显示最后添加的旅行。
对于布局文件,我使用基本实现。
请帮忙...... ^ _ ^
答案 0 :(得分:0)
na
之前, mList.setAdapter(na);
永远不会设置为列表。您无需致电na.notifyDataSetChanged();
请在NotesAdapter
适配器中添加一个新方法。此方法将更新数据:
public void updateData(List<Travel> myCt){
this.myC = myCt;
}
然后更新你的代码
na.notifyDataSetChanged();
mList.setAdapter(na);
带
na.updateData(myCities);
mList.setAdapter(na);
答案 1 :(得分:0)
问题是static
public static String from, to