0

I have phonebook app which stores new Contacts in sqlite table. I have activities to add and (list)View contacts, and they are working just fine.

Now, I need a Details activity which will show the details of contact cliked in the listView. I know that I should use onItemClickListener(), but everything that I have tried is not working.

Details Activity is containt three TextViews (name, last name, phone).
As I'm a beginner, would you be so kind telling me how to do it?

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 ???



}}
szentmihaly
  • 39
  • 11
  • Does Intent myIntent = new Intent(getBaseContext(), DetailView.class); startActivity(myIntent); Works? does it transfer you to Details Activity ? – Ahmad Sanie Oct 27 '16 at 12:34
  • Well, you should probably start with getting a `new UserDbHelper` in the second class. StackOverflow isn't a tutorial site. Please put some attempt into the problem. You also should look at how to make Parcelable objects – OneCricketeer Oct 27 '16 at 12:35
  • @cricket_007 yeah its not perfect but from what I see the fetch mechanism is working just fine; I think he needs to pass data through intents that's all! – Ahmad Sanie Oct 27 '16 at 12:38
  • @AhmadAlsanie, yes, the mechanism is working just fine, it does transfer me to Details Activity. . – szentmihaly Oct 27 '16 at 12:59
  • @cricket_007, I have tried multiple solutions and all I got are multiple errors, that's wht I asked how to do it. In the best case scenario, when I try to pass at least one string, in the Detail View that TextView is empty – szentmihaly Oct 27 '16 at 12:59

2 Answers2

0

Okay I'll assume that:

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

Works.

Still there are a lot of missing links in your code, first you are retrieving (name, lastName and phone) but where does it go! I don't see anything that fill up your list with the data above so you have to do something like this note please rename your DataProvider to Contact its much more better in my opinion:

1-Fill up an ArrayList<Contact> with your data in the while loop

2-Populate your listView with your ArrayList see this.

3-Get data from the selected item in the listView

For example:

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

4-Pass the data through intent:

In your DisplayDataActivity:

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

In your DetailView:

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

Hope this will help!

Community
  • 1
  • 1
Ahmad Sanie
  • 3,628
  • 2
  • 16
  • 52
0

Firstly, you should avoid raw types where possible. Meaning that you can specify what type of class these objects hold.

public class ListDataAdapter extends ArrayAdapter<DataProvider> {

Then, you shouldn't store your own list object within the adapter, so I removed that line.

Secondly, if you don't plan on using different layouts for the adapter, I'd suggest one or both of these constructors instead

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

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

These methods aren't necessary to implement with the current changes. (The ArrayAdapter super class gets the correct values).

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

Now, when you click on a item, you can

DataProvider clicked = listDataAdapter.getItem(position);

As far as passing the data to a new Activity, you have options, but I vote Parcelable. You can also try Serializable just to see if it works how you want.

You can putExtra into the intent that starts the Activity, then you have to getIntent().getParcelableExtra on the other Activity (in the case of a Parcelable object) to receive the data. Those methods are well documented elsewhere like

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

You probably could mess around with passing the ID value from sqlite, then querying it from the other side, but you only should do that if you don't already have the whole DataProvider object sitting in the Adapter.

Community
  • 1
  • 1
OneCricketeer
  • 126,858
  • 14
  • 92
  • 185