timroes / SwipeToDismissUndoList

An Android library to enable swipe to dismiss and undo functionality on ListViews.
250 stars 72 forks source link

having problem with MyItem. It says "MyItem cannot be resolved to a type". please help!! #27

Closed ac1692 closed 10 years ago

ac1692 commented 10 years ago

this is my code:- MainActivity.java package swipe.test;

import de.timroes.swipetodismiss.SwipeDismissList; import de.timroes.swipetodismiss.SwipeDismissList.UndoMode; import de.timroes.swipetodismiss.SwipeDismissList.Undoable; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.ListView; import android.os.Build;

public class MainActivity extends ActionBarActivity {

private final static String month[] = {"January","February","March","April","May",
    "June","July","August","September","October","November","December"};

private final static String number[] = {"Month - 1", "Month - 2","Month - 3",
    "Month - 4","Month - 5","Month - 6",
    "Month - 7","Month - 8","Month - 9",
    "Month - 10","Month - 11","Month - 12"};
ListView lview;
UndoMode mode;
ListViewAdapter lviewAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lviewAdapter = new ListViewAdapter(this, month, number);
    lview = (ListView) findViewById(R.id.listView1);
    SwipeDismissList.OnDismissCallback callback = new SwipeDismissList.OnDismissCallback() {
        // Gets called whenever the user deletes an item.
        public SwipeDismissList.Undoable onDismiss(AbsListView listView, final int position) {
            // Get your item from the adapter (mAdapter being an adapter for MyItem objects)
            final MyItem deletedItem = lviewAdapter.getItem(position);
            // Delete item from adapter
            lviewAdapter.remove(deletedItem);
            // Return an Undoable implementing every method
            return new SwipeDismissList.Undoable() {

                // Method is called when user undoes this deletion
                public void undo() {
                    // Reinsert item to list
                    lviewAdapter.insert(deletedItem, position);
                }

                // Return an undo message for that item
                public String getTitle() {
                    return deletedItem.toString() + " deleted";
                }

                // Called when user cannot undo the action anymore
                public void discard() {
                    // Use this place to e.g. delete the item from database
                    finallyDeleteFromSomeStorage(deletedItem);
                }
            };
        }
    };

    SwipeDismissList swipeList = new SwipeDismissList(lview, callback, mode);
    lview.setAdapter(lviewAdapter);
}

}

package swipe.test;

ListViewAdapter.java

import android.app.Activity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter { Activity context; String title[]; String description[];

public ListViewAdapter(Activity context, String[] title, String[] description) {
    super();
    this.context = context;
    this.title = title;
    this.description = description;
}

public int getCount() {
    // TODO Auto-generated method stub
    return title.length;
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

private class ViewHolder {
    TextView txtViewTitle;
    TextView txtViewDescription;
}

public View getView(int position, View convertView, ViewGroup parent)
{
    // TODO Auto-generated method stub
    ViewHolder holder;
    LayoutInflater inflater =  context.getLayoutInflater();

    if (convertView == null)
    {
        convertView = inflater.inflate(R.layout.inner, null);
        holder = new ViewHolder();
        holder.txtViewTitle = (TextView) convertView.findViewById(R.id.fname);
        holder.txtViewDescription = (TextView) convertView.findViewById(R.id.lname);
        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txtViewTitle.setText(title[position]);
    holder.txtViewDescription.setText(description[position]);

return convertView;
}

}

activity_main.xml <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="swipe.test.MainActivity" tools:ignore="MergeRootFrame" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

timroes commented 10 years ago

You have the line: final MyItem deletedItem = lviewAdapter.getItem(position); I don't see an import to a MyItem class in the top of the file. If you store MyItem obejcts in the adapter you will need to import it. That is also what the error means. If a type cannot be found, you have forgotten to import it correctly (or it doesn't exist at all).

ac1692 commented 10 years ago

Please provide an example for this. How and where should I write it in my adapter?

timroes commented 10 years ago

This is a very, very basic java coding question not anyhow related to this library. Sorry, but I don't want to describe that in any more detail then I did above. If that wasn't understandable, I highly recommend reading some Java coding guides to get more known to the language.