自定义Listview填充项目未显示

时间:2012-12-12 20:43:32

标签: java android listview android-4.2-jelly-bean

我有一个listview,我想显示像zodiac Sign Image这样的自定义数据。十二生肖。十二生肖内容和几个按钮。问题是数据没有显示我调试每一行,项目正确地添加到arraylist然后进入适配器但是listview在屏幕上显示为空。

我正在使用android google api17 jelly beans version

我的主要Xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    app:context=".AdminPage.Hilariouscope" >

    <TableLayout
        android:id="@+id/tableRow_Main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TableRow
            android:id="@+id/tableRow_tittle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <RelativeLayout
                android:id="@+id/Relativelayout_watch"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1" >

                <Button
                    android:id="@+id/btn_topic_horro_Back"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:onClick="GoToHome"
                    android:text="@string/RegisterBack" />

                <TextView
                    android:id="@+id/txt_title_screen"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/btn_topic_horro_Back"
                    android:layout_alignBottom="@+id/btn_topic_horro_Back"
                    android:layout_centerHorizontal="true"
                    android:text="@string/title_activity_hilariouscope"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

            </RelativeLayout>

        </TableRow>

        <ListView
            android:id="@+id/horror_listview"
            android:layout_width="wrap_content"
            android:layout_height="371dp" >
        </ListView>

    </TableLayout>

</ScrollView>

我的ListRow XML

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

<TextView
    android:id="@+id/txt_zodic_Tittle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#ab2130"
    android:textSize="32sp" />

<TextView
    android:id="@+id/txt_zodic_date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/txt_zodic_Tittle"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TableRow
    android:id="@+id/tableRow_content"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/txt_zodic_date" >

    <TextView
        android:id="@+id/txt_main_content"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight=".2"
        android:text="@string/Main_laugh"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="22sp"
        android:textStyle="normal" />

    <ImageView
        android:id="@+id/zodic_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight=".5"
        android:adjustViewBounds="true"
        android:cropToPadding="true"
        android:scaleType="fitCenter"
        android:src="@drawable/lighthouse" />

</TableRow>

<TableRow
    android:id="@+id/tableRow_footer"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true" >

    <RelativeLayout
        android:id="@+id/RelativeLayout03"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:background="#bababa" >

        <Button
            android:id="@+id/Btn_share_fun"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Share"
            app:ignore="HardcodedText" />

        <ImageButton
            android:id="@+id/btn_facebook_fun"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/btn_twitter_fun"
            android:src="@drawable/pic2"
            app:ignore="ContentDescription" />

        <ImageButton
            android:id="@+id/btn_twitter_fun"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/btn_facebook_fun"
            android:src="@drawable/pic3"
            app:ignore="ContentDescription" />
    </RelativeLayout>
</TableRow>

我自定义的Zodic课程

public class Hilariouscope_Items {
private boolean checked;
private int zodicImage;
private String ZodicDate, ZodicTitle, ZodicContent;

public Hilariouscope_Items(String Title, String Date, String Content) {
    this.ZodicTitle = Title;
    // this.zodicImage = ZodicImage;
    this.ZodicDate = Date;
    this.ZodicContent = Content;
}

public void setChecked(boolean checked) {
    this.checked = checked;
}

public boolean isChecked() {
    return checked;
}

public void setTitleName(String name) {
    this.ZodicTitle = name;
}

public void setZodicDate(String date) {
    this.ZodicDate = date;
}

public void setZodicContent(String Content_Text) {
    this.ZodicContent = Content_Text;
}

public String getTitle() {
    return ZodicTitle;
}

public String getZodicDate() {
    return ZodicDate;
}

public String getZodicContent() {
    return ZodicContent;
}

public void toggle() {
    this.checked = !this.checked;
}

public int getZodicImage() {
    return zodicImage;
}

我的ZodicAdapter类

import java.util.List;
import com.example.laysapp.R;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


