我的自定义ListView什么都没显示

时间:2013-11-02 09:06:02

标签: java android listview

我是android的新手。我想创建一个自定义listview arrayadapter。我已经按照一些教程,但我的模拟器没有为Custom ListView显示任何内容。谁能帮我弄清楚我的代码出了什么问题?提前谢谢。

这是我的custom_listview_main.xml

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

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/customListView" />

    </LinearLayout>

这是我的custom_listview_row.xml

    <?xml version="1.0" encoding="utf-8"?>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:minHeight="64dp">

     <!-- <ImageView
        android:id="@+id/clv_imageView"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:contentDescription="@string/empty"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="9dp"
        android:layout_alignParentTop="true"/> -->

     <TextView
    android:id="@+id/clv_textView2"
    android:layout_width="97dp"
    android:layout_height="32dp"
    android:layout_alignParentRight="true"
    android:gravity="center_vertical"
    android:text="@string/tv_definition"
    android:textIsSelectable="true" />

    <TextView
    android:id="@+id/clv_textView"
    android:layout_width="97dp"
    android:layout_height="32dp"
    android:layout_alignParentLeft="true"
    android:gravity="center_vertical"
    android:text="@string/tv_word"
    android:textIsSelectable="true" />

    </RelativeLayout>

这是我的MyPerformanceArrayAdapter.java

    import java.util.List;

    import android.app.Activity;
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.TextView;

    public class MyPerformanceArrayAdapter extends ArrayAdapter<DefinitionObject>{
    private List<DefinitionObject> entries;
    private Activity activity;

    public MyPerformanceArrayAdapter(Activity a, int textViewResourceId,         List<DefinitionObject> entries) {
    super(a, textViewResourceId, entries);
    this.entries = entries;
    this.activity = a;
    }

    public static class ViewHolder{
    public TextView item1;
    public TextView item2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    ViewHolder holder;
    if (v == null) {
        LayoutInflater vi =
            (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.custom_listview_row, null);
        holder = new ViewHolder();
        holder.item1 = (TextView) v.findViewById(R.id.clv_textView);
        holder.item2 = (TextView) v.findViewById(R.id.clv_textView2);
        v.setTag(holder);
    }
    else
        holder=(ViewHolder)v.getTag();

    final DefinitionObject custom = entries.get(position);
    if (custom != null) {
        holder.item1.setText(custom.getWord());
        holder.item2.setText(custom.getFav());
    }
    return v;
    }

    }

最后,这是我的名为TempCLV.java的活动

    package com.example.myidictionary;

    import java.util.List;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.ListView;

    public class TempCLV extends Activity {

private MySQLiteDefinitionHelper db;
String tblName = "";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.custom_listview_main);

    Intent msjIntent = getIntent();
    tblName = msjIntent.getStringExtra(WordDefinitionHomeActivity.TABLENAME2);

    }

    public void refresh()
    {
    db = new MySQLiteDefinitionHelper(this);
    final List<DefinitionObject> values = db.getAllWords(tblName);

    ListView mylist = (ListView)findViewById(R.id.customListView);
    MyPerformanceArrayAdapter adapter = new MyPerformanceArrayAdapter(this, R.id.customListView, values);
    mylist.setAdapter(adapter);
    }
    }

2 个答案:

答案 0 :(得分:2)

您需要在refresh中调用onCreate,因为您要将适配器设置为列表视图。

确保您的values有一些数据

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_listview_main);
Intent msjIntent = getIntent();
tblName = msjIntent.getStringExtra(WordDefinitionHomeActivity.TABLENAME2);
refresh();
}

此外,您可以将inflater初始化移动到适配器类的构造函数。无需在getView

中初始化
LayoutInflater vi; 
public MyPerformanceArrayAdapter(Activity a, int textViewResourceId,List<DefinitionObject> entries) {
super(a, textViewResourceId, entries);
this.entries = entries;
vi =(LayoutInflater)a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
}

答案 1 :(得分:0)

请至少在第一次调用刷新方法,在MainActivity'onCreate方法中设置适配器..