setListAdapter()错误

时间:2018-07-24 15:12:35

标签: android

当我尝试使用setListAdapter显示数据时,会发生这种情况:

  

无法解析方法setListAdapter

这是我的活动

+---+---+
| a | b |
+---+---+
| 1 | 2 |
| 2 | 3 |
| 1 | 4 |
| 2 | 2 | <- Will never happen
| 3 | 4 |
| 4 | 3 | <- Will never happen, same as previous row
+---+---+

这是我的适配器:

package id.sch.smktelkom.www.crud;

import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.List;

public class CollectionActivity extends AppCompatActivity implements 
    FetchCollectListener {
    String[] daftar;
    ListView listView;
    protected Cursor cursor;
    Button search;
    private ProgressDialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collection);
        Button btn=(Button)findViewById(R.id.button2);
        search = (Button) findViewById(R.id.cari);

        initView();


        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent inte = new Intent(CollectionActivity.this, MainActivity.class);
                startActivity(inte);
            }
        });
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent c = new Intent(CollectionActivity.this, SearchV.class);
                startActivity(c);
            }
        });
    }

    private void initView() {
        // show progress dialog
        dialog = ProgressDialog.show(CollectionActivity.this, "", "Loading...");

        String url = "http://192.168.4.5/SqliteSync/readC.php";
        FetchCollection task = new FetchCollection(this);
        task.execute(url);
    }


    @Override
    public void onFetchComplete(List<Collect> data) {
        // dismiss the progress dialog
        if(dialog != null)  dialog.dismiss();
        // create new adapter
        CollectAdapter adapter = new CollectAdapter(this, data);
        // set the adapter to list
        setListAdapter(adapter);
    }

    @Override
    public void onFetchFailure(String msg) {
        // dismiss the progress dialog
        if(dialog != null)  dialog.dismiss();
        // show failure message
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }
}

我不知道这是怎么了,这已经让我很烦。

我想从MySQL检索数据到我的Android应用程序,当我键入setListAdapter时突然变成红色并显示一些错误。

感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

代替setListAdapter(adapter);调用listView.setAdapter(adapter);

setListAdapter用于扩展ListActivity

的活动

我还看到您应该添加onCreate:

ListView listView=(ListView)findViewById(R.id.listView);

如果xml中ListView的名称是listView

答案 1 :(得分:0)

因此,在@AmitVikramSingh提出了一些建议之后

我引用的是列表视图

public class CollectionActivity extends AppCompatActivity implements FetchCollectListener {
String[] daftar;
ListView listView;

protected Cursor cursor;


Button search;

private ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_collection);
    Button btn=(Button)findViewById(R.id.button2);
    search = (Button) findViewById(R.id.cari);
    listView = findViewById(R.id.list);

我将其更改为

    @Override
public void onFetchComplete(List<Collect> data) {
    // dismiss the progress dialog
    if(dialog != null)  dialog.dismiss();
    // create new adapter
    CollectAdapter adapter = new CollectAdapter(this, data);
    // set the adapter to list
    setListAdapter(adapter);
}

对此

    @Override
public void onFetchComplete(List<Collect> data) {
    // dismiss the progress dialog
    if(dialog != null)  dialog.dismiss();
    // create new adapter
    CollectAdapter adapter = new CollectAdapter(this, data);
    // set the adapter to list
    listView.setAdapter(adapter);
}

非常感谢所有帮助我解决此问题的人