0

我沒有綁定到使用ArrayAdapter,但我想要的是一個列表,其中每個項目包含一個ImageView,一個TextView和一個SeekBar,以便我可以將圖像/文本更新爲以及從每個滑塊接收回調(顯然,我需要能夠動態添加和刪除行)。SeekBar在使用ArrayAdapter的ListActivity

我沒有創建包含這些項目的列表,這些項目是靜態的 - >即在我的列表的每一行的xml佈局中定義。但是,有了這個,我無法「覆蓋'onStartTrackingTouch()','onProgressChanged()'等回調方法。我也可以創建一個「只包含搜索條」的列表,我可以獲得回調。問題是當我帶入LayoutInflater,然後嘗試將我的查找條添加到佈局。

經過幾天的研究和在互聯網上搜索,我仍然無法看到一個解決方案,有一個listactivity使用layoutinflator來創建複合列表行,包括一個imageview,textview和seekbar,我可以正確溝通。

有沒有人有解決方案?碼?

到目前爲止,我已經得到了我所付出的是這樣的:

public class MyListActivity extends ListActivity 
{ 
    private class MyView { 
     public ImageView myImageView = null; 
     public TextView myTextView = null; 
     public SeekBar mySeekBar = null;   
    } 

    private class MyAdapter extends ArrayAdapter<MyView> {     
    private ArrayList<MyView> arrayList; 
    private MyListActivity theContext = null; 

    public MyAdapter(MyListActivity context, 
        int textViewResourceId, 
        ArrayList<MyView> arrayListPassedIn) { 
     super(context, textViewResourceId, arrayListPassedIn); 
     this.volumeControls = arrayListPassedIn; 
     theContext = context; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 

     if (v == null) { 
      LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);     
      v = vi.inflate(R.layout.dont_think_i_should_need_to_use_a_resource_here_cuz_it_should_be_generated_or_dynamic, parent, false); 
     } 

     arrayList.get(position).myImageView = new ImageView(theContext); 
     arrayList.get(position).myImageView.setBackgroundResource(R.drawable.generic_image); 

     if(position == 0) 
     { 
      arrayList.get(position).mySeekBar = new SeekBar(theContext);   
      arrayList.get(position).mySeekBar.setMax(100); 
      arrayList.get(position).mySeekBar.setEnabled(true); 
      arrayList.get(position).mySeekBar.setOnSeekBarChangeListener(
       new SeekBar.OnSeekBarChangeListener() {       
        @Override 
        public void onStopTrackingTouch(SeekBar seekBar) { 
         Log.d("test", "0th seekbar touched");     
        } 

        @Override 
        public void onStartTrackingTouch(SeekBar seekBar) { 
         Log.d("test", "0th seekbar touch lifted"); 
        } 

        @Override 
        public void onProgressChanged(SeekBar seekBar, int arg1, boolean fromUser) { 
         Log.d("test", "0th seekbar progress changed");           
        } 
       } 
      );         
     } 
     else 
     {    
      arrayList.get(position).mySeekBar = new SeekBar(theContext);   
      arrayList.get(position).mySeekBar.setMax(100); 
      arrayList.get(position).mySeekBar.setEnabled(true); 
      arrayList.get(position).mySeekBar.setOnSeekBarChangeListener(
       new SeekBar.OnSeekBarChangeListener() {       
        @Override 
        public void onStopTrackingTouch(SeekBar seekBar) { 
         Log.d("test", "*not* 0th seekbar touched");     
        } 

        @Override 
        public void onStartTrackingTouch(SeekBar seekBar) { 
         Log.d("test", "*not* 0th seekbar touch lifted"); 
        } 

        @Override 
        public void onProgressChanged(SeekBar seekBar, int arg1, boolean fromUser) { 
         Log.d("test", "*not* 0th seekbar progress changed");           
        } 
       } 
      ); 
     } 

     //***this is the fuzzy part -> I need to be able to obtain the layout for the 'parent ViewGroup' or something 
     //to where I can somehow add my seekbar (and textview and imageview) and then return the 'parent view' 
     //that is to say that, this method is required to return a View and I want to return a View that composits 
     //my imageview, textview, and seekbar. One main problem seems to be that an ArrayAdapter won't allow me to add 
    //child views dynamically. 
     final View viewToAdd = arrayList.get(position). mySeekBar;    
     LinearLayout layout = ???;//new LinearLayout(theContext);    
     layout.post(new Runnable() { 
      public void run() { 
       layout.addView(viewToAdd); 
       layout.destroyDrawingCache(); 
       layout.buildDrawingCache(); 
      } 
     }); 

     return ???; //layout.view?? Can I obtain the 'parent view' (the view for the entire row) somehow to return? 
    }   
} 

private ArrayList<MyView> myArrayList; 
    private MyAdapter myAdapter; 

@Override 
    public void onCreate(Bundle savedInstanceState) { 
    try {   
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.layout_for_the_list_activity_all_it_contains_is_a_linearlayout_containing_the_listview); 
     myArrayList = new ArrayList<MyView>(); 
     myArrayList.add(new MyView()); 
     myArrayList.add(new MyView()); 
      mAdapter = new SettingsListViewArrayAdapter(this, R.layout.wtf_not_sure_what_I_should_put_here, this.myArrayList); 
      setListAdapter(mAdapter);   
    } catch (NullPointerException e) { 
     Log.v(getString(R.string.app_name), e.getMessage()); 
    } 
    } 
} 

對於所有我知道我wayyy偏離軌道。從外觀來看,這甚至不可能?有人請幫忙!? (並且預先感謝= -D)

P.S.我真的不在乎如何解決這個問題,我只需要一個「動態調整大小」的列表,其中每個項目包含一個imageview,textview和seekbar,並且我可以修改每個項目並接收每個單獨的seekbar的回調。 (如果解決方案規模化,這將是很酷的,但絕對不是要求,因爲列表通常可能只包含少於20個項目)

+0

好的,我想我終於明白了(在這篇文章發佈後不久你就不知道了)。使用指南: http://www.vogella.de/articles/AndroidListView/article.html#listsactivity 它似乎現在正在工作。 – fatfreddyscat 2011-06-02 16:48:06

回答