行布局在Android中没有膨胀

时间:2015-12-06 22:23:50

标签: android list layout android-inflate

我有问题。几个星期前我开始学习Android。我正在尝试为列表创建适配器。但这些名单并没有显示在所有人身上。有什么想法吗?这些项目是Verb的对象类型。这是我的代码:

///我的主要活动///

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 Verb {

public String verbname;
public String fpersg;
public String fperspl;
public String sperssg;
public String sperspl;
public String tperssg;
public String tperspl;

//constructor
public Verb(String s, String s1, String s2, String s3, String s4, String s5,String s6){
    this.verbname = s;
    this.fpersg = s1;
    this.fperspl = s2;
    this.sperssg = s3;
    this.sperspl = s4;
    this.tperssg = s5;
    this.tperspl = s6;
}
//setters

public void setVbName(String vbName){
    this.verbname = vbName;
}
public void setFpersg(String fpersg){
    this.fpersg = fpersg;
}
public void setFperpl(String fperpl){
    this.fperspl = fperpl;
}
public void setSpersg(String spersg){
    this.sperssg = spersg;
}
public void setSperspl(String sperpl){
    this.sperspl = sperpl;
}
public void setTpersg(String tpersg){
    this.tperssg = tpersg;
}
public void setTperpl(String tperpl){
    this.tperspl = tperpl;
}

//getters

public String getVerbname(){
    return this.verbname;
}
public String getFpersg(){
    return this.fpersg;
}
public String getFperpl(){
    return this.fperspl;
}
public String getSpersg(){
    return this.sperssg;
}
public String getSperpl(){
    return this.sperspl;
}
public String getTpersg(){
    return this.tperssg;
}
public String getTperspl(){
    return this.tperspl;
}

}

//适配器

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;
}

}

------和两个名为activity_verbs的XML文件,其中声明了列表,并且row_verbs声明了每个项目的布局-----

row_verbs.xml ------

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/nameVerb"
        android:text="@string/nameVerb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#7663d5"
        android:textColor="#f8f8f8"
        android:padding="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="0dp"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/firstpersonsg"
        android:layout_width="182dp"
        android:layout_height="wrap_content"
        android:text="@string/firstsg"
        android:layout_below="@+id/nameVerb"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="2dp"
        android:padding="5dp"
        android:background="#cacaca"
        android:textColor="#7663d5" />
    <TextView
        android:id="@+id/firstpersonpl"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:text="@string/firstpl"
        android:padding="5dp"
        android:layout_marginTop="2dp"
        android:layout_marginLeft="2dp"
        android:background="#FFCACACA"
        android:textColor="#7663d5"
        android:layout_below="@+id/nameVerb"
        android:layout_toRightOf="@+id/firstpersonsg"/>
    <TextView
        android:id="@+id/secondpersonsg"
        android:layout_width="182dp"
        android:layout_height="wrap_content"
        android:text="@string/secondsg"
        android:layout_below="@+id/firstpersonsg"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="2dp"
        android:padding="5dp"
        android:background="#FFCACACA"
        android:textColor="#7663d5" />
    <TextView
        android:id="@+id/secondpersonpl"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:text="@string/secondpl"
        android:padding="5dp"
        android:layout_marginTop="2dp"
        android:layout_marginLeft="2dp"
        android:background="#FFCACACA"
        android:textColor="#7663d5"
        android:layout_below="@+id/firstpersonpl"
        android:layout_toRightOf="@+id/secondpersonsg"/>
    <TextView
        android:id="@+id/thirdpersonsg"
        android:layout_width="182dp"
        android:layout_height="wrap_content"
        android:text="@string/thirdsg"
        android:layout_below="@+id/secondpersonsg"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="2dp"
        android:padding="5dp"
        android:background="#FFCACACA"
        android:textColor="#7663d5" />
    <TextView
        android:id="@+id/thirdpersonpl"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:text="@string/thirdpl"
        android:padding="5dp"
        android:layout_marginTop="2dp"
        android:layout_marginLeft="2dp"
        android:background="#FFCACACA"
        android:textColor="#7663d5"
        android:layout_below="@+id/secondpersonpl"
        android:layout_toRightOf="@+id/thirdpersonsg"/>
    </RelativeLayout>

和另一个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>

1 个答案:

答案 0 :(得分:0)

我只用一个更改复制了你的代码: 我删除了

  tools:context="${relativePackage}.${activityClass}"

来自activity_verbs,它对我来说就像一个魅力:

enter image description here

我将我的测试项目与你的代码上传到DropBox,随时下载它: https://www.dropbox.com/s/8fcdab3rmx8zb57/ElaPinkSO.zip?dl=0

P.S。结构很好的问题,干得好!