片段内的自定义列表视图

时间:2013-09-09 06:06:27

标签: android listview android-fragments

我正在使用片段来显示自定义列表视图。问题是它没有表现出来。但是,如果我使用普通的listview它可以工作。我知道我可以使用listfragment。但我需要一些片段布局的按钮。

此外,我还使用tabHost管理这些片段。

class Contactos extends Fragment 

private ListView lstContactos;
private AdLstContactos adListaContactos;

@Override
public void onActivityCreated(Bundle state) {
    super.onActivityCreated(state);
    displayListView();
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    if(container == null){
        return null;
    }
    return inflater.inflate(R.layout.activity_contactos, container, false);
}

// Aqui se hace los findview
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    // lstContactos = (ListView)view.findViewById(R.id.lstContactos);

    // adLstContatos = new LstContactos(view.getContext());

    // lstContactos.setAdapter(adLstContatos);
}

private void displayListView() {

    // create an ArrayAdaptar from the String Array
    adListaContactos = new AdLstContactos(getActivity().getApplicationContext());
    ListView listView = (ListView) getActivity()
            .findViewById(R.id.lstContactos);
    // Assign adapter to ListView
    listView.setAdapter(adListaContactos);
}


public class AdLstContactos extends ArrayAdapter<String>

private Context context;
private  ArrayList<Contactos> datos;
private  List<String> urlList;
private TextView txtNombre, txtNumero;

public AdLstContactos(Context contexto) {
    super(contexto, R.layout.adaptador_lst_contactos);
    this.context = contexto;
    //this.datos = datos;
    urlList = new ArrayList<String>();
      urlList.add("test1");
      urlList.add("test1");
      urlList.add("test1");
}

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
        // Create a new view into the list.
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.adaptador_lst_contactos, parent, false);

        txtNombre = (TextView)rowView.findViewById(R.id.txtNombreC);
        txtNumero = (TextView)rowView.findViewById(R.id.txtNumeroC);

        txtNombre.setText(urlList.get(position));

        txtNumero.setText(urlList.get(position));
        System.out.println("getView");

    return rowView;
}

主要活动

private FragmentTabHost mTabHost;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mTabHost = new FragmentTabHost(this);
    setContentView(mTabHost);

    mTabHost.setup(this, getSupportFragmentManager(),
            R.layout.activity_principal);
    mTabHost.addTab(mTabHost.newTabSpec("DatosPersonales")
            .setIndicator("Your Info"),DatosPersonales.class,null);

    mTabHost.addTab(mTabHost.newTabSpec("Contactos")
            .setIndicator("Contacts"),Contactos.class,null);
}

xml Contactos

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:text="Button" />

<ListView
    android:id="@+id/lstContactos"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button1"
    android:layout_below="@+id/button1" >
</ListView>

2 个答案:

答案 0 :(得分:0)

您的代码可能有错误。 请记住,如果您使用自定义列表,则必须执行以下操作:

  • 创建由ArrayAdapter或BaseAdapter
  • 扩展的自定义适配器
  • 覆盖适配器中的“getView()”函数
  • 将一些数据添加到适配器
  • 按功能“list.setAdapter()”
  • 将适配器添加到列表中

它应该可以工作,但如果没有,那么是一个错误,请添加您的代码。 Here you have example

答案 1 :(得分:0)

Contactos

private void displayListView() {
      List<String> urlList;
    urlList = new ArrayList<String>();
      urlList.add("http://www.google.com");
      urlList.add("http://mail.google.com");
      urlList.add("http://maps.google.com");
    // create an ArrayAdaptar from the String Array
    adListaContactos = new AdLstContactos(getActivity().getApplicationContext(),urlList);
    ListView listView = (ListView) getActivity()
            .findViewById(R.id.lstContactos);
    // Assign adapter to ListView
    listView.setAdapter(adListaContactos);


}

AdLstContactos

public AdLstContactos(Context contexto, List<String>urlList) {
    super(contexto, R.layout.adaptador_lst_contactos,urlList);
    this.context = contexto;
    this.urlList = urlList;
}