首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Listview OnItemClick事件

Listview OnItemClick事件
EN

Stack Overflow用户
提问于 2017-08-06 18:47:17
回答 3查看 77关注 0票数 0

我有以下代码,应该是:

代码语言:javascript
复制
listView = (ListView) findViewById(R.id.listview_github_entries);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

         }
    });

这是我加载到ListView中的内容:

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_item"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:orientation="horizontal"
    android:clickable="true">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/github_icon"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/github_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/github_url"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>


</LinearLayout>

这是适配器:

代码语言:javascript
复制
public class GithubEntryAdapter extends ArrayAdapter<GithubEntry>{

    public GithubEntryAdapter(Activity context, ArrayList<GithubEntry> githubEntries){
        super(context, 0, githubEntries);
    }

    public View getView(int position, View convertView, ViewGroup parent){
        View listItemView = convertView;
        if (listItemView == null){
            listItemView = LayoutInflater.from(getContext()).inflate(
                    R.layout.list_item, parent, false);
        }

        GithubEntry currentGithubEntry = getItem(position);
        TextView github_url = (TextView) listItemView.findViewById(R.id.github_url);
        github_url.setText(currentGithubEntry.getGithub_url());
        TextView github_name = (TextView) listItemView.findViewById(R.id.github_name);
        github_name.setText(currentGithubEntry.getGithub_name());
        return listItemView;

    }
}

这对我不起作用。我不确定我应该把这段代码放在哪里。我能把这个放到onCreate里吗?如果不是,我应该把它移到哪里?我在Android方面完全是新手,我在Java方面也没有太多经验。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-08-06 19:50:20

这就是我为你所做的。

代码语言:javascript
复制
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.ListView;
 import android.widget.Toast;

 import java.util.ArrayList;

 public class MainActivity extends AppCompatActivity {

private ListView listView;

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

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

    listView.setAdapter(new GithubEntryAdapter(MainActivity.this, getList()));


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            TextView github_url_tv = view.findViewById(R.id.github_url); 
            String url_text= github_url_tv.getText().toString();

            Toast.makeText(MainActivity.this, url_text", Toast.LENGTH_LONG).show();
        }
    });
}


private ArrayList<GithubEntry> getList() {

    ArrayList<GithubEntry> githubEntries = new ArrayList<>();

    GithubEntry githubEntry = new GithubEntry();
    githubEntry.setGithub_name("Name");
    githubEntry.setGithub_url("url");


    GithubEntry githubEntry1 = new GithubEntry();
    githubEntry1.setGithub_name("Name");
    githubEntry1.setGithub_url("url");

    GithubEntry githubEntry2 = new GithubEntry();
    githubEntry2.setGithub_name("Name");
    githubEntry2.setGithub_url("url");

    githubEntries.add(githubEntry);
    githubEntries.add(githubEntry1);
    githubEntries.add(githubEntry2);

    return githubEntries;

}
}

这是适配器

代码语言:javascript
复制
   import android.app.Activity;
   import android.view.LayoutInflater;
   import android.view.View;
   import android.view.ViewGroup;
   import android.widget.ArrayAdapter;
   import android.widget.TextView;

   import java.util.ArrayList;

    public class GithubEntryAdapter extends ArrayAdapter<GithubEntry> {

    public GithubEntryAdapter(Activity context, ArrayList<GithubEntry> 
    githubEntries){
    super(context, 0, githubEntries);
}

public View getView(int position, View convertView, ViewGroup parent){
    View listItemView = convertView;
    if (listItemView == null){
        listItemView = LayoutInflater.from(getContext()).inflate(
                R.layout.list_item, parent, false);
    }

    GithubEntry currentGithubEntry = getItem(position);
    TextView github_url = (TextView) listItemView.findViewById(R.id.github_url);
    github_url.setText(currentGithubEntry.getGithub_url());
    TextView github_name = (TextView) listItemView.findViewById(R.id.github_name);
    github_name.setText(currentGithubEntry.getGithub_name());
    return listItemView;

}
}

下面是POJO(plan java object)类

代码语言:javascript
复制
class GithubEntry {

private String Github_url;
private String Github_name;


public String getGithub_url() {
    return Github_url;
}

public void setGithub_url(String github_url) {
    Github_url = github_url;
}

public String getGithub_name() {
    return Github_name;
}

public void setGithub_name(String github_name) {
    Github_name = github_name;
}
}

这是list_item

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="horizontal"
android:clickable="false">

<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@mipmap/ic_launcher_round"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/github_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/github_url"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>


</LinearLayout>

这是活动布局

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.kaimeramedia.githubentry.MainActivity">

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

</LinearLayout>
票数 1
EN

Stack Overflow用户

发布于 2017-08-06 19:06:28

如果意味着OnItemClickListeneter不工作,那么您需要通过扩展ArrayAdater来实现一个自定义适配器来为您自定义行提供服务,而且在自定义适配器中,您可以使用一个回调接口,或者在它自己看到example视图上实现一个侦听器。

票数 1
EN

Stack Overflow用户

发布于 2017-08-06 18:53:59

我认为您想要在这里显示值列表。您应该在onCreate中编写它,因为onCreate是启动和运行方法的方法。因此,将其放入onCreate中。如需更多正确答案,请解释一切。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45531207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档