大家好。我的目标是当我想点击ListView中的特定项目时,它会打开我编程的新活动。例如:当我点击“João”时,我希望它打开一个名为Joao的课程。我尝试过使用简单的代码,但它不起作用。当我从ListView点击João的名字时没有任何反应。
这是我正在使用的代码:
public class Searchsort extends Activity {
private ListView lv1;
private EditText ed;
private String lv_arr[]={"John","Mary","Carl","Rose","Charlie","Allan", "João"};
private ArrayList<String> arr_sort= new ArrayList<String>();
int textlength=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
lv1 = (ListView)findViewById(R.id.ListView01);
ed = (EditText)findViewById(R.id.EditText01);
// By using setAdpater method in listview we an add string array in list.
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
ed.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
textlength=ed.getText().length();
arr_sort.clear();
for(int i=0;i<lv_arr.length;i++) {
if(textlength<=lv_arr[i].length()) {
if(ed.getText().toString().equalsIgnoreCase((String) lv_arr[i].subSequence(0, textlength))) {
arr_sort.add(lv_arr[i]);
}
}
}
lv1.setAdapter(new ArrayAdapter<String> (Searchsort.this,android.R.layout.simple_list_item_1 , arr_sort));
}
});
}
public void onListItemClick(ListView parent, View v, int position, long id) {
if ("João".equals(lv_arr[position])) {
Intent myIntent = new Intent(Searchsort.this, Joao.class);
startActivity(myIntent);
}
}
}
答案 0 :(得分:0)
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Searchsort.this, Joao.class);
startActivity(intent);
}
}
});
答案 1 :(得分:0)
setOnItemClickListener
使用ListView
并处理那里的活动。
答案 2 :(得分:0)
请检查您的Androidmanifest.xml
。
添加声明如:
<activity android:name=".CLASS_NAME"> </activity>
您必须声明所有活动。
答案 3 :(得分:0)
正如Niko Suggested以下列方式使用setOnItemClickListener()。
public class Searchsort extends Activity {
private ListView lv1;
private EditText ed;
private String lv_arr[]={"John","Mary","Carl","Rose","Charlie","Allan", "João"};
private ArrayList<String> arr_sort= new ArrayList<String>();
int textlength=0;
private String getItem;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
lv1 = (ListView)findViewById(R.id.listView1);
ed = (EditText)findViewById(R.id.EditText01);
// By using setAdpater method in listview we an add string array in list.
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
// Here I have set Listener for listview
lv1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
// Here I am getting the selected item of listview.
getItem = (arg0.getItemAtPosition(arg2).toString());
if(getItem.equals("John"))
{
Intent i = new Intent(Searchsort.this,John.class);
startActivity(i);
}
}
});
ed.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
textlength=ed.getText().length();
arr_sort.clear();
for(int i=0;i<lv_arr.length;i++) {
if(textlength<=lv_arr[i].length()) {
if(ed.getText().toString().equalsIgnoreCase((String) lv_arr[i].subSequence(0, textlength))) {
arr_sort.add(lv_arr[i]);
}
}
}
lv1.setAdapter(new ArrayAdapter<String> (Searchsort.this,android.R.layout.simple_list_item_1 , arr_sort));
}
});
}
}
答案 4 :(得分:0)
试试这个..
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position == 1)
{
Intent myIntent = new Intent(YourActivity.this, SecondActivity.class);
startActivityForResult(myIntent, 0);
}
if(position == 2)
{
Intent myIntent = new Intent(YourActivity.this, ThirdActivity.class);
startActivityForResult(myIntent, 0);
}
}
});
希望这会对你有所帮助: - )