我尝试使用baseAdapter创建一个listView,但它不起作用

时间:2015-06-23 04:19:37

标签: android

我在网上找到了一个例子,并模仿了这个例子。例子可以正确运行,但是我看不到contentView中的listView.below是我的代码

activity_main.xml中

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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"
    tools:context="com.example.test7.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</RelativeLayout>

program_list.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="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_gravity="center"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="TextView"
        android:textSize="25dp" />

</LinearLayout>

BaseAdapterTest.java

public class BaseAdapterTest extends BaseAdapter{
String[] texts;
int[] imgs;
Context mContext;
private LayoutInflater inflater;

public BaseAdapterTest(Context context, String[] texts, int[] imgs) {
    mContext = context;
    this.texts = texts;
    this.imgs = imgs;
    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public class Holder {
    TextView tv;
    ImageView img;
}

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

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

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

@Override
public View getView(final int position, View convertView, ViewGroup parent)             {
    Holder holder = new Holder();
    View rowView;
    rowView = inflater.inflate(R.layout.program_list, null);
    holder.tv = (TextView) rowView.findViewById(R.id.textView1);
    holder.img = (ImageView) rowView.findViewById(R.id.imageView1);
    holder.tv.setText(texts[position]);
    holder.img.setImageResource(imgs[position]);
    rowView.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            Toast.makeText(mContext,texts[position],   Toast.LENGTH_LONG).show();
        }               
    });
    return rowView;
}
} 

MainActivity.java

public class MainActivity extends Activity {
ListView lv;
Context context;

public static int[] prgmImages = { R.drawable.ic_launcher,
        R.drawable.ic_launcher, R.drawable.ic_launcher,
        R.drawable.ic_launcher, R.drawable.ic_launcher,
        R.drawable.ic_launcher, R.drawable.ic_launcher,
        R.drawable.ic_launcher, R.drawable.ic_launcher,
        R.drawable.ic_launcher };
public static String[] prgmNameList = { "I", "II", "III", "IV", "V", "VI",
        "VII", "IIX", "IX", "X" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    // ListViewTest.init(MainActivity.this);
    // ListViewTest.getInstance().setAdapter();
    // setContentView(ListViewTest.getInstance());
    setContentView(R.layout.activity_main);

    context = this;

    lv = (ListView) findViewById(R.id.listView);
    lv.setAdapter(new BaseAdapterTest(this, prgmNameList, prgmImages));
}

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

我尝试通过ArrayAdapter创建listView,它工作正常,我可以在contentView中看到listView。 我调试了程序(BaseAdapter),然后发现listView不是一个空视图,它有一个名为mAdapter的属性,它有我的信息。为什么我在contentView中看不到listView? 谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

请将getCount()更改为

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