2015-05-24 125 views
0

我實現了一個自定義適配器來填充自定義ListView與一個image和2 TextField。現在我想要做的是添加新的行動態滾動列表。何可以做到這一點?將數據添加到自定義列表視圖滾動列表

這是適配器

public class CustomListAdapter extends ArrayAdapter<String> { 

    private final Activity context; 
    private final String[] itemname; 
    private final Integer[] imgid; 
    private final String[] questions; 

    public CustomListAdapter(Activity context, String[] itemname, Integer[] imgid, String[] questions) { 
     super(context, R.layout.mylist, itemname); 
     // TODO Auto-generated constructor stub 

     this.context = context; 
     this.itemname = itemname; 
     this.imgid = imgid; 
     this.questions=questions; 
    } 

    public View getView(int position, View view, ViewGroup parent) { 
     LayoutInflater inflater = context.getLayoutInflater(); 
     View rowView = inflater.inflate(R.layout.mylist, null, true); 
     rowView.setPadding(0,10,0,10); 

     TextView txtTitle = (TextView) rowView.findViewById(R.id.nameList); 
     txtTitle.setTextColor(Color.BLACK); 
     ImageView imageView = (ImageView) rowView.findViewById(R.id.imgList); 
     TextView extratxt = (TextView) rowView.findViewById(R.id.question); 

     txtTitle.setText(itemname[position]); 
     imageView.setImageDrawable(new RoundImage(BitmapFactory.decodeResource(context.getResources(), imgid[position]))); 
     //imageView.setImageResource(imgid[position]); 
     extratxt.setText(questions[position]); 
     return rowView; 

    }; 
} 

這是在列表中位於

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedBundle) { 
    this.adapter=new CustomListAdapter(getActivity(), itemname, imgid, questions); 
      list=(ListView)firstAccessView.findViewById(R.id.list); 
      list.setAdapter(adapter); 

      list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 


    //stuff 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
           int position, long id) { 
      // TODO Auto-generated method stub 
      String Slecteditem= itemname[+position]; 
      Toast.makeText(getActivity().getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show(); 

     } 
    }); 
} 

回答

1

覆蓋onScroll()方法在你的片段,其具有onCreateView()的的fragment的代碼部分的代碼,實施onScrollListener

將您的問題數組的字符串更改爲ArrayList。您也可以爲其他列表執行此操作。

private ArrayList<String> questions; 

public CustomListAdapter(Activity context, String[] itemname, Integer[] imgid, ArrayList<String> questions) { 
    super(context, R.layout.mylist, itemname); 
    // TODO Auto-generated constructor stub 

    this.context = context; 
    this.itemname = itemname; 
    this.imgid = imgid; 
    this.questions=questions; 
} 

public View getView(int position, View view, ViewGroup parent) { 
    LayoutInflater inflater = context.getLayoutInflater(); 
    View rowView = inflater.inflate(R.layout.mylist, null, true); 
    rowView.setPadding(0,10,0,10); 

    TextView txtTitle = (TextView) rowView.findViewById(R.id.nameList); 
    txtTitle.setTextColor(Color.BLACK); 
    ImageView imageView = (ImageView) rowView.findViewById(R.id.imgList); 
    TextView extratxt = (TextView) rowView.findViewById(R.id.question); 

    txtTitle.setText(itemname[position]); 
    imageView.setImageDrawable(new RoundImage(BitmapFactory.decodeResource(context.getResources(), imgid[position]))); 
    //imageView.setImageResource(imgid[position]); 
    extratxt.setText(questions.get(position)); 
    return rowView; 

}; 
} 

實施onScroll

@Override 
public void onScroll(AbsListView view, int firstVisibleItem, 
     int visibleItemCount, int totalItemCount) { 

//load your new set of data here. 
your_adapter.notifyDataSetChanged(); 
} 
+0

,我怎麼能追加我的數據? – Bella

+0

對你的數據做一些分頁..分數據。那麼你可以使用collection來追加數據。然後通知您的適配器。 – Android

+0

如果我的適配器擴展了ArrayAdapter ,我該如何追加數據。當我嘗試在裏面的方法裏面做myadapter.addAll()時,我可以只放入String,而不是我想把更多的 – Bella

-1
@Override 
    public void onScroll(AbsListView view, int firstVisibleItem, 
      int visibleItemCount, int totalItemCount) { 

    //loadMoreData 

    //if using arrayAdapter, use this 
    for (String i: itemname) { 
     adapter.add(i); 
    } 
    adapter.notifyDataSetChanged(); 
    } 
+0

僅用代碼發佈答案不是答案。請解釋代碼。 –

+0

代碼是明確的沒有nead解釋 –

+0

你可以解釋你改變了什麼,爲什麼這應該工作... –

相關問題