沒有錯誤,但是當我點擊一個listview項時什麼也沒有發生。我有超過10個項目。並去差異化的活動。任何想法?我也是Eclipse的初學者也是java。如何在過濾器ListView- Item-Click上加載特定的活動(多重意圖)?
例子: 1.Niat(ListView控件-OnItemClickListener)會去NiatActivity.class 2.Lafaz(ListView控件-OnItemClickListener)會去LafazActivity.class
這裏我的代碼。提前致謝。
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class MainTwoActivity extends Activity {
// List view
private ListView lv;
// Listview Adapter
ArrayAdapter<String> adapter;
// Search EditText
EditText inputSearch;
// ArrayList for Listview
ArrayList<HashMap<String, String>> productList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_two);
// Listview Data
final String products[] = { "Niat",
"Lafaz", "Solat" };
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, products);
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
MainTwoActivity.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) {
// TODO Auto-generated method stub
}
});
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// if(position == 1)
String openClass = adapter.getItem(position);
if (openClass.equals("Niat")) {
// code specific to first list item
Intent myIntent = new Intent(view.getContext(),
NiatActivity.class);
startActivity(myIntent);
}
else if (openClass.equals("Lafaz")) {
Intent myIntent1 = new Intent(view.getContext(),
LafazActivity.class);
startActivity(myIntent1);
}
}
});
}
}
使用Java反射使用http://tutorials.jenkov.com/java-reflection/dynamic-class -loading-reloading.html – SelvaMariappan 2014-12-04 09:57:57
如果您想要在列表項上點擊不同的活動,只需使用Switch語句獲取其位置。 – Piyush 2014-12-04 10:36:05
@PiyushGupta已經切換語句,但仍然不工作 – malona 2014-12-05 05:52:09