【问题标题】:I want to make a list view with two columns in android.why is it not working我想在android中创建一个包含两列的列表视图。为什么它不起作用
【发布时间】:2017-08-07 13:13:33
【问题描述】:

这是我的主要活动

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

                                                      }
                                                  }
    );


}

}

自定义行

<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 时,第一个窗口(主要活动)运行没有错误,没有出现带有列表视图的窗口。此错误出现在 RUN 并关闭应用程序

E/AndroidRuntime: 致命异常: main 进程:com.example.amila.newglossary,PID:6309 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.amila.newglossary/com.example.amila.newglossary.pst}: java.lang.RuntimeException: 你的内容必须有一个 ID 的 ListView 属性是'android.R.id.list' 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 在 android.app.ActivityThread.-wrap12(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 在 android.os.Handler.dispatchMessage(Handler.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) 原因:java.lang.RuntimeException:您的内容必须有一个 id 属性为 'android.R.id.list' 的 ListView 在 android.app.ListActivity.onContentChanged(ListActivity.java:243) 在 com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:419) 在 android.app.Activity.setContentView(Activity.java:2414) 在 com.example.amila.newglossary.pst.onCreate(pst.java:42) 在 android.app.Activity.performCreate(Activity.java:6664) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 在 android.app.ActivityThread.-wrap12(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 在 android.os.Handler.dispatchMessage(Handler.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)

【问题讨论】:

    标签: java android arrays listview multiple-columns


    【解决方案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;
    }
    

    【讨论】:

    • 它对我有用。你能建议一种将外部字体(另一种语言)应用到第二列的方法吗?我正在尝试一个词汇表应用程序。这样一列应该是另一种语言。
    猜你喜欢
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    • 2020-01-18
    相关资源
    最近更新 更多