问题描述
下面是有问题我的code。我试图进入一个事件后更新列表视图我SQLite数据库的数据,但列表视图没有显示,我在用我的片段坏了,任何人都可以指导我,我做错了什么在这里?对不起,我是新来的Android应用程序的开发。谢谢你。
公开查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){ 查看rootView = inflater.inflate(R.layout.event,集装箱,FALSE);
秀=(按钮)rootView.findViewById(R.id.button1);
show.setOnClickListener(本);
返回rootView;
}公共无效onActivityCreated(INT申请code,INT结果code,意图数据){
ArrayList的<&HashMap的LT;字符串,字符串>> EVENTLIST =控制器
.getAllEvents();
如果(eventList.size()!= 0){
LV的ListView =(ListView控件)rootView.findViewById(R.id.list);
lv.setOnItemClickListener(新OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<>母公司,观景,
INT位置,长的id){
事件ID =(TextView中)view.findViewById(R.id.eventId);
。字符串valEventId = eventId.getText()的toString();
意图objIndent =新意图(getActivity()
EditEvent.class);
objIndent.putExtra(事件ID,valEventId);
startActivity(objIndent);
}
});
ListAdapter适配器=新SimpleAdapter(getActivity(),EVENTLIST,
R.layout.view_event_entry,新的String [] {事件ID
eventName的},新的INT [] {R.id.eventId,
R.id.eventName});
lv.setAdapter(适配器);
}}@覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
意图objIntent =新意图(getActivity(),AddEvent.class);
startActivity(objIntent);
}
下面是我的XML code:
< RelativeLayout的
机器人:ID =@ + ID / relativeLayout1
机器人:layout_width =FILL_PARENT
机器人:layout_height =40dp
机器人:方向=垂直> <按钮
机器人:ID =@ + ID /按钮1
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:背景=@绘制/ ADDEVENTS
机器人:layout_alignParentRight =真
机器人:layout_alignParentTop =真/>< / RelativeLayout的>
< RelativeLayout的
机器人:ID =@ ID / relativeLayout1
机器人:layout_width =FILL_PARENT
机器人:layout_height =match_parent
机器人:layout_alignParentLeft =真
机器人:layout_below =@ + ID / relativeLayout1
机器人:方向=垂直
机器人:layout_marginTop =40dp>< ListView控件
机器人:ID =@ + ID /列表
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentLeft =真正的>
< /&的ListView GT;
< / RelativeLayout的>
< / RelativeLayout的>
下面是code设立我的数据库。我不知道这是哪里出了问题发生。你们可以看看这里。
公共EventController(上下文的ApplicationContext){
超(ApplicationContext的androidsqlite.db,NULL,1);
Log.d(logcat中,创建);
}@覆盖
//为数据库创建表
公共无效的onCreate(SQLiteDatabase数据库){
查询字符串;
查询=CREATE TABLE事件(事件ID INTEGER PRIMARY KEY,eventName的文本);
database.execSQL(查询);
Log.d(logcat中,事件创建);
}
@覆盖
//删除数据库,如果需要复位
公共无效onUpgrade(SQLiteDatabase数据库,诠释version_old,诠释CURRENT_VERSION){
查询字符串;
查询=DROP TABLE IF EXISTS事件;
database.execSQL(查询);
的onCreate(数据库);
}公共无效insertEvent(HashMap的<字符串,字符串> queryValues){
SQLiteDatabase数据库= this.getWritableDatabase();
ContentValues值=新ContentValues();
values.put(eventName的,queryValues.get(eventName的));
database.insert(事件,空,价值);
database.close();
}公众诠释updateEvent(HashMap的<字符串,字符串> queryValues){
SQLiteDatabase数据库= this.getWritableDatabase();
ContentValues值=新ContentValues();
values.put(eventName的,queryValues.get(eventName的));
返回database.update(事件的价值观,事件ID+=?,新的String [] {queryValues.get(事件ID)});
//字符串updateQuery =更新的话设置txtWord ='+字+阿凡txtWord ='+ oldWord +';
//Log.d(LOGCAT,updateQuery);
//database.rawQuery(updateQuery,NULL);
//返回database.update(字,价值观,txtWord =?,新的String [] {字});
}公共无效deleteEvent(字符串ID){
Log.d(logcat中,删除);
SQLiteDatabase数据库= this.getWritableDatabase();
字符串deleteQuery =DELETE FROM事件,其中EVENTID ='+身份证+';
Log.d(查询,deleteQuery);
database.execSQL(deleteQuery);
}公众的ArrayList<&HashMap的LT;字符串,字符串>> getAllEvents(){
ArrayList的<&HashMap的LT;字符串,字符串>>单词表;
单词表=新的ArrayList<&HashMap的LT;字符串,字符串>>();
字符串selectQuery =SELECT * FROM事件;
SQLiteDatabase数据库= this.getWritableDatabase();
光标光标= database.rawQuery(selectQuery,NULL);
如果(cursor.moveToFirst()){
做{
HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
map.put(EVENTID,cursor.getString(0));
map.put(eventName的,cursor.getString(1));
wordList.add(地图);
}而(cursor.moveToNext());
} //返回联系人列表
返回单词表;
}公众的HashMap<字符串,字符串> getEventInfo(字符串ID){
HashMap的<字符串,字符串>单词表=新的HashMap<字符串,字符串>();
SQLiteDatabase数据库= this.getReadableDatabase();
字符串selectQuery =SELECT * FROM事件,其中EVENTID ='+身份证+';
光标光标= database.rawQuery(selectQuery,NULL);
如果(cursor.moveToFirst()){
做{
// HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
wordList.put(eventName的,cursor.getString(1));
//wordList.add(map);
}而(cursor.moveToNext());
}
返回单词表;
}
There are a few things that raise red flags in your code. Which leads me to my first question: are you using eclipse or intellij? Because it doesn't look like it'll even compile to me. Maybe you edited this on here, but now to help you get it working. Don't even use onActivityCreated
for initializing your ListView
in a Fragment (Andrew also correctly brought up the point that you aren't even using it correctly, which is why I am surprised its even compiling if it is). You should be doing it in onCreateView
. Now I will post your code that I have edited. I am not testing it so it serves as a better guide to what it should look like (and hopefully it compiles).
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.event, container, false);
ArrayList<HashMap<String, String>> eventList = controller
.getAllEvents();
if (eventList.size() != 0) {
ListView lv = (ListView) rootView.findViewById(R.id.list);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
eventId = (TextView) view.findViewById(R.id.eventId);
String valEventId = eventId.getText().toString();
Intent objIndent = new Intent(getActivity(),
EditEvent.class);
objIndent.putExtra("eventId", valEventId);
startActivity(objIndent);
}
});
ListAdapter adapter = new SimpleAdapter(getActivity(), eventList,
R.layout.view_event_entry, new String[] { "eventId",
"eventName" }, new int[] { R.id.eventId,
R.id.eventName });
lv.setAdapter(adapter);
}
show = (Button)rootView.findViewById(R.id.button1);
show.setOnClickListener(this);
return rootView;
}
...
这篇关于Android的 - 在的ListView片段不会显示出来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!