我做了一个简单的任务,从数据库中获取数据,然后在listview上充气。但是当我点击listrow它没有被选中时我遇到问题,当我点击我从数据库列出的文本然后它执行选择(setonitemclicklistenr工作)....我还想将该特定的listrow_text传递给其他活动来编辑该文本....我尝试了一种方法在getview()视图上执行clicklistener但它不接受startActivity (意图)命令
如果我在活动类中使用setonitemclicklistener,我该如何获得该文本... 我是一个初学者,在理解代码时遇到了问题,我搜索了我的问题......
列出我使用自定义适配器的类
package com.example.smarttext2;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class dialoglist extends Activity {
ListView list;
Cursor cursor;
int count;
String[] str;
dialogadapter adapter;
database demo2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.dialoglist);
demo2 = new database(getApplicationContext());
list = (ListView)findViewById(R.id.listView1);
cursor = demo2.fetch();
count = cursor.getCount();
str = new String[count];
cursor.moveToFirst();
for(int i=0;i<count;i++)
{
str[i]=cursor.getString(0);
cursor.moveToNext();
}
adapter = new dialogadapter(this, str);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapview, View view, int position,
long pos_id) {
// TODO Auto-generated method stub
String pos = view.toString();//this line gives some value like android.widget.linearlayout473y5834434....
//Toast.makeText(getApplicationContext(), pos, Toast.LENGTH_LONG).show();
Intent intent = new Intent(dialoglist.this,third.class);
intent.putExtra("key",pos);
startActivity(intent);
}
});
}
}
适配器类
package com.example.smarttext2;
public class dialogadapter extends BaseAdapter {
Context context;
String[] str1;
LayoutInflater inflater;
public dialogadapter(Context con,String[] st1) {
// TODO Auto-generated constructor stub
context = con;
str1 = st1;
inflater = ((Activity)con).getLayoutInflater();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return str1.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View v, ViewGroup parent) {
// TODO Auto-generated method stub
v = inflater.inflate(R.layout.dialoglistitem, null);
final TextView text = (TextView)v.findViewById(R.id.textView1);
text.setText(str1[position]);
return v;
}
}
我想发送数据的活动
package com.example.smarttext2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class third extends Activity {
EditText et1;
Button btn1;
database demo;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.third);
Intent intent = getIntent();
String str1 = intent.getStringExtra("key");
et1 =(EditText)findViewById(R.id.editText1);
btn1 = (Button)findViewById(R.id.button1);
et1.setText(str1);
demo = new database(getApplicationContext());
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
demo.add(et1.getText().toString());
finish();
}
});
}
}
答案 0 :(得分:0)
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int arg2,
final long arg3) {
// TODO Auto-generated method stub
TextView text = (TextView) v.findViewById(R.id.textView1);
String string = text.getText().toString();
Toast.makeText(getApplicationContext(), string, 3000).show();
Intent intent = new Intent(dialoglist.this,third.class);
intent.putExtra("key", string);
startActivity(intent);
}
});