2014-12-03 116 views
0

我想,如果我點擊'Leerlingenraad'的列表視圖,他打開Leerlingenraad.xml類的Leerlingenraad類,如果我點擊聯繫人,他打開類與contact.xml聯繫我已經找到這很多話題,但它不會工作。有人知道我的腳本中出了什麼問題嗎?請不要評價。Android listview onclicklistener活動

Myclass.java:

package com.example.myapp; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem.*; 
import android.view.MenuItem; 
import android.view.MenuInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.*; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.HashMap; 
import android.text.Editable; 
import android.widget.AdapterView.OnItemClickListener; 
import android.text.TextWatcher; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.view.ViewGroup; 
import android.widget.Toast; 

public class Myclass extends Activity { 

// List view 
private ListView lv; 

// Listview Adapter 
ArrayAdapter<String> adapter; 

// Search EditText 
EditText inputSearch; 


// ArrayList for Listview 
ArrayList<HashMap<String, String>> onderneming; 


/** 
* Called when the activity is first created. 
*/ 

@Override 
public void onCreate(Bundle savedInstanceState) { 

super.onCreate(savedInstanceState); 
setContentView(R.layout.myclass); 


String onderneming[] = {"Leerlingenraad", "Medezeggenschapsraad", "3", "4", "5", "6", "Contact"}; 

lv = (ListView) findViewById(R.id.list_view); 
inputSearch = (EditText) findViewById(R.id.inputSearch); 

// Adding items to listview 
adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, onderneming); 
lv.setAdapter(adapter);  

/** 
* Enabling Search Filter 
* */ 
inputSearch.addTextChangedListener(new TextWatcher() { 

@Override 
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { 

// When user changed the Text 
Ondernemerschap.this.adapter.getFilter().filter(cs); 

} 

@Override 
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
int arg3) { 
// TODO Auto-generated method stub 

} 

@Override 
public void afterTextChanged(Editable arg0) { 
} 
}); 

lv.setOnItemClickListener(new OnItemClickListener() { 

@Override 
public void onItemClick(AdapterView<?> parent, View view, 

final int position, long id) { 

String main = lv.getSelectedItem().toString(); 

} 
}); 
} 
private ListView getListView() { 
return null; 

} 
} 

myclass.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 

android:id="@+id/background" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/pallasrood" 
android:orientation="vertical" > 

<ImageView 

android:id="@+id/imageView1" 
android:layout_width="wrap_content" 
android:layout_height="100dp" 
android:layout_alignParentLeft="true" 
android:layout_alignParentTop="true" 
android:src="@drawable/android" /> 

<EditText android:id="@+id/inputSearch" 

android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_below="@+id/imageView1"android:hint="Zoek de onderneming.." 
android:inputType="textVisiblePassword"/> 

<ListView 

android:id="@+id/list_view" 
android:layout_width="fill_parent" 
android:layout_height="match_parent" 
android:layout_alignParentBottom="true" 
android:layout_below="@+id/inputSearch" 
android:background="@android:color/transparent" 
android:cacheColorHint="@android:color/transparent" > 

</ListView> 

</RelativeLayout> 

AndroidManifest.xml中

<activity android:name=".Leerlingenraad"  
android:label="Pallasapp"> 
</activity> 
+1

聽起來你需要使用活動。爲'Leerlingenraad'和'Contact'創建一個新的活動,並使用'Intent's和'startActivity()'來改變這些活動。 – RogueBaneling 2014-12-03 16:08:40

+0

你應該看看[這個](http://developer.android.com/training/index.html),這會幫助你很多。特別是[開始另一個活動](http://developer.android.com/training/basics/firstapp/starting-activity.html)一章。 – James 2014-12-03 16:14:03

+0

你可以粘貼你的列表視圖屏幕截圖,讓我們明白你在問什麼 – Psypher 2014-12-03 16:22:16

回答

0

我假設你已經有了詳細的活動,我把它叫做OndernemingDetailActivity

在你的ListView活動:

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

      Intent in = new Intent(this, OndernemingDetailActivity.class); 
      //Send onderneming 
      in.putExtra("selected_onderneming", adapter.getItem(i)); 
      startActivity(in); 
     } 
    }); 

然後在您的詳細信息視圖,你會得到「selected_onderneming」從包像這樣:

String selectedOnderneming; 
Bundle extras = intent.getExtras(); 
    if(extras != null) 
     onderneming = extras.getString("selected_onderneming"); // retrieve the selected onderneming 

這是爲了讓演員出最快捷的方式捆綁。檢查出this發佈一個更健壯和安全的方式來這樣做。

然後,您可以顯示您選擇的'選擇開始'的數據。我希望這回答了你的問題。

+0

是的請解釋 – Anoniem 2014-12-03 18:22:11

+0

我會在15分鐘內編輯我的答案,我很忙atm;) – James 2014-12-03 18:23:24

+0

好吧我會看到它 – Anoniem 2014-12-03 18:24:22

相關問題