Listview OnItemClick事件

时间:2017-08-06 10:47:17

标签: java android listview events adapter

我有以下代码应该:

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中的内容:

<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>

这是适配器:

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经验。

3 个答案:

答案 0 :(得分:1)

如果意味着OnItemClickListeneter不起作用,则需要通过扩展ArrayAdater来为自定义行提供自定义适配器,并且在自定义适配器中,您可以使用回调接口或在视图上实现侦听器它自我参见example

答案 1 :(得分:1)

这就是我为你所做的......

 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;

}
}

这是适配器

   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(计划java对象)类

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

<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>

这是活动布局

<?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>

答案 2 :(得分:0)

我想你想在这里显示值列表。您应该在onCreate中编写它,因为onCreate是一个方法启动和运行的方法。所以,把它放在onCreate上。如需更正确的答案,请解释一切。