-1

アプリケーションでリスト ビューを使用したいのですが、コーディングは正しく行いましたが、そのアプリケーションを実行すると、それに近づきます。これが私のコードです:

setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1,R.id.textView1,getResources().getStringArray(R.array.cities)));

}

アダプタ

private class MyAdapter extends ArrayAdapter<String>{

    public MyAdapter(Context context, int resource, int textViewResourceId,
            String[] string) {
        super(context, resource, textViewResourceId, string);
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater in=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row=in.inflate(R.layout.list_item, parent, false);
        String[] items=getResources().getStringArray(R.array.cities);
        TextView tv=(TextView) row.findViewById(R.id.textView1);

        return super.getView(position, convertView, parent);



    }

}
}
4

2 に答える 2

0

次のようなことを試すことができます:

public class MySimpleArrayAdapter extends ArrayAdapter<String> {
  private final Context context;
  private final String[] values;

public MySimpleArrayAdapter(Context context, String[] values) {
   super(context, R.layout.rowlayout, values);
   this.context = context;
   this.values = values;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
   LayoutInflater inflater = (LayoutInflater) context
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
   TextView textView = (TextView) rowView.findViewById(R.id.label);       
   textView.setText(values[position]);   

   return rowView;
 }
} 
于 2013-09-30T11:36:38.557 に答える
0

以下に変更

LayoutInflater in;
String[] items;
 public MyAdapter(Context context, int resource, int textViewResourceId,
            String[] string) {
        super(context, resource, textViewResourceId, string);
  in=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  items=context.getResources().getStringArray(R.array.cities);

    }

getResources()アクティビティ コンテキストが必要です。

于 2013-09-30T11:29:15.090 に答える