2017-08-06 112 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

相關問題