简体   繁体   中英

Custom adapter for listview using pasrequery

I am trying to create a demo app where the users enter a search and destination, the data will be queried from parse.com and then the matching result will be displayed in the next screen's listview. I have followed a few tutorials but for sure haven't been able to grasp the concept concretely enough. When I enter a search query, the progress bar shows up and then the application goes back into the previous activity. I can't understand where exactly things are going wrong.

This is my mainactivity class.

public class CarpoolingActivitySearch extends ActionBarActivity {

ListView listView;
ArrayList<Travellers> travellers;
CarpoolingAdapter adapter;
protected ProgressDialog proDialog;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_carpooling_activity_search);


    final EditText mSrc = (EditText)findViewById(R.id.carpooling_source);
    final EditText mDst = (EditText)findViewById(R.id.destination);

    Button mSubmitButton = (Button)findViewById(R.id.carpooling_submit);
    mSubmitButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String source = mSrc.getEditableText().toString();
            String destination = mDst.getEditableText().toString();

            listView = (ListView) findViewById(R.id.list);

            ParseQuery<ParseObject> query = ParseQuery.getQuery("Carpooling");
            query.whereEqualTo("Source", source); //assume you have a DonAcc column in your Country table
            query.whereEqualTo("Destination", destination); //assume you have a DonAcc column in your Country table

            startLoading();
            query.findInBackground(new FindCallback<ParseObject>() {
                @Override
                public void done(List<ParseObject> parseObjects, ParseException e) {
                   if(e==null)
                   {
                      for(int i=0;i<parseObjects.size();i++)
                       {
                           Travellers travellers1 =  new Travellers();
                           travellers1.setSource("Source");
                           travellers1.setDestination("Destination");

                           travellers.add(travellers1);
                           if(travellers.size() > 0)
                           {
                               adapter = new CarpoolingAdapter(getApplicationContext(),R.layout.activity_carpooling_activity_search,travellers);
                               listView.setAdapter(adapter);
                   } else {


                               AlertDialog.Builder popup = new AlertDialog.Builder(CarpoolingActivitySearch.this);
                               popup.setMessage("Seems our servers are busy. Try again in some time.");
                               popup.setPositiveButton("Back",null);

                               AlertDialog dialog = popup.create();
                               dialog.show();

                           }
                        }

                     stopLoading();
                       finish();
                   }
                   else {
                       stopLoading();
                       AlertDialog.Builder popup = new AlertDialog.Builder(CarpoolingActivitySearch.this);
                       popup.setMessage(e.getMessage());
                       popup.setPositiveButton("Back",null);

                       AlertDialog dialog = popup.create();
                       dialog.show();



                   }
                }
            });
        }
    });

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_carpooling_activity_search, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
protected void startLoading() {
    proDialog = new ProgressDialog(this);
    proDialog.setMessage("loading...");
    proDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    proDialog.setCancelable(false);
    proDialog.show();
}

protected void stopLoading() {
    proDialog.dismiss();
    proDialog = null;
  }
}

I have created a custom adapter for this purpose.

public class CarpoolingAdapter extends ArrayAdapter<Travellers> {
ArrayList<Travellers> travellersArrayList;
LayoutInflater vi;
int Resource;
ViewHolder holder;
Context context;

public CarpoolingAdapter(Context context, int resource, ArrayList<Travellers> objects) {
    super(context, resource, objects);
    vi = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    Resource = resource;
    travellersArrayList = objects;
    this.context = context;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView== null) {
        convertView = vi.inflate(Resource,null);
        holder = new ViewHolder();
        holder.mSource = (TextView)convertView.findViewById(R.id.textView);
        holder.mDestination = (TextView)convertView.findViewById(R.id.textView2);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder)convertView.getTag();
    }
    holder.mSource.setText(travellersArrayList.get(position).getSource());
    holder.mDestination.setText(travellersArrayList.get(position).getDestination());

    return convertView;
}

static class ViewHolder {
    public TextView mSource;
    public TextView mDestination;

 }
}

And the travellers class with a default constructor and getter/setter methods.

  public class Travellers {

private String Source;


private String Destination;

public Travellers() {

}

public Travellers(String source, String destination) {
    super();
    Source = source;
    Destination = destination;
}


public String getSource() {
    return Source;
}

public void setSource(String source) {
    Source = source;
}

public String getDestination() {
    return Destination;
}

public void setDestination(String destination) {
    Destination = destination;
 }

}

The main layout file named activity_carpooling_activity_search.xml.

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.android.myapplication.Carpooling.Carpooling">

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/carpooling_source"
    android:hint="Source"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/destination"
    android:hint="Destination"
    android:layout_below="@+id/carpooling_source"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="86dp" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Submit"
    android:id="@+id/carpooling_submit"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/scrollView"
    android:layout_toEndOf="@+id/carpooling_submit"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true"
    android:layout_below="@+id/carpooling_submit">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/list" />
  </ScrollView>
</RelativeLayout>

And finally my list_item_deatil.xml file.

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">

<TextView
    android:layout_width="174dp"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView"
    android:layout_weight="0.13" />

<TextView
    android:layout_width="245dp"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView2"
    android:layout_weight="0.13" />
 </LinearLayout>

What should I modify to make it work appropriately? I am not getting any errors as such.

First of all, you have posted a very long code blocks where indentation is not good enough to read you code. So what I understood, as your ParseException e is null that means there is no exception. Inside that if block, after the for loop you are calling finish() which is causing your current activity instance to be destroyed thus going to previous activity. I hope this helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM