Android中的行布局未膨胀
我有个问题。我几周前开始学习Android。我正在尝试为列表创建适配器。但这些名单根本没有出现。你知道为什么吗?这些项目是对象类型的动词。这是我的密码: ///我的主要活动///Android中的行布局未膨胀,android,list,layout,android-inflate,Android,List,Layout,Android Inflate,我有个问题。我几周前开始学习Android。我正在尝试为列表创建适配器。但这些名单根本没有出现。你知道为什么吗?这些项目是对象类型的动词。这是我的密码: ///我的主要活动/// public class Verbs extends Activity { private ListView listViewVerbs; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstan
public class Verbs extends Activity {
private ListView listViewVerbs;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verbs);
//instantiate Verb
Verb[] VerbData=new Verb[3];
VerbData[0] = new Verb("machen","mache","machen","mache","machen","mache","machen");
VerbData[1] = new Verb("machen","mache","machen","mache","machen","mache","machen");
VerbData[2] = new Verb("machen","mache","machen","mache","machen","mache","machen");
//pass data to adapter
VerbAdapter adapter = new VerbAdapter(this,R.layout.row_verbs,VerbData);
listViewVerbs = (ListView)findViewById(R.id.list);
listViewVerbs.setAdapter(adapter);
listViewVerbs.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String wasClicked = view.findViewById(R.id.nameVerb).toString();
Toast.makeText(Verbs.this,
"You clicked: " + wasClicked, Toast.LENGTH_LONG)
.show();
}
});
}
}
//适配器
public class VerbAdapter extends ArrayAdapter<Verb>{
Context mContext;
int layoutResourceId;
Verb data[] = null;
public VerbAdapter(Context mContext, int layoutResourceId, Verb[] data){
super(mContext, layoutResourceId, data);
this.mContext = mContext;
this.layoutResourceId =layoutResourceId;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
if (convertView == null){
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId,parent, false);
}
TextView textView0 = (TextView) convertView.findViewById(R.id.nameVerb);
TextView textView1 = (TextView) convertView.findViewById(R.id.firstpersonsg);
TextView textView2 = (TextView) convertView.findViewById(R.id.firstpersonpl);
TextView textView3 = (TextView) convertView.findViewById(R.id.secondpersonsg);
TextView textView4 = (TextView) convertView.findViewById(R.id.secondpersonpl);
TextView textView5 = (TextView) convertView.findViewById(R.id.thirdpersonsg);
TextView textView6 = (TextView) convertView.findViewById(R.id.thirdpersonpl);
Verb verb = data[position];
textView0.setText(verb.verbname);
textView1.setText(verb.fpersg);
textView2.setText(verb.fperspl);
textView3.setText(verb.sperssg);
textView4.setText(verb.sperspl);
textView5.setText(verb.tperssg);
textView6.setText(verb.tperspl);
return convertView;
}
}
公共类VerbAdapter扩展了ArrayAdapter{
语境;
国际布局资源;
动词数据[]=null;
公共VerbAdapter(上下文mContext、int-layoutResourceId、谓词[]数据){
super(mContext、layoutResourceId、data);
this.mContext=mContext;
this.layoutResourceId=layoutResourceId;
这个数据=数据;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
if(convertView==null){
LayoutFlater充气器=((活动)mContext.getLayoutFlater();
convertView=充气机。充气(layoutResourceId,父项,false);
}
TextView textView0=(TextView)convertView.findViewById(R.id.nameVerb);
TextView textView1=(TextView)convertView.findViewById(R.id.firstpersonsg);
TextView textView2=(TextView)convertView.findViewById(R.id.firstpersonpl);
TextView textView3=(TextView)convertView.findViewById(R.id.secondpersonsg);
TextView textView4=(TextView)convertView.findViewById(R.id.secondpersonpl);
TextView textView5=(TextView)convertView.findViewById(R.id.thirdpersonsg);
TextView textView6=(TextView)convertView.findViewById(R.id.thirdpersonpl);
动词=数据[位置];
textView0.setText(动词.verbname);
textView1.setText(动词.fpersg);
textView2.setText(动词.fperspl);
textView3.setText(动词.sperssg);
textView4.setText(动词.sperspl);
textView5.setText(动词.tperssg);
textView6.setText(动词.tperspl);
返回视图;
}
}
------以及两个名为activity_verbs的XML文件,其中声明了列表和row_verbs,其中声明了每个项的布局-----
row_verbs.xml------
另一个是activity_verbs.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="${relativePackage}.${activityClass}"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textSize="20sp"
android:padding="10dp"
android:background="#7663d5"
android:textColor="#f8f8f8"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lessonId"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#292929"
android:textColor="#f0f0f0"
android:padding="10dp"/>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/lessonId"/>
</RelativeLayout>
我只做了一次更改就复制了您的代码: 我搬走了
tools:context="${relativePackage}.${activityClass}"
从活动中,它对我来说就像一个符咒:
我已将我的测试项目和您的代码上载到DropBox,请随意下载:
问题结构非常好,干得好
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="${relativePackage}.${activityClass}"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textSize="20sp"
android:padding="10dp"
android:background="#7663d5"
android:textColor="#f8f8f8"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lessonId"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#292929"
android:textColor="#f0f0f0"
android:padding="10dp"/>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/lessonId"/>
</RelativeLayout>
tools:context="${relativePackage}.${activityClass}"