定制阵列适配器

发布于 2024-12-15 09:37:25 字数 1282 浏览 0 评论 0原文

我正在编写一个新闻应用程序,但在显示自定义列表时遇到一些问题。我想要的只是列表项中有 2 个 TextView:

  • News-Title 和
  • News-Description

它们包含在 2 个静态数组中:homeScreen.title[] 和 homeScreen.descriptionLong[]。

下面是我的 HashMap 和 Adapter 代码:

final static ArrayList>数据 = 新的 ArrayList>();

static{
    HashMap<String, String> row = new HashMap<String, String>();
    for (int i = 0; i<HomeScreen.arrayLength; i++){
        row.put("Title", HomeScreen.title[i]);
        row.put("Description", HomeScreen.descriptionLong[i]);
        data.add(row);
    }
}

    SimpleAdapter adapter = new SimpleAdapter(this,
            data,
              R.layout.mainmenu,
              new String[] {"Title", "Description"},
              new int[] { R.id.textView1, R.id.textView2});
   setListAdapter(adapter);
}

    public void onItemClick(SimpleAdapter arg0, View arg1, int position,
                long id) {
            selectedNews = position;
            Toast.makeText(getApplicationContext(), "This is: " + selectedNews, Toast.LENGTH_LONG).show();
            Intent intent = new Intent(MainMenu.this, ReadNews.class);
            startActivity(intent);
        }

我遇到的问题是它只显示我的第 20 条(最后一条)新闻信息,而且默认的 OnItemClick 不再工作。我很感激你的帮助...

I'm writing a news-app and I'm having some trouble with displaying a custom list. All I want is that list items have 2 TextViews in them:

  • News-Title and
  • News-Description

These are contained in 2 static arrays: homeScreen.title[] and homeScreen.descriptionLong[].

Lower you have my code for the HashMap and the Adapter:

final static ArrayList> data = new ArrayList>();

static{
    HashMap<String, String> row = new HashMap<String, String>();
    for (int i = 0; i<HomeScreen.arrayLength; i++){
        row.put("Title", HomeScreen.title[i]);
        row.put("Description", HomeScreen.descriptionLong[i]);
        data.add(row);
    }
}

    SimpleAdapter adapter = new SimpleAdapter(this,
            data,
              R.layout.mainmenu,
              new String[] {"Title", "Description"},
              new int[] { R.id.textView1, R.id.textView2});
   setListAdapter(adapter);
}

    public void onItemClick(SimpleAdapter arg0, View arg1, int position,
                long id) {
            selectedNews = position;
            Toast.makeText(getApplicationContext(), "This is: " + selectedNews, Toast.LENGTH_LONG).show();
            Intent intent = new Intent(MainMenu.this, ReadNews.class);
            startActivity(intent);
        }

The problem I'm having is that it only displays my 20'th (last) news information and also the default OnItemClick isn't working anymore. I'd appreciate your help...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最好是你 2024-12-22 09:37:25

您应该将 row 实例化放在 for 循环中:

for (int i = 0; i<HomeScreen.arrayLength; i++){
    HashMap<String, String> row = new HashMap<String, String>();
    row.put("Title", HomeScreen.title[i]);
    row.put("Description", HomeScreen.descriptionLong[i]);
    data.add(row);
}

更新:

您的 OnItemClickListener 注册应如下所示:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, 
                              int position, long id) {
        // your code here
    }
});

You should put the row instantiation inside the for loop:

for (int i = 0; i<HomeScreen.arrayLength; i++){
    HashMap<String, String> row = new HashMap<String, String>();
    row.put("Title", HomeScreen.title[i]);
    row.put("Description", HomeScreen.descriptionLong[i]);
    data.add(row);
}

Update:

Your OnItemClickListener registration should look something like this:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, 
                              int position, long id) {
        // your code here
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文