.setAdapter不起作用

时间:2017-11-14 15:08:55

标签: android listview

ListView为空。 Logcat没有错误。该应用程序可以从网站上读取数据。我用“Log.i(”String“,readLine.toString())检查了它;”命令。

String gamehost = "https://example.com";

private SharedPreferences speicher;
private SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_worldhighscore);
    speicher = getApplicationContext().getSharedPreferences("Data", 0);
    editor = speicher.edit();

    String userid = speicher.getString("userid", null);


    ArrayList<String> strArray;
    strArray = new ArrayList<String>();
    ArrayAdapter<String> stradapt;
    String quellcodee = null;
    URL urle = null;

    ListView listView = (ListView) findViewById(R.id.listviewuser);
    try {
        urle = new URL(gamehost + "/highscoreread.php");
        String readLine = null;
        BufferedReader buffReader = new BufferedReader (new InputStreamReader(urle.openStream ()));
        while ((readLine = buffReader.readLine ()) != null) {
            strArray.add(readLine);
            Log.e("Data", readLine.toString());
        }
    }
    catch (MalformedURLException me) {
        me.printStackTrace();
    }
    catch (IOException ioe) {
        ioe.printStackTrace();
    }

    stradapt = new ArrayAdapter<String>(Worldhighscore.this, android.R.layout.simple_list_item_1, strArray);
    listView.setAdapter(stradapt);
    Log.e("Datas", strArray.toString());
}
  

logcat的:   I / Data:1305957942849692 - 18   I / Data:1587261998061410 - 10   I / Data:88030 - 4   I / Datas:[1305957942849692 - 18,1587261998061410 - 10,88030 - 4]

R.layout.activity_worldhighscore:

<ListView
    android:id="@+id/listviewuser"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="56dp"
    android:headerDividersEnabled="false"
    android:scrollbars="horizontal"
    android:visibility="visible"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:layout_constraintLeft_creator="1"
    tools:layout_constraintRight_creator="1" />

编辑:代码已更改。

1 个答案:

答案 0 :(得分:0)

尝试使用此

public class Adapter extends ArrayAdapter {
    List arr;
    int resource;
    Context context;
    public Adapter(Context context, int resource, List arr) {
        super(context, resource, arr);
        this.arr=arr;
        this.resource=resource;
        this.context=context;
    }
    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        View rowView;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.layout_adapter, parent, false);
        TextView param1=(TextView)rowView.findViewById(R.id.param1);
        TextView param2=(TextView)rowView.findViewById(R.id.param2);
        final ArrayList<Class.MyOBJECT> data=(ArrayList<Class.MyOBJECT>)arr;
        param1.setText(data.get(position).param1);
        param2.setText(data.get(position).param2);

        return rowView;
    }
}

...

Adapter adapter=new Adapter(this,R.layout..,arr);
ListView.setAdapter(adapter);

如果你想使用默认适配器

,也可以采用下一种方法

把这个

stradapt = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, strArray);

而不是

stradapt = new ArrayAdapter<String>(Worldhighscore.this, android.R.layout.simple_list_item_1, strArray);