如何解决“java.lang.IllegalStateException:ArrayAdapter要求资源ID为TextView”错误?
代码基于tinder tutorial:
public class arrayadapter extends ArrayAdapter<cards>
{
Context context;
public arrayadapter(Context context, int resource_id, List<cards> items)
{
super(context,resource_id,items);
}
public View getview(int position, View convertview, ViewGroup parent)
{
cards card_item = getItem(position);
if (convertview == null)
{
convertview = LayoutInflater.from(getContext()).inflate(R.layout.item,parent,false);
}
TextView user_name =(TextView) convertview.findViewById(R.id.user_name);
ImageView user_image =(ImageView) convertview.findViewById(R.id.image);
user_name.setText(card_item.getUser_name());
user_image.setImageResource(R.mipmap.ic_launcher); // change in the future with user pic
return convertview;
}
}
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
安卓:layout_gravity="center"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:paddingLeft="40sp"
安卓:paddingRight="40sp"
安卓:paddingTop="20sp"
安卓:paddingBottom="20sp"
安卓:outlineProvider="bounds"
安卓:clipToPadding="false"
>
<安卓x.cardview.widget.CardView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_gravity="center"
app:cardCornerRadius="6dp"
安卓:elevation="2dp"
安卓:id="@+id/card_view"
>
<FrameLayout
安卓:layout_gravity="center"
安卓:layout_width="250dp"
安卓:layout_height="170dp"
>
<ImageView
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:id="@+id/image">
</ImageView>
<TextView
安卓:id="@+id/user_name"
安卓:textSize="40sp"
安卓:textColor="@安卓:color/black"
安卓:gravity="center"
tools:text="hello"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent" />
</FrameLayout>
</安卓x.cardview.widget.CardView>
</LinearLayout>
这是swipe、arrayadapter类和项目xml。用户的数据现在并不重要
public class swipe_cards extends AppCompatActivity {
private cards cards_data[] ;
private arrayadapter arrayadapter;
private int i;
ListView listView;
List<cards>rowitems;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe_cards);
Bundle extras = getIntent().getExtras();
rowitems = new ArrayList<cards>();
//arrayadapter = new arrayadapter (this,R.layout.item,rowitems);
//context,YourCustomLayoutID,TextViewIDinYourLayout,ListData
arrayadapter = new arrayadapter(this,R.layout.item,rowitems);
// ArrayAdapter arrayAdapter = new ArrayAdapter(MainActivity.this,
// R.layout.layout_item_autocomplete, R.id.tvCustom, getResources().getStringArray(R.array.sweets));
SwipeFlingAdapterView flingContainer =(SwipeFlingAdapterView)findViewById(R.id.frame);
flingContainer.setAdapter(arrayadapter);
flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
@Override
public void removeFirstObjectInAdapter() {
// this is the simplest way to delete an object from the Adapter (/AdapterView)
Log.d("LIST", "removed object!");
rowitems.remove(0);
arrayadapter.notifyDataSetChanged();
}
@Override
public void onLeftCardExit(Object dataObject) {
//Do something on the left!
//You also have access to the original object.
//If you want to use it just cast it (String) dataObject
Toast.makeText(swipe_cards.this, "Left!",Toast.LENGTH_SHORT).show();
}
@Override
public void onRightCardExit(Object dataObject) {
Toast.makeText(swipe_cards.this, "Right!",Toast.LENGTH_SHORT).show();
}
@Override
public void onAdapterAboutToEmpty(int itemsInAdapter) {
// Ask for more data here
//rowitems.add("XML ".concat(String.valueOf(i)));
arrayadapter.notifyDataSetChanged();
Log.d("LIST", "notified");
i++;
}
@Override
public void onScroll(float scrollProgressPercent) {
}
});
// Optionally add an OnItemClickListener
flingContainer.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() {
@Override
public void onItemClicked(int itemPosition, Object dataObject) {
Toast.makeText(swipe_cards.this, "Clicked!",Toast.LENGTH_SHORT).show();
}
});
if (extras != null)
{
ArrayList<String> users= extras.getStringArrayList("potential_users");
for (int counter = 0; counter < users.size(); counter++) {
String user = users.get(counter);
//String[] user_details= user.split(":");
String[] user_details = user.split(",");
String [] name_parmetrs= user_details[0].split(":");
String user_name= name_parmetrs[1];
cards card = new cards("222",user_name);
rowitems.add(card);
arrayadapter.notifyDataSetChanged();
}
}
}
# 1 楼答案
您正在使用constructor创建一个数组适配器,其中只提供3个参数——上下文、布局Id和行项
因此,对于只有3个参数的构造函数
R.layout.item
必须是只包含TextView的XML布局文件的id(TextView不能被其他布局包装,如LinearLayout、RelativeLayout等!),比如:但在您的情况下,您使用的是一个复杂的布局,因此在这种情况下,您需要在构造函数中传递布局id和文本视图id
如果你需要,你没有
textViewId
,你可以把textViewId
作为0传递像这样
如果你有textview ResourceId,你可以传递ResourceId来代替0