我正在尝试使用BaseAdapter填充列表视图,我遇到的问题是显示数据的textview,拒绝实际绘制文本。 我在textview上调用了getText(),它确实显示了正确的信息,但由于某种原因,它只是没有被渲染。
CustomAdapter:
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;
}
}
MainActivity:
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_width="match_parent"
android:layout_height="match_parent" 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_width="wrap_content"
android:layout_height="wrap_content"
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_width="match_parent" android:layout_height="wrap_content"
android:background="@drawable/bg_card">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/numbertitle"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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错误,但结果未更改。
编辑2:问题是白色背景列表视图和白色文本
在评论中提供对@ N1to的回复。
答案 0 :(得分:1)
你有代码:
this.listview = (ListView)findViewById(R.id.Listview);
在您的XML中,您的ID不同:
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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);
}
}
}