 public class horrorItemAdapter extends BaseAdapter {

private final List<Hilariouscope_Items> items;

public horrorItemAdapter(final Context context, final int itemResId,
        final List<Hilariouscope_Items> items) {
    this.items = items;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final Hilariouscope_Items item = this.items.get(position);
    View itemView = null;

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) parent.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        itemView = inflater.inflate(R.layout.horror_list_row, null);
    } else {
        itemView = convertView;
    }

    // Set the text of the item
    TextView txtName = (TextView) itemView
            .findViewById(R.id.txt_zodic_Tittle);
    txtName.setText(item.getTitle());

    // Set the text of the item
    TextView txtDate = (TextView) itemView
            .findViewById(R.id.txt_zodic_date);
    txtDate.setText(item.getZodicDate());

    // Set the text of the item
    TextView txtContent = (TextView) itemView
            .findViewById(R.id.txt_main_content);
    txtContent.setText(item.getZodicContent());

    // Set the check-icon
    ImageView imgView = (ImageView) itemView.findViewById(R.id.zodic_image);

    imgView.setImageResource(item.getZodicImage());
    /*
     * imgViewChecked.setImageResource(item.isChecked() ? R.drawable.checked
     * : R.drawable.unchecked);
     */
    return itemView;
}

 }

我的主要课程

    public class Hilariouscope extends Activity {

private ListView mainListView;
private horrorItemAdapter itemAdapter;
private ArrayAdapter<Hilariouscope_Items> listAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hilariouscope);

    ArrayList<Hilariouscope_Items> items = new ArrayList<Hilariouscope_Items>();
    /*
     * items.add(new Hilariouscope_Items("AQUARIUS", R.drawable.hydrangeas,
     * "MAR 21 - APR 19", "sample text Aquarius")); items.add(new
     * Hilariouscope_Items("Aries", R.drawable.jellyfish, "MAR 21 – APR 20",
     * "sample text Aries")); items.add(new Hilariouscope_Items("Taurus",
     * R.drawable.penguins, "APR 21 – MAY 21", "sample text Taurus"));
     * items.add(new Hilariouscope_Items("Gemini", R.drawable.lighthouse,
     * "MAY 22 – JUN 21", "sample text Gemini"));
     */

    items.add(new Hilariouscope_Items("AQUARIUS", "MAR 21 - APR 19",
            "sample text Aquarius"));
    items.add(new Hilariouscope_Items("Aries", "MAR 21 – APR 20",
            "sample text Aries"));
    items.add(new Hilariouscope_Items("Taurus", "APR 21 – MAY 21",
            "sample text Taurus"));
    items.add(new Hilariouscope_Items("Gemini", "MAY 22 – JUN 21",
            "sample text Gemini"));

    itemAdapter = new horrorItemAdapter(getApplicationContext(),
            R.layout.horror_list_row, items);

    mainListView = (ListView) findViewById(R.id.horror_listview);
    // listAdapter = new ArrayAdapter<Hilariouscope_Items>(this,
    // R.layout.horror_list_row, items);
    mainListView.setAdapter(itemAdapter);
    /*
     * View header = (View) getLayoutInflater().inflate(
     * R.layout.horror_list_row, null);
     * 
     * lv.addHeaderView(header);
     */

    mainListView.setTextFilterEnabled(true);
    mainListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapter, View v, int pos,
                long id) {

            // Toggle the state of the item and refresh the view
            /*
             * Hilariouscope_Items item = (Hilariouscope_Items) mainListView
             * .getItemAtPosition(pos);
             * Toast.makeText(getApplicationContext(),
             * item.getTitle().toString(), Toast.LENGTH_SHORT).show(); //
             * itemAdapter.notifyDataSetChanged();
             */
        }

    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_hilariouscope, menu);
    return true;
}

等待您的回复

提前致谢

2 个答案:

答案 0 :(得分:2)

在您的ZodicAdapter类中 getCount()您将返回0。

将其作为

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return items.size();
}

答案 1 :(得分:0)

您正在0中返回getCount(),因此该列表认为适配器中没有任何项目。