我的fragment_main中的文本视图应该显示字符串,而是显示包名称。
在我的主要活动类中,
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
ListView main_list;
ArrayAdapter<Cinema> adapter;
ArrayList<Cinema> ItemList = new ArrayList<Cinema>();
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.v("Check", "1");
final View rootView = inflater.inflate(R.layout.fragment_main, container, false);
ItemList.addAll(initialization());
ListView main_list = (ListView) rootView.findViewById(R.id.mainlist);
adapter = new ListAdapater(rootView.getContext(), ItemList);
main_list.setAdapter(adapter);
main_list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// TODO Auto-generated method stub
Intent i = new Intent(rootView.getContext(), CinemaDetails.class);
Bundle b = new Bundle();
b.putParcelable("Cinema", ItemList.get(position));
i.putExtra("Bundle", b);
startActivity(i);
}
});
return rootView;
}
public ArrayList<Cinema> initialization () {
ArrayList<Cinema> tempItemList = new ArrayList<Cinema>();
ArrayList<Movie> movielistone = new ArrayList<Movie>();
Movie movieone = new Movie(101, "MovieOne", "null", 5, "nULL");
Movie movietwo = new Movie(102, "Movietwo", "null", 3, "nuLL");
movielistone.add(movieone);
movielistone.add(movietwo);
Cinema cinemaone = new Cinema(01, "test", "ygn", 55555, movielistone, 3);
Cinema cinematwo = new Cinema(02, "test2", "ygn", 554555, movielistone, 3);
tempItemList.add(cinemaone);
tempItemList.add(cinematwo);
return tempItemList;
}
}
我使用Array Adapter,代码是
public class ListAdapater extends ArrayAdapter<Cinema> {
private LayoutInflater inflater;
public ListAdapater(Context context, List<Cinema> itemList) {
super(context, R.layout.row, R.id.txt_cinemaname, itemList );
inflater = LayoutInflater.from(context) ;
// TODO Auto-generated constructor stub
}
@Override
public Cinema getItem(int position) {
// TODO Auto-generated method stub
return super.getItem(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Cinema cinema = this.getItem(position);
TextView txt_name;
TextView txt_movie;
if (convertView == null) {
convertView = inflater.inflate(R.layout.row, null);
txt_name = (TextView) convertView.findViewById(R.id.txt_cinemaname);
txt_movie = (TextView) convertView.findViewById(R.id.txt_moviesonshow);
convertView.setTag(new ItemViewHolder(txt_name, txt_movie));
} else {
ItemViewHolder viewHolder = (ItemViewHolder) convertView.getTag();
txt_name = viewHolder.getName();
txt_movie = viewHolder.getMoviename();
}
txt_name.setText("test");
Log.v("name", txt_name.getText().toString());
txt_movie.setText("");
for (Movie movie : cinema.getMovieonshow()) {
String temp = txt_movie.getText().toString();
txt_movie.setText(temp + movie.getName() + "\n");
}
return super.getView(position, convertView, parent);
}
public static class ItemViewHolder {
private TextView name;
private TextView moviename;
public ItemViewHolder() {}
public ItemViewHolder(TextView name, TextView moviename) {
super();
this.name = name;
this.moviename = moviename;
}
public TextView getName() {
return name;
}
public void setName(TextView name) {
this.name = name;
}
public TextView getMoviename() {
return moviename;
}
public void setMoviename(TextView moviename) {
this.moviename = moviename;
}
}}
我之前从未遇到过这个问题。我绑定了从TextView记录文本,它显示“测试”就像我把它放在那里。有什么建议吗?谢谢你的阅读。
答案 0 :(得分:3)
问题是
return super.getView(position, convertView, parent);
默认情况下, ArrayAdapter
使用TextView
的结果填充指定资源ID的R.id.txt_cinemaname
视图(在本例中为item.ToString()
)(其中item
}是支持数组中指定位置的对象。
由于Cinema
未实现toString()
,因此会生成默认文本。
使用return convertView;
应该修复它。
答案 1 :(得分:1)
我无法弄清楚,但为什么要在ListAdapter构造函数中传入R.id.txt_cinemaname
?问题看起来像是在传入一个Object(该字符串“com.example ...是指向一个对象的指针,我猜一个TextView”而不是一个字符串。
此外,您不应该在convertView
方法中返回super.getView(position, convertView, parent);
而不是getView()
吗?