【问题标题】:Populating a listview with a tweet array using an array adapter使用数组适配器使用推文数组填充列表视图
【发布时间】:2015-08-21 00:37:14
【问题描述】:

我正在尝试使用我的 tweets 数组填充我的列表视图,但我要么抛出错误,要么应用程序在启动时崩溃。任何人都可以帮助我确保我有正确的语法或让我知道我是否有什么不合适的地方吗?错误在我的TweetAdapter 类中,并且在代码中进行了注释。错误状态Cannot resolve method 'super(android.app.Activity, int, java.lang.String[])'

这是来自我的 TweetAdapter 类的代码:

import android.content.Context;
import android.widget.ArrayAdapter;
import s607central.pettastic.R;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


public class TweetAdapter extends ArrayAdapter<Tweet> {
    private LayoutInflater inflater;
    private Context context;
    private List<Tweet> tweetList;

    public TweetAdapter(Activity activity, String[] items){  

        super(activity, R.layout.row_tweet, items); // **THIS IS THE ERROR**
        inflater = activity.getWindow().getLayoutInflater();
    }

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

        if (convertView == null) {

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.activity_tweet_detail, parent, false);
        }

        TextView tt = (TextView) convertView.findViewById(R.id.tweetTitle);
        TextView tb = (TextView) convertView.findViewById(R.id.tweetBody);
        Tweet t = tweetList.get(position);


        return convertView;

如果相关,这是我的 TweetListActivity 类中的代码,这里没有错误:

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class TweetListActivity extends ListActivity {
    private ArrayAdapter tweetItemArrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tweet_list);

        List<Tweet> tweets = new ArrayList<Tweet>();



        for (int i = 0; i < 20; i++) {
            Tweet tweet = new Tweet();
            tweet.setTitle("A nice header for Tweet # " + i);
            tweet.setBody("Some random body text for the tweet # " + i);
            tweets = new ArrayList<Tweet>();
            tweets.add(tweet);
        }


        tweetItemArrayAdapter = new TweetAdapter(this, new String[10]);
        setListAdapter(tweetItemArrayAdapter);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Intent intent = new Intent(this, TweetDetailActivity.class);
        startActivity(intent);
    }

}

谢谢,我们将不胜感激。

【问题讨论】:

    标签: java android arrays listview twitter


    【解决方案1】:

    错误是告诉您您尝试调用的方法不存在。

    我假设您正在尝试使用 ArrayAdapter(Context, int, T[]) 构造函数。

    它不起作用的原因是因为您正在使用Tweet 参数化您的类,所以您需要传递Tweet[],而不是String[]。要么这样,要么将您的类更改为使用extends ArrayAdapter&lt;String&gt;

    【讨论】:

    • 我已经尝试了这两种方法,使用 String 消除了错误,但应用程序在启动时仍然崩溃。当我使用 Tweet[] 时只会引发更多错误。
    • 进行编译是第一步,也是您在这个问题中定义的问题。编译错误与运行时错误非常不同:一旦编译,您就可以运行它并查看它在哪里/为什么会崩溃。
    猜你喜欢
    • 1970-01-01
    • 2011-01-24
    • 2021-04-29
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多