package com.example.listview;
import android.app.Activity;
import android.content.Context;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private Context mContext;
private TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1找到listview
ListView listview=(ListView)findViewById(R.id.listview);
//2創建一個adapter對象,以為下面已經申明了一個類
mylistadpater mylistadpater1=new mylistadpater();
//3將adapter設置個listview
listview.setAdapter(mylistadpater1);
}
class mylistadpater extends BaseAdapter{//default base simple
@Override
public int getCount() {
// TODO Auto-generated method stub
//集合裡面又多少條目,告訴listview要顯示多少條目
return 10;
}
//根據position獲取listview上面的bean書局,該方法不影響數據的展示,可以先不實現
@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(int position, View convertView, ViewGroup parent) {
textview=(TextView)findViewById(R.id.TextView);
textview.setText("position:"+position);
return textview;
}
}
}
不報錯,但是安裝到虛擬機上面就立刻崩潰
你這個適配器寫的,你那個getView()返回的是一個item項,你這個返回一個textview是個什麼鬼。因為寫convertview =LayoutInflater.from(MainActivity.this).inflate(R.layout.list_item, null); tv = (TextView) convertView.findViewById(R.id.tv);。。。。。。 return convertView;