setAdapter不起作用

setAdapter不起作用

本文介绍了.setAdapter不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The ListView is empty. The Logcat haven't an error. The application can read the data from the website. I checked it with the "Log.i("String", readLine.toString());" command.

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

R.layout.activity_worldhighscore:

The 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" />

代码已更改.

推荐答案

尝试使用此功能

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

以及要使用默认适配器的下一种方法

and also next approach if you want to use default 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);

这篇关于.setAdapter不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 21:01