2017-08-07 82 views
0

这是我的主要活动我希望做一个列表视图与android.why两列它不工作

public class MainActivity extends AppCompatActivity { 


//private static Button buttonPst; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.content_main); 
    findViewById(R.id.button4).setOnClickListener(new View.OnClickListener() { 
                 public void onClick(View v) { 
                  Intent I = new Intent(); 
                  I.setAction("android.amila.action.PST"); 
                  startActivity(I); 

                 } 
                } 
    ); 


} 

}

Custom_row

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:orientation="horizontal" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<TextView 
    android:text="@string/textview" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/rowTitle" 
    android:layout_weight="1" 
    android:textSize="18sp" 
    android:typeface="normal" /> 

<TextView 
    android:text="@string/textview1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/rowSubtitle" 
    android:layout_weight="1" 
    android:textSize="18sp" /> 

这是xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/content_pst" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 


tools:context="com.example.amila.newglossary.pst" 
> 

<ListView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 
    android:layout_marginRight="10dp" 
    android:layout_marginEnd="10dp" 
    android:id="@+id/pstList" /> 

java文件

public class pst extends Activity { 

String[] englishName = new String[]{"bacon", "Ham", "Tuna", "Candy", "Meatball", "Potato"}; 
String[] sinhalaName = new String[]{"wdddbhha", "wdddbhha", "wdddbhha", "wdddbhha", "wa", "wa"}; 
// Button button; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.content_pst); 
    ListView listView = (ListView) findViewById(R.id.pstList); 
    adapter ada= new adapter(this,englishName,sinhalaName); 



    listView.setAdapter(ada); 
} 

}

这是适配器类

public class adapter extends ArrayAdapter<String> { 
String[] englishName; 
String[] sinhalaName; 
Context c; 
LayoutInflater inflater; 
public adapter(Context context, String[] englishName,String[] sinhalaName) { 
    super(context, R.layout.custom_row,englishName); 
this.c=context; 
    this.englishName=englishName; 
    this.sinhalaName=sinhalaName; 
} 
public class ViewHolder{ 
TextView englishTv; 
TextView sinhalaTv; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    if (convertView == null) { 
     inflater = (LayoutInflater) 
c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.custom_row, null); 
    } 
final ViewHolder holder=new ViewHolder(); 
    holder.englishTv=(TextView)convertView.findViewById(R.id.rowTitle); 
    holder.sinhalaTv=(TextView)convertView.findViewById(R.id.rowSubtitle); 

    holder.englishTv.setText(englishName[position]); 
    holder.sinhalaTv.setText(sinhalaName[position]); 
    return super.getView(position, convertView, parent); 
} 
} 

我曾经尝试这样做非常多,第一个窗口(主活动)正在运行按下PST时没有错误,带有列表视图的窗口不会出现。出现在运行该错误,并关闭该应用

E/AndroidRuntime:致命异常:主 工艺:com.example.amila.newglossary,PID:6309 了java.lang.RuntimeException:无法启动活动ComponentInfo { com.example.amila.newglossary/com.example.amila.newglossary.pst}: java.lang.RuntimeException:您的内容必须在android中具有一个ListView,其ID为 属性为'android.R.id.list' 。 app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(Acti voyThread.java) at android.app.ActivityThread $ h.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper。 java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 引起:java.lang.RuntimeException:您的内容必须具有List属性为android的ListView。 R.id.list' at android.app.ListActivity.onContentChanged(ListActivity.java:243) at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:419) at android.app.Activity.setContentView(Activity.java:2414) at com.example.amila.newglossary.pst.onCreate( (Activity.read)中的android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 。 java:2599) at android.app.ActivityThread $(android.app.ActivityThread.EventThread.java:2707) android.app.ActivityThread.-wrap12(ActivityThread.java) $ H.handleMessage(ActivityThread.java :1460) at android.os.Handler.d ispatchMessage(处理程序。的java:102) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6077) 在java.lang.reflect.Method.invoke(本地方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

回答

1

在您的适配器回报你,而不是定制返回超级

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder = new ViewHolder(); 
    if (convertView == null) { 
     inflater = (LayoutInflater) 
        c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.custom_row, null); 
     holder.englishTv=(TextView)convertView.findViewById(R.id.rowTitle); 
     holder.sinhalaTv=(TextView)convertView.findViewById(R.id.rowSubtitle); 
     convertView.setTag(holder); 
    }else{ 
     holder = (ViewHolder) convertView.getTag(); 
    } 
    holder.englishTv.setText(englishName[position]); 
    holder.sinhalaTv.setText(sinhalaName[position]); 
    return convertView; 
} 
+0

它为me.can的视图您建议的方式来应用前ternal字体(另一种语言)到第二列?我正在尝试一个词汇表应用程序,所以一列应该是另一种语言。 – amilab

相关问题