2017-08-06 132 views
0

我有下面的代码应该:列表视图OnItemClick事件

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方面的经验也不多。

+0

的onCreate是罚款。 listview中单元格的视图是什么?你在listview单元布局中使用按钮吗? –

+0

它假设工作是你确定ID是正确的?尝试打印一些东西insede点击 –

+0

@farisfaris是的编号是正确的 –

回答

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

感谢问题是,我有clickable = true –

+0

最后一个问题:我怎样才能从文本github_url在选定的list_item中? –

+0

请检查项目点击代表列表视图。我已经更新了答案。 –

0

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

1

如果表示OnItemClickListeneter无法正常工作,那么您需要通过扩展ArrayAdater来实现自定义适配器来为您提供自定义行,并且在自定义适配器中,您可以使用回调接口或在它自己的视图上实现侦听器example

相关问题