0

我是android的新手。我想创建一个自定义列表视图阵列适配器。我遵循了一些教程,但我的模拟器没有显示自定义 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);
    }
    }
4

2 回答 2

2

您需要调用refreshonCreate因为您正在将适配器设置为列表视图。

确保你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();
}

您也可以将充气机初始化移动到适配器类的构造函数中。无需初始化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);  
}
于 2013-11-02T09:14:45.800 回答
0

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

于 2013-11-02T09:15:16.230 回答