Listview BaseAdapter 数据未显示
Posted
技术标签:
【中文标题】Listview BaseAdapter 数据未显示【英文标题】:Listview BaseAdapter Data not being shown 【发布时间】:2015-09-26 00:23:06 【问题描述】:我正在尝试使用 BaseAdapter 填充列表视图,我遇到的问题是显示数据的文本视图,拒绝实际绘制文本。 我在 textview 上调用了 getText(),它确实显示了正确的信息,但是由于某种原因,它根本没有被渲染。
自定义适配器:
public class CardListviewAdapter extends BaseAdapter
private Context context;
private ArrayList<String> numbers;
private LayoutInflater layoutInflater;
public CardListviewAdapter(Context context, ArrayList<String> numbers)
this.context = context;
this.numbers = numbers;
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@Override
public int getCount()
return numbers.size();
@Override
public Object getItem(int position)
return numbers.get(position);
@Override
public long getItemId(int position)
return 0;
@Override
public View getView(int position, View convertView, ViewGroup parent)
ViewHolder holder = new ViewHolder();
if(convertView == null)
Log.i("Inflating","Inflating Layout...");
convertView = layoutInflater.inflate(R.layout.conversation,null);
holder.numbers = (TextView)convertView.findViewById(R.id.numbertitle);
Log.e("Number",holder.numbers.toString());
convertView.setTag(holder);
Log.i("Item:", "" + numbers.get(position));
holder.numbers.setText(numbers.get(position));
return convertView;
public final class ViewHolder
public TextView numbers;
主活动:
public class ConversationThreads extends AppCompatActivity
private ListView listview;
CardListviewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_conversation_threads);
this.listview = (ListView)findViewById(R.id.conversationListview);
populateConversations();
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_conversation_threads, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
private void populateConversations()
Log.d("Populate", "Populating...");
//Array of Strings returned -- Target Data
ArrayList<String> numbers = TSMS.getConversationNumbers(getApplicationContext());
adapter = new CardListviewAdapter(getApplicationContext(),numbers);
listview.setAdapter(adapter);
Listview.xml 布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_
android:layout_ android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:layout_gravity="center_horizontal"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".CThreads"
android:id="@+id/conversationThreadLayout">
<ListView
android:layout_
android:layout_
android:id="@+id/conversationListview" />
</RelativeLayout>
conversation.xml 布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_ android:layout_
android:background="@drawable/bg_card">
<TextView
android:layout_
android:layout_
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/numbertitle"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_
android:layout_
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/lastmessage"
android:layout_below="@+id/numbertitle"
android:layout_centerHorizontal="true" />
</RelativeLayout>
编辑:修复了 Listview ID 错误,但结果未更改。Edit2:问题是白色背景列表视图和白色文本。 回复@N1to,在 cmets 中提供。
【问题讨论】:
您的列表视图参考是 R.id.conversationListview。此外,在 onCreate 中初始化您的 ArrayList 对象 感谢@m0rpheu5,但是数据仍然没有显示在列表视图项目中。 【参考方案1】:你有代码:
this.listview = (ListView)findViewById(R.id.Listview);
在您的 XML 中,您的 id 是不同的:
<ListView
android:layout_
android:layout_
android:id="@+id/conversationListview" />
另外,把它移到你的activity_conversation_threads.xml
,它不需要单独的布局。
添加到类 `ConversationThreads:
private ArrayList<String> numbers;
private CardListviewAdapter adapter;
然后将您的方法更改为:
private void populateConversations()
Log.d("Populate", "Populating...");
//Array of Strings returned -- Target Data
numbers = TSMS.getConversationNumbers(getApplicationContext());
Log.d("ArrayList size", String.valueOf(numbers.size()); // check if it returns non zero
adapter = new CardListviewAdapter(getApplicationContext(),numbers);
listview.setAdapter(adapter);
适配器代码
public class CardListViewAdapter extends BaseAdapter
private Context context;
private ArrayList<String> numbers;
public CardListViewAdapter(Context context, ArrayList<String> numbers)
this.context = context;
this.numbers = numbers;
@Override
public int getCount()
return numbers.size();
@Override
public Object getItem(int position)
return numbers.get(position);
@Override
public long getItemId(int position)
return 0;
@Override
public View getView(int position, View convertView, ViewGroup parent)
View rowView = convertView;
if (rowView == null)
LayoutInflater layoutInflater = LayoutInflater.from(context);
rowView = layoutInflater.inflate(R.layout.conversation, null);
ViewHolder viewHolder = new ViewHolder(rowView);
rowView.setTag(viewHolder);
final ViewHolder holder = (ViewHolder) rowView.getTag();
holder.numbers.setText(numbers.get(position));
return rowView;
public class ViewHolder
private final TextView numbers;
public ViewHolder(View v)
this.numbers = (TextView) v.findViewById(R.id.numbertitle);
【讨论】:
感谢回复,我已经调整了ListView ID的错误,并检查了arraylist的大小,(它是正确的大小)和内容(也正确)。但是文本仍然没有呈现。感谢列表视图修复。 试试我的适配器并告诉结果。 我试过你提供的适配器,很遗憾结果没有变化。 所以它在内存中,但它不显示。您是否尝试使用不同的背景、字体颜色等来确保您没有出现“白色背景上的白色文字”的情况? 我的上帝....我不敢相信我没有想到白色背景上的白色文字,是的,这就是问题所在,非常感谢@N1to :)以上是关于Listview BaseAdapter 数据未显示的主要内容,如果未能解决你的问题,请参考以下文章
使用 BaseAdapter 在 ListView 中显示 ArrayList
android:ListView缓存机制及BaseAdapter的三重境界(逗比式,普通式,文艺式)