0

我有一个列表片段。该列表的创建没有问题。

现在我需要访问每行列表视图中的文本视图,但我不能,我不知道为什么。访问列表行内的每个文本视图以更改每个文本视图。

 public class ScreenSlidePageFragment extends ListFragment implements AdapterView.OnItemClickListener {

private SimpleAdapter adapter;
    private ListView lv;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.fragment_screen_slide_page, container,false);
    lv = (ListView)rootView.findViewById(android.R.id.list);
    //create the grid item mapping
   String[]from = new String[]{"rowid","item1","item2","item3","item4","item5","item6","item7","item8","item9","item10","item11","item12","item13","item"};/*,"col1","col2","col3","col4","col5","col6","col7"};*/
   int[] to = new int[]{R.id.tv_list_item1,R.id.tv_listRow_item1,R.id.tv_listRow_item2,R.id.tv_listRow_item3,R.id.tv_listRow_item4,R.id.tv_listRow_item5,R.id.tv_listRow_item6,R.id.tv_listRow_item7,R.id.tv_listRow_item8,R.id.tv_listRow_item9,R.id.tv_listRow_item10,R.id.tv_listRow_item11,R.id.tv_listRow_item12,R.id.tv_listRow_item13,R.id.tv_listRow_item14};  /*, R.id.tv_list_item3, R.id.tv_list_item4, R.id.tv_list_item5, R.id.tv_list_item6, R.id.tv_list_item7};*/

   //prepare the list of all records
   List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
   for(int i = 0 ; i < 24 ; i++){
     Log.d(ScreenSlidePageFragment.class.getName(), "Entrou no ciclo --- i: " +i);
    HashMap<String, String> map = new HashMap<String, String>();
    if(i<10){
    map.put("rowid","" + "0"+i+":00");
    map.put("item1", "item1");
    map.put("item2", "item2");
    map.put("item3", "item3");
    map.put("item4", "item4");
    map.put("item5", "item5");
    map.put("item6", "item6");
    map.put("item7", "item7");
    map.put("item8", "item8");
    map.put("item9", "item9");
    map.put("item10", "item10");
    map.put("item11", "item11");
    map.put("item12", "item12");
    map.put("item13", "item13");
    map.put("item14", "item14");        
    }
    else if(i>=10)
        map.put("rowid","" +i+":00");
    fillMaps.add(map);      
   } 

   //fill in the grid_item layout
   adapter = new SimpleAdapter(getActivity(), fillMaps, R.layout.sechedule_list_row, from, to);
   lv.setAdapter(adapter);


   **//I try this but don't work and i don't know why.....**
   **TextView tv = (TextView)lv.findViewById(R.id.tv_listRow_item1);
   tv.setText("test");**


  //******** Row of list
  <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_height="wrap_content"
  android:layout_width="fill_parent">

        <TextView 
            android:id="@+id/tv_list_item1"
            android:layout_height="50dp"
            android:layout_width="80dp"
            android:gravity="center"
            android:background="@drawable/textview_border"
            android:textStyle="bold"
            android:width="80dip"
            /><!--  android:text="@string/tv_list_item1" -->

       <LinearLayout
          android:orientation="vertical"
          android:layout_height="fill_parent"
          android:layout_width="0dip"
          android:layout_weight="1">    
         <TextView 
            android:id="@+id/tv_listRow_item1"
            android:tag="tv_listRow_item1_1"
            android:layout_height="0dip"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:width="100dip"
            android:height="25dip"
            android:background="@drawable/textview_listrow_border"
            android:text=""/>

        <TextView 
            android:id="@+id/tv_listRow_item2"
            android:tag="tv_listRow_item1_2"
            android:layout_height="0dip"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:height="25dip"
            android:width="100dip"
            android:background="@drawable/textview_listrow_border"
            android:text=""
            android:clickable="true"
            />
    </LinearLayout>     
    <LinearLayout
          android:orientation="vertical"
          android:layout_height="fill_parent"
          android:layout_width="0dip"
          android:layout_weight="1">    
         <TextView 
            android:id="@+id/tv_listRow_item3"
            android:tag="tv_listRow_item1_3"
            android:layout_height="0dip"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:width="100dip"
            android:height="25dip"
            android:background="@drawable/textview_listrow_border"
            android:text=""
            android:clickable="true"/>

        <TextView 
            android:id="@+id/tv_listRow_item4"
            android:tag="tv_listRow_item1_4"
            android:layout_height="0dip"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:height="25dip"
            android:width="100dip"
            android:background="@drawable/textview_listrow_border"
            android:text=""
            android:clickable="true"/>
    </LinearLayout>  
    <LinearLayout
          android:orientation="vertical"
          android:layout_height="fill_parent"
          android:layout_width="0dip"
          android:layout_weight="1">    
         <TextView 
            android:id="@+id/tv_listRow_item5"
            android:tag="tv_listRow_item1_5"
            android:layout_height="0dip"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:width="100dip"
            android:height="25dip"
            android:background="@drawable/textview_listrow_border"
            android:text=""
            android:clickable="true"/>

        <TextView 
            android:id="@+id/tv_listRow_item6"
            android:tag="tv_listRow_item1_6"
            android:layout_height="0dip"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:height="25dip"
            android:width="100dip"
            android:background="@drawable/textview_listrow_border"
            android:text=""
            android:clickable="true"/>
    </LinearLayout>  
     <LinearLayout
          android:orientation="vertical"
          android:layout_height="fill_parent"
          android:layout_width="0dip"
          android:layout_weight="1">    
         <TextView 
            android:id="@+id/tv_listRow_item7"
            android:tag="tv_listRow_itemt1_7"
            android:layout_height="0dip"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:width="100dip"
            android:height="25dip"
            android:background="@drawable/textview_listrow_border"
            android:text=""
            android:clickable="true"/>

        <TextView 
            android:id="@+id/tv_listRow_item8"
            android:tag="tv_listRow_item1_8"
            android:layout_height="0dip"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:height="25dip"
            android:width="100dip"
            android:background="@drawable/textview_listrow_border"
            android:text=""
            android:clickable="true"/>
    </LinearLayout>  
    <LinearLayout
          android:orientation="vertical"
          android:layout_height="fill_parent"
          android:layout_width="0dip"
          android:layout_weight="1">    
         <TextView 
            android:id="@+id/tv_listRow_item9"
            android:tag="tv_listRow_item1_9"
            android:layout_height="0dip"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:width="100dip"
            android:height="25dip"
            android:background="@drawable/textview_listrow_border"
            android:text=""
            android:clickable="true"/>

        <TextView 
            android:id="@+id/tv_listRow_item10"
            android:tag="tv_listRow_item1_10"
            android:layout_height="0dip"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:height="25dip"
            android:width="100dip"
            android:background="@drawable/textview_listrow_border"
            android:text=""
            android:clickable="true"/>
    </LinearLayout>  

    <LinearLayout
          android:orientation="vertical"
          android:layout_height="fill_parent"
          android:layout_width="0dip"
          android:layout_weight="1">    
         <TextView 
            android:id="@+id/tv_listRow_item11"
            android:tag="tv_listRow_item1_11"
            android:layout_height="0dip"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:width="100dip"
            android:height="25dip"
            android:background="@drawable/textview_listrow_border"
            android:text=""
            android:clickable="true"/>

        <TextView 
            android:id="@+id/tv_listRow_item12"
            android:tag="tv_listRow_item1_12"
            android:layout_height="0dip"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:height="25dip"
            android:width="100dip"
            android:background="@drawable/textview_listrow_border"
            android:text=""
            android:clickable="true"/>
    </LinearLayout>  

  <LinearLayout
          android:orientation="vertical"
          android:layout_height="fill_parent"
          android:layout_width="0dip"
          android:layout_weight="1">    
         <TextView 
            android:id="@+id/tv_listRow_item13"
            android:tag="tv_listRow_item1_13"
            android:layout_height="0dip"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:width="100dip"
            android:height="25dip"
            android:background="@drawable/textview_listrow_border"
            android:text=""
            android:clickable="true"/>

        <TextView 
            android:id="@+id/tv_listRow_item14"
            android:tag="tv_listRow_item1_14"
            android:layout_height="0dip"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:height="25dip"
            android:width="100dip"
            android:background="@drawable/textview_listrow_border"
            android:text=""
            android:clickable="true"/>
    </LinearLayout>  
    <!-- android:text="@string/tv_list_item8" -->

    </LinearLayout>
4

2 回答 2

1

只需使用

TextView tv = (TextView) rootView.findViewById(R.id.tv_listRow_item1); 

应该可以正常工作...

于 2013-01-29T11:06:11.157 回答
0

您可以在 onitemclick 中访问您的视图

OnItemClickListener ClickListener = new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View v, int arg2,long arg3) {
         TextView tv = (TextView) v.findViewById(R.id.tv_listRow_item1); 
    }
}; 

否则你需要创建类扩展基本适配器并在 getview 方法中膨胀你的 xml 视图

于 2013-01-29T11:38:25.833 回答