我有电话本应用程序,它在sqlite表中存储新的联系人。我有活动要添加和(列表)查看联系人,他们工作得很好
现在,我需要一个Details活动,它将显示listView中cliked的联系人详细信息。
我知道我应该使用onItemClickListener()
,但我尝试的所有内容都无效。详细信息活动包含三个TextView(名称,姓氏,电话)
我所能得到的只是一个错误:
无法将DataProvider强制转换为java.lang.String
如果有其他方法可以做到这一点,请好好告诉我如何做到这一点。
这是DisplayDataActivity.java:
public class DisplayDataActivity extends AppCompatActivity {
ListView listView;
SQLiteDatabase sqLiteDatabase;
UserDbHelper userDbHelper;
Cursor cursor;
ListDataAdapter listDataAdapter;
EditText Search_Last;
String search_Last;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_data);
Search_Last = (EditText) findViewById(R.id.searchName);
// This part of the code is from tutorial
listView = (ListView) findViewById(R.id.list_view_display);
listDataAdapter = new ListDataAdapter(getApplicationContext(), R.layout.row_layout);
listView.setAdapter(listDataAdapter);
userDbHelper = new UserDbHelper(getApplicationContext());
sqLiteDatabase = userDbHelper.getReadableDatabase();
cursor = userDbHelper.getInformations(sqLiteDatabase);
if (cursor.moveToFirst()) {
do {
String name, lastName, phone;
name = cursor.getString(0);
lastName = cursor.getString(1);
phone = cursor.getString(2);
DataProvider dataProvider = new DataProvider(name, lastName);
listDataAdapter.add(dataProvider);
} while (cursor.moveToNext());
}
// Opens detailed view when contact is clicked
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView entryName = (TextView) findViewById(R.id.entryNameDetail);
TextView entryLastName = (TextView) findViewById(R.id.entryLastDetail);
TextView entryPhone = (TextView) findViewById(R.id.entryPhoneDetail);
String data = (String) parent.getItemAtPosition(position);
String Name = entryName.getText().toString();
String Last = entryLastName.getText().toString();
String Phone = entryPhone.getText().toString();
Intent modify_intent = new Intent(getApplicationContext(), DetailView.class);
modify_intent.putExtra("Name", Name);
modify_intent.putExtra("last", Last);
modify_intent.putExtra("phone", Phone);
startActivity(modify_intent);
}
});
}
这是DataProvider.java:
public class DataProvider {
private String name;
private String lastName;
private String phone;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public DataProvider(String name, String lastName)
{
this.name = name;
this.lastName = lastName;
}}
DetailView.java:
public class DetailView extends AppCompatActivity {
TextView entryName, entryLastName, entryPhone;
ImageView entryPhoto;
Cursor cursor;
private DataProvider dataProvider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_view);
/* WHEN I RUN THIS PART OF THE CODE, THE NAME TEXTVIEW SHOWS EMPTY
Bundle data_from_list = getIntent().getExtras();
String value_in_name = data_from_list.getString("passed key data");
TextView entryName = (TextView)findViewById(R.id.entryNameDetail);
entryName.setText(value_in_name);
*/
/* Running this code throws me an error from above */
Intent modify_intent = getIntent();
String s1 = modify_intent.getStringExtra("Name");
String s2 = modify_intent.getStringExtra("Last");
String s3 = modify_intent.getStringExtra("Phone");
entryName = (TextView) findViewById(R.id.entryNameDetail);
entryLastName = (TextView) findViewById(R.id.entryLastDetail);
entryPhone = (TextView) findViewById(R.id.entryPhoneDetail);
entryName.setText(s1);
entryLastName.setText(s2);
entryPhone.setText(s3);
// Adding hardcoded data into TextView for testing purposes
// WORKING
//entryName.setText("Test ime");
//entryLastName.setText("Test PRezime");
//entryPhone.setText("test telefon");
}}
ListDataAdapter.class:
public class ListDataAdapter extends ArrayAdapter {
List list = new ArrayList();
public ListDataAdapter(Context context, int resource) {
super(context, resource);
}
static class LayoutHandler
{
TextView NAME, LASTNAME;
}
@Override
public void add(Object object) {
super.add(object);
list.add(object);
}
@Override
public int getCount() {
return list.size();
}
@Nullable
@Override
public Object getItem(int position) {
return list.get(position);
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
LayoutHandler layoutHandler;
if(row == null)
{
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.row_layout, parent, false);
layoutHandler = new LayoutHandler();
layoutHandler.NAME = (TextView) row.findViewById(R.id.text_user_name);
layoutHandler.LASTNAME = (TextView) row.findViewById(R.id.text_user_last);
row.setTag(layoutHandler);
}
else
{
layoutHandler = (LayoutHandler) row.getTag();
}
DataProvider dataProvider = (DataProvider) this.getItem(position);
layoutHandler.NAME.setText(dataProvider.getName());
layoutHandler.LASTNAME.setText(dataProvider.getLastName());
return row;
}}
答案 0 :(得分:1)
listDataAdapter.add(dataProvider);
我认为您必须将dataProvider(您要添加到列表中的数据)添加到ArrayList而不是自定义适配器。
而是将自定义适配器与ArrayList和ListView相关联以进行可视化。
答案 1 :(得分:1)
为什么需要这条线?
epislon
您没有在任何地方使用该变量,并且该行正在为您提供该错误(可能)。
另外我认为你应该在onItemClick监听器中使用view.findViewById而不仅仅是findViewById。