使用onItemClickListener()将数据(存储在Sqlite数据库中)从一个活动传递到另一个活动

时间:2016-10-27 12:26:53

标签: java android sqlite listview onitemclicklistener

我有电话本应用程序,它在sqlite表中存储新的联系人。我有添加和(列表)查看联系人的活动,他们的工作正常。

现在,我需要一个Details活动,它将显示listView中cliked的联系人详细信息。我知道我应该使用onItemClickListener(),但我尝试过的一切都无法正常工作。

详细信息活动包含三个TextView(姓名,姓氏,电话) 因为我是初学者,你会这么善意地告诉我该怎么做吗?

DisplayDataActivity.class:

public class DisplayDataActivity extends AppCompatActivity {




ListView listView;
SQLiteDatabase sqLiteDatabase;
UserDbHelper userDbHelper;
Cursor cursor;
ListDataAdapter listDataAdapter;
SimpleCursorAdapter adapter;
String name, lastName;
TextView proba;

//FROM SEARCH
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);


    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, phone);
            listDataAdapter.add(dataProvider);


        } while (cursor.moveToNext());
    }

    proba = (TextView) findViewById(R.id.text_user_name);

    // Opens detailed view when contact is clicked
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            // What code goes here??

            Intent myIntent = new Intent(getBaseContext(), DetailView.class);
            startActivity(myIntent);


    });}  

DataProvider.class:

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 String getPhone() { return phone; }

public void setPhone(String phone) {this.phone = phone;}



public DataProvider(String name, String lastName, String phone)
{
    this.name = name;
    this.lastName = lastName;
    this.phone = phone;


}}  

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;


}}  

DetailView.class:

public class DetailView extends AppCompatActivity {

TextView entryName, entryLastName, entryPhone;
String strName, strLast, strPhone;
ImageView entryPhoto;
Cursor cursor;

private DataProvider dataProvider;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detail_view);





    //What code goes here ???



}}

2 个答案:

答案 0 :(得分:0)

好的,我会假设:

Intent myIntent = new Intent(getBaseContext(), DetailView.class);
startActivity(myIntent);

作品。

您的代码中仍有很多缺失的链接,首先您要检索(姓名,姓名和电话),但它会去哪里!我没有看到任何内容填满您的列表上面的数据,所以你必须做这样的事情,请在我看来更好地将DataProvider重命名为Contact

1 - 使用while循环中的数据填写ArrayList<Contact>

2 - 使用ArrayList填充listView并this

3 - 从listView

中的所选项目中获取数据

例如:

String phone =((TextView)view.findViewById(R.id.tv_phone)).getText().toString();

4 - 通过意图传递数据:

DisplayDataActivity

Intent myIntent = new Intent(getBaseContext(), DetailView.class);
intent.putExtra("phoneKey", phone);
startActivity(myIntent);

DetailView

Bundle bundle = getIntent().getExtras();
String phone = bundle.getString("phoneKey");

希望这会有所帮助!

答案 1 :(得分:0)

首先,您应尽可能避免使用原始类型。这意味着您可以指定这些对象包含的类类型。

public class ListDataAdapter extends ArrayAdapter<DataProvider> {

然后,您不应该在适配器中存储自己的列表对象,因此我删除了该行。

其次,如果您不打算为适配器使用不同的布局,我建议使用其中一个或两个构造函数

public ListDataAdapter(Context context) {
    super(context, 0, new ArrayList<DataProvider>());
}

public ListDataAdapter(Context context, ArrayList<DataProvider> data) {
    super(context, 0, data);
}

使用当前更改来实现这些方法并不是必需的。 (ArrayAdapter超类获取正确的值)。

@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);
}

现在,当您点击某个项目时,您可以

DataProvider clicked = listDataAdapter.getItem(position);

至于将数据传递给新的Activity,你有选项,但我投票Parcelable。您也可以尝试使用Serializable来查看它是否有效。

您可以putExtra进入启动Activity的意图,然后您必须getIntent().getParcelableExtra在另一个Activity(在Parcelable对象的情况下)接收数据。这些方法在其他地方有详细记载,如

How to send an object from one Android Activity to another using Intents?

你可能会从sqlite传递ID值,然后从另一端查询它,但如果你还没有将整个DataProvider对象放在适配器中,那么你应该这样做。