我有一个包含String数组的ListAdapter。
当我点击列表中的任何项目时,我希望应用程序将当前单击的项目传递给另一个活动并转到另一个活动。
我正在尝试设置onItemClickListener但没有成功。
活动:
package com.nituach.nituach;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
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.Button;
import android.widget.ListView;
public class NituachActivity extends Activity implements OnClickListener,
OnItemClickListener {
Button addNewBuisness;
ListView blv;
public static int bname;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String TAG = NituachActivity.class.getSimpleName();
addNewBuisness = (Button) findViewById(R.id.btnAddNewBuisness);
addNewBuisness.setOnClickListener(NituachActivity.this);
blv = (ListView) findViewById(R.id.listView1);
Log.d(TAG, "All Variables was created");
DataBase dbc = new DataBase(NituachActivity.this);
dbc.open();
List<String> cs = dbc.getData();
blv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, cs));
dbc.close();
blv.setOnItemClickListener(NituachActivity.this);
}
public void onClick(View v) {
if (v == addNewBuisness) {
Intent addnbIntent = new Intent(NituachActivity.this,
AddNewBuisness.class);
NituachActivity.this.startActivity(addnbIntent);
}
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
}
}
答案 0 :(得分:2)
您的onItemClick()
- 方法为空。
ListView使用OnItemClickListener
,它导出onItemClick()
- 方法。 You Activity实现了“普通”OnClickListener
(公开onClick()
- 方法)和OnItemClickListener。您不需要OnClickListener
,只需要前一个。
在现场添加匿名实现通常更具可读性:
blv.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// Your Code
}
});
答案 1 :(得分:0)
在当前活动中声明一个静态变量,并在列表上单击任何项目时将其值设置为单击的项目。
public static String abc;
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
abc = cs[arg2];
Intent intent = new Intent(context, AddNewBusiness.class);
startActivity(intent);
}
答案 2 :(得分:0)
试试这个,可能对你有所帮助......
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//here arg2 is the position of that item in the listview, which u can use for indexing the string array, assumes `cs` is ur string array
Intent addnbIntent = new Intent(NituachActivity.this,
AddNewBuisness.class);
addnbIntent.putExtra("value",cs[arg2])
NituachActivity.this.startActivity(addnbIntent);
}