2014-09-03 4 views
1

그래서 사용자 지정 ArrayAdapter를 사용하여 사용자 지정 ListView를 만들려고합니다. ListView를로드하는 조각이있어서 CardsAdapter이라는 자체 ArrayAdapter를 만들고 설정합니다.사용자 지정 ArrayAdapter가있는 ListView가 항목을 표시하지 않습니다.

문제가 있습니까? 목록이 표시되지 않습니다. 목록이 비어 있으면 표시하도록 설정된 기본 텍스트 (android : id = "@ android : id/empty") 만 표시됩니다.

이 방법을 테스트하는 데있어 cards.createDummy();은 표시 할 더미 개체 3 개로 목록을 채 웁니다. 목록이 비어 있으면 내가 선언하고 나는 목록을 가지고

public class MainFragment extends Fragment implements Callback{ 

private ListView listView; 
private CardsAdapter cardsAdapter; 
private CardList cards; //contains the array to be shown 

public MainFragment() { 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    cards = new CardList(getActivity().getApplicationContext()); 
    cards.createDummy(); //Fills up dummy list 

    View view = inflater.inflate(R.layout.fragment_main, container, false); 
    listView = (ListView) view.findViewById(R.id.list); 

    //Creates the adapter with the dummy list cards.cardList 
    cardsAdapter = new CardsAdapter(getActivity().getApplicationContext(),cards.cardList); 
    listView.setAdapter(cardsAdapter); 

    //don't bother with this 
    HttpHandle handle = new HttpHandle(listView, this); 
    handle.download("http://someapp.herokuapp.com/"); 

    // Inflate the layout for this fragment 
    return view; 
} 

목록보기의 레이아웃 파일 (기자 R.layout.fragment_main에) 목록보기 및 텍스트 뷰를 만들

조각이 표시된다 :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="#E6E6E6" > 

<ListView 
    android:id="@+id/list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" /> 

<TextView 
    android:id="@android:id/empty" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#E6E6E6" 
    android:text="Retrieving data"/> 

</RelativeLayout> 

내 사용자 정의 ArrayAdapter와 :

public class CardsAdapter extends ArrayAdapter<Card> { 
public LinearLayout cardView; 

public CardsAdapter(Context context, ArrayList cards) { 
    super(context, R.layout.card, cards); 
} 

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

    if(rowView == null) { 
     LayoutInflater inflater = (LayoutInflater) this.getContext(). 
      getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     rowView = inflater.inflate(R.layout.card, parent, false); 
    } 

    Card card = getItem(position); 

    this.cardView = (LinearLayout) rowView.findViewById(R.id.tableRow1); 

    TextView title = (TextView) rowView.findViewById(R.id.title); 
    title.setText(card.getTitle()); 

    return rowView; 
} 
} 

CardsAdapter 사용하는 카드 레이아웃 파일 (R.layout.card로 생성자에서 참조) : res/layout/card.xml을 :

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

<TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#FFFFFF" 
     android:layout_marginBottom="7dp" 
     > 

     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="85dp" 
      android:layout_height="wrap_content" 
      android:adjustViewBounds="True" 
      android:paddingTop="7dp" 
      android:paddingBottom="7dp" 
      android:paddingLeft="7dp" 
      android:src="@drawable/photo" /> 

     <TextView 
      android:id="@+id/title" 
      android:layout_width="240dp" 
      android:layout_height="wrap_content" 
      android:paddingTop="2dp" 
      android:paddingLeft="3dp" 
      android:maxLines="2" 
      android:ellipsize="end" 
      android:text="@string/title"/> 

     <TextView 
      android:id="@+id/dateStart" 
      android:layout_width="240dp" 
      android:layout_height="wrap_content" 
      android:paddingTop="2dp" 
      android:paddingLeft="3dp" 
      android:maxLines="2" 
      android:ellipsize="end"/> 
    </TableRow> 

    </LinearLayout> 

참고 : 더미 목록이 생성되고 비어 있지입니다 : 내가 디버그 도구 을 함께 확인 앱이 실행되고 충돌하지 않습니다. 그러나 그것은 단지 ListView에이 비어있는 경우 당신은 보이지 위해 텍스트 뷰의 가시성을 전환하지 않았다

+0

* xml * 파일을 표시하고 목록이 emtpy인지 여부를 확인하는 코드는 ** textView **의 표시 여부를 전환합니다. –

+0

textview의 ID를 'android : id = "@ android : id/empty " ', 안드로이드는 목록이 비어 있으면이 텍스트를 보여줍니다. 자동입니다. id는 '@ + id/empty'가 아니라 '@android : id/empty'인 안드로이드 기본값입니다. – Pedro

답변

1

빈 텍스트 뷰를 보여준다는 의 ListView를 텍스트 뷰를 제거하고 응용 프로그램을 실행하려고 나타나야합니다. 당신이 당신의 활동을해야 하나 빈 텍스트 뷰를 사용

ListViewActivity을 확장하거나 가시성의 전환을 처리 할 수 ​​listView.setEmptyView(youTextView);를 사용하여 JavaListView에의 빈 텍스트 뷰을 설정해야합니다.

+1

visiblity = "gone"로 해결했습니다. 정말 고맙습니다! – Pedro

+0

listview가 실제로 비어 있으면 빈 텍스트 뷰가 표시되도록 내 대답 업데이트를 확인하십시오. –

관련 문제