无法在android的列表视图中获取已单击的行号
我试图建立一个简单的短信应用程序,编辑页面列出所有保存的短信。我在布局中提供了两个按钮用于编辑和删除,但当我单击它们时,我无法获取rowid(从顶部开始的行号)以检索对应于该特定行的sms。。有人请帮助我。我的sms适配器没有扩展任何内容。这是我的类和xml文件无法在android的列表视图中获取已单击的行号,android,listview,android-layout,android-listview,listactivity,Android,Listview,Android Layout,Android Listview,Listactivity,我试图建立一个简单的短信应用程序,编辑页面列出所有保存的短信。我在布局中提供了两个按钮用于编辑和删除,但当我单击它们时,我无法获取rowid(从顶部开始的行号)以检索对应于该特定行的sms。。有人请帮助我。我的sms适配器没有扩展任何内容。这是我的类和xml文件 public class SMSEdit extends ListActivity{ private SMSDbAdapter mDbHelper; //private Button editsmsbtn, deletesmsbt
public class SMSEdit extends ListActivity{
private SMSDbAdapter mDbHelper;
//private Button editsmsbtn, deletesmsbtn;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editsmslist);
mDbHelper = new SMSDbAdapter(this);
mDbHelper.open();
fillData();
}
public void fillData() {
Cursor smsCursor = mDbHelper.fetchAllNotes();
startManagingCursor(smsCursor);
// Create an array to specify the fields we want to display in the list (only TITLE)
String[] from = new String[]{SMSDbAdapter.KEY_TITLE,
SMSDbAdapter.KEY_BODY,
SMSDbAdapter.KEY_DATE,
SMSDbAdapter.KEY_TIME};
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{R.id.smseditlistTO,
R.id.smseditlistMessage,
R.id.smseditlistDay,
R.id.smseditlistTime};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter smsadap =
new SimpleCursorAdapter(this, R.layout.smsrow, smsCursor, from, to);
setListAdapter(smsadap);
}
public void myEditClickHandler(View v)
{
//get the row the clicked button is in
LinearLayout vwParentRow = (LinearLayout)v.getParent();
Button btnChild = (Button)vwParentRow.getChildAt(4);
btnChild.setText("I've been clicked!");
ListAdapter pos = getListAdapter();
Intent intent = new Intent();
intent.setClass(SMSEdit.this, SMS.class);
// intent.putExtra(SMSDbAdapter.KEY_ROWID, id);
startActivity(intent);
finish();
}
public void myDeleteClickHandler(View v)
{
//get the row the clicked button is in
LinearLayout vwParentRow = (LinearLayout)v.getParent();
//TextView child = (TextView)vwParentRow.getChildAt(0);
Button btnChild = (Button)vwParentRow.getChildAt(5);
//btnChild.setText(child.getText());
btnChild.setText("I've been clicked!");
int c = Color.CYAN;
vwParentRow.setBackgroundColor(c);
vwParentRow.refreshDrawableState();
}
}
editsmslist.xml-->
smsrow.xml-->>
另外,请建议是否有更好的方法来实现此功能或使布局看起来更好…我的
列表视图也有类似的问题
假设您正在动态填充列表视图。您有5行(例如)。第一行的id为1,第二行的id为2,依此类推
当您滚动任何可见的行时,第一行(您当前在屏幕上看到的)的id将始终为1。您需要使用另一个类(XYZ
)来控制它
我只是简单地使用了ArrayList
,这就奏效了
您可以在SMSAdapter
中重写此方法:
public View getView(int position, View convertView, ViewGroup parent)
然后调用get(position)
方法,该方法返回一个ArrayList
,您应该使用ListView来显示消息
将从数据库检索到的数据存储在字符串数组中,例如s1。
然后
private ArrayList arr_sort=new ArrayList()代码>
并使用add()方法将整个s1复制到arr_sort中
设置适配器时
lv.setAdapter(新的阵列适配器(DataAttach.this、,
android.R.layout.simple_list_item_1,arr_sort))代码>
其中lv是ListView引用
然后将此方法用于onClick
lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String item = (String) lv.getItemAtPosition(position);
Toast.makeText(*.this,
"You selected - " + item + " ,Please wait...",
Toast.LENGTH_LONG).show();
Intent intent = new Intent(*.this, *.class);
*.this.startActivity(intent);
}
});
lv.setOnItemClickListener(新的android.widget.AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
字符串项=(字符串)lv.getItemAtPosition(位置);
敬酒,把这个,
“您选择了-”+项目+“,请稍候…”,
Toast.LENGTH_LONG).show();
意向意向=新意向(*.this,*.class);
*.这一点。开始触觉(意图);
}
});
希望这有帮助。您可以像这样扩展游标适配器
public class exampleAdapter extends CursorAdapter
{
LayoutInflater inflater;
public exampleAdapter (Context context, Cursor c) {
super(context, c);
inflater = LayoutInflater.from(context);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
Button btn_del = (Button)view.findViewById(R.id.button1);
btn_del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SMSDbAdapter vb = new SMSDbAdapter(context);
vb.open();
String xyz = cursor.getString(0); //Persumably your ID
vb.Delete(Integer.parseInt(cursor.getString(0)));
Toast.makeText(context, xyz + " Deleted!", Toast.LENGTH_SHORT).show();
vb.close();
}
});
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(R.layout.smsrow, parent, false);
}
}
lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String item = (String) lv.getItemAtPosition(position);
Toast.makeText(*.this,
"You selected - " + item + " ,Please wait...",
Toast.LENGTH_LONG).show();
Intent intent = new Intent(*.this, *.class);
*.this.startActivity(intent);
}
});
public class exampleAdapter extends CursorAdapter
{
LayoutInflater inflater;
public exampleAdapter (Context context, Cursor c) {
super(context, c);
inflater = LayoutInflater.from(context);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
Button btn_del = (Button)view.findViewById(R.id.button1);
btn_del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SMSDbAdapter vb = new SMSDbAdapter(context);
vb.open();
String xyz = cursor.getString(0); //Persumably your ID
vb.Delete(Integer.parseInt(cursor.getString(0)));
Toast.makeText(context, xyz + " Deleted!", Toast.LENGTH_SHORT).show();
vb.close();
}
});
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(R.layout.smsrow, parent, false);
}
}