MainActivity扩展AppCompatActivity时如何使用setListAdapter?

时间:2016-05-31 15:15:32

标签: android

我是Android的新手,并遵循2014 Lynda教程使用适配器制作ListView。这是MainActivity:

<table border="1" style="width:100%;">
  <tbody>
    <tr>
      <th>Number</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Points</th>
    </tr>
    <tr>
      <td>1</td>
      <td>Eve</td>
      <td>Jackson</td>
      <td>94</td>
    </tr>
    <tr>
      <td>2</td>
      <td>John</td>
      <td>Doe</td>
      <td>80</td>
    </tr>
    <tr>
      <td>3</td>
      <td>Adam</td>
      <td>Johnson</td>
      <td>67</td>
    </tr>
    <tr>
      <td>4</td>
      <td>Jill</td>
      <td>Smith</td>
      <td>50</td>
    </tr>
  </tbody>
</table>

public class MainActivity extends AppCompatActivity { TextView output; ProgressBar pb; List<MyTask> tasks; List<Flower> flowerList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); // Initialize the TextView for vertical scrolling output = (TextView) findViewById(R.id.textView); output.setMovementMethod(new ScrollingMovementMethod()); pb = (ProgressBar) findViewById(R.id.progressBar); pb.setVisibility(View.INVISIBLE); tasks = new ArrayList<>(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.action_do_task) { if (isOnline()) { requestData("http://services.hanselandpetal.com/secure/flowers.json"); } else { Toast.makeText(this, "Network isn't available", Toast.LENGTH_LONG).show(); } return false; } return false; } private void requestData(String uri) { MyTask task = new MyTask(); task.execute(uri); } protected void updateDisplay() { FlowerAdapter adapter = new FlowerAdapter(this, R.layout.item_flower, flowerList); setListAdapter(adapter); //<---Here is the error } 无法解决方法错误。

我从another answer了解到,如果MainActivity扩展setListAdapter(adapter) I get而不是ListActivity,则会解决此错误。但后来我得到了许多其他我无法解决的错误。非常感谢您的解决方案。

如果您需要,这里是AppCompatActivity

FlowerAdapter

这是我在content_main.xml

中创建的ListView
public class FlowerAdapter extends ArrayAdapter<Flower> {

    private Context context;
    private List<Flower> flowerList;

    public FlowerAdapter(Context context, int resource, List<Flower> objects) {
        super(context, resource, objects);
        this.context = context;
        this.flowerList = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater =
                (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.item_flower, parent, false);

        //Display flower name in the TextView widget
        Flower flower = flowerList.get(position);
        TextView tv = (TextView) view.findViewById(R.id.textView1);
        tv.setText(flower.getName());

        return view;
    }

}

activity_main.xml中

<ListView
    android:id="@+id/listView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"
    android:text="" />

2 个答案:

答案 0 :(得分:1)

您必须在activity_main.xml中创建ListView:

<ListView
  android:id="@+id/list"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" >
</ListView> 

并使用它:

ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);

尝试教程:http://androidexample.com/Create_A_Simple_Listview_-_Android_Example/index.php?view=article_discription&aid=65&aaid=90

或者:http://www.tutorialspoint.com/android/android_list_view.htm

答案 1 :(得分:1)

您的ListView位于content_main布局内,而不是activity_main的一部分。

content_main布局的ID设为:

<include 
    android:id="+id/id_content_main"
    layout="@layout/content_main" />

然后像这样访问

View contentView = findViewById(R.id.id_content_main);
listView = (ListView) contentView.findViewById(R.id.listView);
listView.setAdapter(adapter);
相关问题