使用自定义 listView 适配器设置 TextView 的文本
setting text of TextView , using custom listView Adaptor
我正在尝试使用自定义列表视图适配器来自定义列表视图的 UI。但是我无法设置自定义列表视图的 setTextview。 titleView.setText(tem);
自定义适配器不工作。它没有给出任何错误。它在文本视图中显示空字符串(我从适配器 class 动态设置),并正确显示文本视图,这是在 itemlistView 中硬编码的(显示自定义适配器与片段正确连接)。有人可以帮我解决这个问题吗?
片段class显示列表
public class Help_FromFriend_OfferedFragment extends Fragment {
ListView msgList;
ArrayList<post> details;
AdapterView.AdapterContextMenuInfo info;
private String Preference;
public Help_FromFriend_OfferedFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_help__from_friend__offered, container, false);
msgList = (ListView) rootView.findViewById(R.id.listView_post);
details = new ArrayList<post>();
post Detail;
Detail = new post();
Detail.setPost_heading("Heading 1");
Detail.setPost_owner_name("Bob");
details.add(Detail);
Detail = new post();
Detail.setPost_heading("Heading 2");
Detail.setPost_owner_name("Bob 2");
details.add(Detail);
msgList.setAdapter(new postListViewCustomAdaptor(details, getActivity()));
return rootView;
}
}
片段布局
<FrameLayout 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.Help_FromFriend_OfferedFragment"
tools:showIn="@layout/activity_help__from_friend__offered"
android:background="#999999"
>
<ListView
android:background="#984442"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView_post"
android:layout_gravity="left|top" />
</FrameLayout>
自定义适配器 class
public class postListViewCustomAdaptor extends BaseAdapter {
private ArrayList<post> _data;
Context _c;
postListViewCustomAdaptor (ArrayList<post> data, Context c){
_data = data;
_c = c;
}
public int getCount() {
// TODO Auto-generated method stub
return _data.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return _data.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
if (v == null)
{
LayoutInflater vi = (LayoutInflater)_c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item_post, null);
Log.v("custom Adaptor","v is null ");
}
//ImageView image = (ImageView) v.findViewById(R.id.post_photo);
TextView titleView = (TextView)v.findViewById(R.id.post_title);
TextView ownerNameView = (TextView)v.findViewById(R.id.post_owner_name);
post post_details = _data.get(position);
//image.setImageResource(post_details .getPost_photo());
Log.v("custom Adaptor", "setting postHeading: " + post_details.getPost_heading());
String tem ="faltu";
Log.v("custom Adaptor", "title from View : " + titleView.getText());
titleView.setText(tem);
ownerNameView.setText(post_details .getPost_owner_name());
return v;
}
}
list_item_post.xml(自定义列表项)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content" android:orientation="horizontal" >
<ImageView
android:id="@+id/post_photo"
android:layout_width="wrap_content"
android:layout_height="40dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="oo"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
android:background="#896322" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/post_title"
android:layout_gravity="center"
android:text="ahaha"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
android:background="#003366" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/post_owner_name"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#003366" />
</LinearLayout>
</LinearLayout>
检查 LinearLayout
中 TextView
的布局参数。它们都是 match_parent
宽度,这意味着第一个将第二个和第三个压扁。
使用 layout_width="0dp"
和 layout_weight="1"
使所有 TextView
等宽。
我正在尝试使用自定义列表视图适配器来自定义列表视图的 UI。但是我无法设置自定义列表视图的 setTextview。 titleView.setText(tem);
自定义适配器不工作。它没有给出任何错误。它在文本视图中显示空字符串(我从适配器 class 动态设置),并正确显示文本视图,这是在 itemlistView 中硬编码的(显示自定义适配器与片段正确连接)。有人可以帮我解决这个问题吗?
片段class显示列表
public class Help_FromFriend_OfferedFragment extends Fragment {
ListView msgList;
ArrayList<post> details;
AdapterView.AdapterContextMenuInfo info;
private String Preference;
public Help_FromFriend_OfferedFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_help__from_friend__offered, container, false);
msgList = (ListView) rootView.findViewById(R.id.listView_post);
details = new ArrayList<post>();
post Detail;
Detail = new post();
Detail.setPost_heading("Heading 1");
Detail.setPost_owner_name("Bob");
details.add(Detail);
Detail = new post();
Detail.setPost_heading("Heading 2");
Detail.setPost_owner_name("Bob 2");
details.add(Detail);
msgList.setAdapter(new postListViewCustomAdaptor(details, getActivity()));
return rootView;
}
}
片段布局
<FrameLayout 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.Help_FromFriend_OfferedFragment"
tools:showIn="@layout/activity_help__from_friend__offered"
android:background="#999999"
>
<ListView
android:background="#984442"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView_post"
android:layout_gravity="left|top" />
</FrameLayout>
自定义适配器 class
public class postListViewCustomAdaptor extends BaseAdapter {
private ArrayList<post> _data;
Context _c;
postListViewCustomAdaptor (ArrayList<post> data, Context c){
_data = data;
_c = c;
}
public int getCount() {
// TODO Auto-generated method stub
return _data.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return _data.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
if (v == null)
{
LayoutInflater vi = (LayoutInflater)_c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item_post, null);
Log.v("custom Adaptor","v is null ");
}
//ImageView image = (ImageView) v.findViewById(R.id.post_photo);
TextView titleView = (TextView)v.findViewById(R.id.post_title);
TextView ownerNameView = (TextView)v.findViewById(R.id.post_owner_name);
post post_details = _data.get(position);
//image.setImageResource(post_details .getPost_photo());
Log.v("custom Adaptor", "setting postHeading: " + post_details.getPost_heading());
String tem ="faltu";
Log.v("custom Adaptor", "title from View : " + titleView.getText());
titleView.setText(tem);
ownerNameView.setText(post_details .getPost_owner_name());
return v;
}
}
list_item_post.xml(自定义列表项)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content" android:orientation="horizontal" >
<ImageView
android:id="@+id/post_photo"
android:layout_width="wrap_content"
android:layout_height="40dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="oo"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
android:background="#896322" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/post_title"
android:layout_gravity="center"
android:text="ahaha"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
android:background="#003366" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/post_owner_name"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#003366" />
</LinearLayout>
</LinearLayout>
检查 LinearLayout
中 TextView
的布局参数。它们都是 match_parent
宽度,这意味着第一个将第二个和第三个压扁。
使用 layout_width="0dp"
和 layout_weight="1"
使所有 TextView
等宽。