2015-06-23 46 views
-3

我在網絡中找到了一個例子,並模仿示例。該示例可以正確運行,但我無法看到contentView.below中的listView是我的代碼我嘗試使用baseAdapter來創建一個listView,但它不起作用

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控件,它的工作,我可以看到在內容查看列表視圖。 我做了調試程序(BaseAdapter),然後發現listView不是空視圖,它有一個名爲mAdapter的屬性,它具有我的信息。爲什麼我在contentView中看不到listView? 感謝您的幫助。

+1

getCount將方法返回0,這就是爲什麼ListView控件將保持爲空 –

+0

@GopalSinghSirvi它的工作,謝謝。 –

回答

1

請改變你的getCount()作爲

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