以下是main.xml中的文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/lv_items"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>下面是studetail.xml中的内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:numeric="integer"
></TextView>
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
></TextView>
<TextView
android:id="@+id/score"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:numeric="integer"
></TextView>
</LinearLayout>下面是MainActivity.java 中的部分内容:
ListView lv_items = (ListView) findViewById(R.id.lv_items);
ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map; map = new HashMap<String, Object>();
map.put("id", 1111);
map.put("name", "james0");
map.put("score", 156);
data.add(map);
map = new HashMap<String, Object>();
map.put("id", 2222);
map.put("name", "james1");
map.put("score", 156);
data.add(map);
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.studetail, new String[]
{"id","name","score"}, new int[]{R.id.id,R.id.name,R.id.score});
lv_items.setAdapter(adapter);
Toast.makeText(MainActivity.this, "Before Hello", Toast.LENGTH_SHORT).show();
lv_items.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(MainActivity.this, "Hello", Toast.LENGTH_SHORT).show();
}
});
运行取来貌似不会执行setOnItemClickListener(){}里面的内容,这是为什么呢?
我sdk是用的2.2,LongClick的listener是有用的。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/lv_items"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>下面是studetail.xml中的内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:numeric="integer"
></TextView>
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
></TextView>
<TextView
android:id="@+id/score"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:numeric="integer"
></TextView>
</LinearLayout>下面是MainActivity.java 中的部分内容:
ListView lv_items = (ListView) findViewById(R.id.lv_items);
ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map; map = new HashMap<String, Object>();
map.put("id", 1111);
map.put("name", "james0");
map.put("score", 156);
data.add(map);
map = new HashMap<String, Object>();
map.put("id", 2222);
map.put("name", "james1");
map.put("score", 156);
data.add(map);
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.studetail, new String[]
{"id","name","score"}, new int[]{R.id.id,R.id.name,R.id.score});
lv_items.setAdapter(adapter);
Toast.makeText(MainActivity.this, "Before Hello", Toast.LENGTH_SHORT).show();
lv_items.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(MainActivity.this, "Hello", Toast.LENGTH_SHORT).show();
}
});
运行取来貌似不会执行setOnItemClickListener(){}里面的内容,这是为什么呢?
我sdk是用的2.2,LongClick的listener是有用的。
解决方案 »
- android想给TextView中的文字加入下划线但是想让下划线和文字的颜色不一样
- textView.setText(getChild(groupPosition,childPosition).toString());
- listview单击事件传值问题
- Android2.3的Launcher2源代码移到Eclipse里面去编译不能成功
- 如何获取蓝牙连后的配置文件状态
- android是如何解析Manifest文件的
- 单activity中,进行多接口的请求怎样才是最好的方法(求教)
- Android 中WebView加载本地css和js
- 用ksoap2调用webService出现错误
- android 通过HID与PC通信
- android中数据库的操作
- ImageSwitcher能否实现图片放大与缩小
import java.util.HashMap;
import java.util.List;import james.students.db.StuDBService;
import james.students.entity.Student;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;public class MainActivity extends Activity {
/** Called when the activity is first created. */
EditText et_id;
EditText et_name;
EditText et_score;
Button btn_save;
Button btn_listall;
ListView lv_detail;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initialWidget();
btn_save.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
StuDBService service = new StuDBService(MainActivity.this);
Student student = new Student();
String id = et_id.getText().toString();
String name = et_name.getText().toString();
String score = et_score.getText().toString();
student.setStu_id(new Integer(id));
student.setStu_name(name);
student.setStu_score(new Integer(score));
service.insert(student);
et_id.setText("");
et_name.setText("");
et_score.setText("");
Toast.makeText(MainActivity.this, "保存记录成功!", Toast.LENGTH_LONG).show();
}
});
btn_listall.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
StuDBService service = new StuDBService(MainActivity.this);
List<Student> students = service.getAll();
ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
for(Student stu:students){
HashMap<String,Object> map = new HashMap<String,Object>();
map.put("id", stu.getStu_id());
map.put("name", stu.getStu_name());
map.put("score", stu.getStu_score());
data.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, data, R.layout.studetail, new String[]{"id","name","score"}, new int[]{R.id.id,R.id.name,R.id.score});
lv_detail.setAdapter(adapter);
Toast.makeText(MainActivity.this, "显示成功!", Toast.LENGTH_LONG).show();
lv_detail.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Log.i("TEST", "fdaga");
Toast.makeText(MainActivity.this, "hehe", Toast.LENGTH_SHORT).show();
}
});
}
});
}
private void initialWidget()
{
et_id= (EditText) findViewById(R.id.et_id);
et_name =(EditText) findViewById(R.id.et_name);
et_score = (EditText) findViewById(R.id.et_score);
btn_save = (Button) findViewById(R.id.btn_save);
btn_listall = (Button) findViewById(R.id.btn_listall);
lv_detail = (ListView) findViewById(R.id.lv_studetail);
}
}
Toast.makeText(MainActivity.this, "显示成功!", Toast.LENGTH_LONG).show();
lv_detail.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Log.i("TEST", "fdaga");
Toast.makeText(MainActivity.this, "hehe", Toast.LENGTH_SHORT).show();
}
});
这个拿出来和其他2个监听器并列放置
android:descendantFocusability=”blocksDescendant”
即可