我正在编写一个ListView,每行有2个TextView,但是当我执行时,我只得到一个TextView。
这是我的适配器扩展基本适配器
public class GlossaryListViewAdapter extends BaseAdapter {
List<Glossary_Entry> glossary_entryList;
Context context;
public GlossaryListViewAdapter(Context context,List<Glossary_Entry> glossary_entryList){
this.glossary_entryList = glossary_entryList;
this.context = context;
}
@Override
public int getCount() {
return glossary_entryList.size();
}
@Override
public Object getItem(int postion) {
return glossary_entryList.get(postion);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if(view == null){
view = LayoutInflater.from(context).inflate(R.layout.glossary_single_row,viewGroup,false);
viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)view.getTag();
}
viewHolder.titleTextView.setText(glossary_entryList.get(position).getTitle());
viewHolder.definitionTextView.setText(glossary_entryList.get(position).getDefinition());
return view;
}
private static class ViewHolder{
TextView titleTextView;
TextView definitionTextView;
public ViewHolder(View view){
titleTextView = (TextView)view.findViewById(R.id.glossary_title_textview);
definitionTextView = (TextView)view.findViewById(R.id.glossary_word_definition_textview);
}
}
}
这是我的活动代码
public class GlossaryAcvtivity extends AppCompatActivity {
ListView glossaryListView;
ListAdapter listAdapter;
List<Glossary_Entry> glossary_entryList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_glossary);
/**
* Sample data
*/
glossary_entryList = new ArrayList<>();
glossary_entryList.add( new Glossary_Entry("Force","This is a push or push on an object.They can be duew to a phenomenon such as gravity, magnetism, or anything that might cause a mass to accelerate."));
glossary_entryList.add( new Glossary_Entry("Motion","Change in position of an object over time, described in terms of displacement,distance,velocity,acceleration,time and speed"));
glossary_entryList.add( new Glossary_Entry("Quantum Leap","An Abrupt transition of an electron,atom, or molecule from one quantum state to another,with the absorption or emission of a quantum"));
glossary_entryList.add( new Glossary_Entry("Force","This is a push or push on an object.They can be duew to a phenomenon such as gravity, magnetism, or anything that might cause a mass to accelerate."));
glossaryListView = (ListView)findViewById(R.id.glossary_list_view);
listAdapter = new GlossaryListViewAdapter(getApplicationContext(),glossary_entryList);
glossaryListView.setAdapter(listAdapter);
}
}
我的数据来源是一个Entry类
public class Glossary_Entry {
public String title;
public String definition;
public String getTitle() {
return title;
}
public String getDefinition() {
return definition;
}
public Glossary_Entry(String title,String definition){
this.title = title;
this.definition = definition;
}
}
这是我的glossary_single_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textColor="@color/blue"
android:textStyle="bold"
android:textSize="16sp"
android:layout_margin="4dp"
android:id="@+id/glossary_title_textview"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_description"
android:layout_margin="4dp"
android:textSize="12sp"
android:id="@+id/glossary_word_definition_textview"/>
</LinearLayout>
这是我的glossary_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@color/white"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:id="@+id/glossary_toolbar"
android:background="@color/colorPrimary"
android:layout_height="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
<ListView
android:layout_width="match_parent"
android:id="@+id/glossary_list_view"
android:layout_margin="8dp"
android:layout_below="@+id/glossary_toolbar"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
执行时请帮助我每行只能获得一个TextView ...
答案 0 :(得分:0)
显然,我需要在第二个TextView中将文本颜色更改为黑色。那里的文字不可见。
orderBy: 'key'
android:layout_width =“match_parent” 机器人:layout_height = “WRAP_CONTENT” &GT;
<LinearLayout xmlns:android="http:// schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"