2014-08-28 57 views
1

所以我正在将我的代码在所有活动环境中工作的过程中移动到使用一个或两个活动加上一堆片段的代码。我遇到的问题是现在我试图转换一个活动,需要从网络中获取一堆基于JSON文本的对象,将它们保存到本地SQLite数据库,然后查询它们并将它们放入ListView中。这是不是很难移植的JSON SQLite的东西,但我有列表视图的问题...特别是在findViewById行,它“找不到符号”列表“”错误处理片段列表视图,无法找到符号'列表'

这是我的onCreateView代码:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_event, container, false); 

    dbHelper.open(); 
    Cursor cursor = dbHelper.fetchAllTestObjects(); 
    dbHelper.close(); 

    // The desired columns to be bound 
    String[] columns = new String[]{ 
      SQLiteDbAdapter.test_object_id, 
    }; 

    // the XML defined views which the data will be bound to 
    int[] to = new int[]{ 
      R.id.test_object_id, 
    }; 

    // create the adapter using the cursor pointing to the desired data 
    //as well as the layout information 
    SimpleCursorAdapter dataAdapter = new SimpleCursorAdapter(
      getActivity(), R.layout.home_test_objects_info, 
      cursor, 
      columns, 
      to, 
      0); 

    listView = (ListView) view.findViewById(R.id.list); 
    // Assign adapter to ListView 
    listView.setAdapter(dataAdapter); 


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> listView, View view, 
           int position, long id) { 
      // Get the cursor, positioned to the corresponding row in the result set 
      Cursor cursor = (Cursor) listView.getItemAtPosition(position); 

      int testObjectId = cursor.getInt(cursor.getColumnIndexOrThrow(SQLiteDbAdapter.test_object_id)); 
      DynamoDBManager.CoreInfo listCore = new DynamoDBManager.CoreInfo(testObjectId, "", ""); 
      // Do action based on listCore 
     } 
    }); 

    return view; 
} 

对于我fragment_event_list.xml我有:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.android.navigationdrawerexample.EventFragment">  

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</FrameLayout> 

和对象设置为对象,将填充ListView,home_events_info.xml这将有它的其他的TextView项目点:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/eventsRelLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="6dip"> 

    <TextView 
     android:id="@+id/test_object_id" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView" /> 

</RelativeLayout> 

主要我的活动代码之间的差异和这是我使用的片段(列表)模板,并将其自动命名为fragment_event_list.xml的列表,而不是ListView1的,我在我的activity_home.xml和ID不使用LinearLayout而是使用FrameLayout。

activity_home.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

</LinearLayout> 

我想象这些差异考虑我目前的问题,因为碎片和工作的FrameLayout比活动和不同的LinearLayout,但我不知道如何/为什么和如何修复错误。

回答

2

我想的ListView你的id属性设置不正确的XML

变化:机器人:ID = “@机器人:ID /列表”

为:机器人:ID =“@ + ID/list“