.setOnItemClicklistener不起作用

时间:2016-10-23 12:52:36

标签: android android-intent android-sqlite

我想在意图和uri的帮助下将我的数据从一个活动发送到另一个活动但是任务没有完成

我的主要活动代码

package com.example.vidit.inventoryapp;

import android.app.LoaderManager;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ListView;

import static android.R.attr.id;

public class MainActivity extends AppCompatActivity implements    LoaderManager.LoaderCallbacks<Cursor> {

/** Identifier for the pet data loader */
private static final int ITEM_LOADER = 0;

/** Adapter for the ListView */
ItemCursorAdapter mCursorAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          Intent intent = new Intent(MainActivity.this,editor.class);
            startActivity(intent);
        }
    });

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

    // Find and set empty view on the ListView, so that it only shows when the list has 0 items.
    View emptyView = findViewById(R.id.empty_view);
    ItemListView.setEmptyView(emptyView);

    // Setup an Adapter to create a list item for each row of pet data in the Cursor.
    // There is no pet data yet (until the loader finishes) so pass in null for the Cursor.
    mCursorAdapter = new ItemCursorAdapter(this, null);
    ItemListView.setAdapter(mCursorAdapter);

    // Setup the item click listener
    ItemListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            // Create new intent to go to {@link EditorActivity}
            Intent intent = new Intent(MainActivity.this,editor.class);

            Uri itemUri = ContentUris.withAppendedId(ItemContract.ItemEntry.CONTENT_URI, id);

            intent.setData(itemUri);

            startActivity(intent);
        }
    });

    // Kick off the loader
    getLoaderManager().initLoader(ITEM_LOADER, null, this);
}

private void insertPet() {
    // Create a ContentValues object where column names are the keys,
    // and Toto's pet attributes are the values.
    ContentValues values = new ContentValues();
    values.put(ItemContract.ItemEntry.NAME, "Football");
    values.put(ItemContract.ItemEntry.PRICE, 200);
    values.put(ItemContract.ItemEntry.STATUS, ItemContract.ItemEntry.ACCEPTED);
    values.put(ItemContract.ItemEntry.QUANTITY, 7);


    // Insert a new row for Toto into the provider using the ContentResolver.
    // Use the {@link PetEntry#CONTENT_URI} to indicate that we want to insert
    // into the pets database table.
    // Receive the new content URI that will allow us to access Toto's data in the future.
    Uri newUri = getContentResolver().insert(ItemContract.ItemEntry.CONTENT_URI, values);
}

/**
 * Helper method to delete all pets in the database.
 */
private void deleteAllPets() {
    int rowsDeleted = getContentResolver().delete(ItemContract.ItemEntry.CONTENT_URI, null, null);
    Log.v("CatalogActivity", rowsDeleted + " rows deleted from pet database");
}
@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_main, 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);
}

@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
    String[] projection = {
            ItemContract.ItemEntry._ID,
            ItemContract.ItemEntry.NAME,
            ItemContract.ItemEntry.PRICE,
            ItemContract.ItemEntry.QUANTITY,
            };

    // This loader will execute the ContentProvider's query method on a background thread
    return new CursorLoader(this,   // Parent activity context
            ItemContract.ItemEntry.CONTENT_URI,   // Provider content URI to query
            projection,             // Columns to include in the resulting Cursor
            null,                   // No selection clause
            null,                   // No selection arguments
            null);

}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    mCursorAdapter.swapCursor(data);

}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
    mCursorAdapter.swapCursor(null);

}

}

我的适配器类:

 package com.example.vidit.inventoryapp;

 import android.content.ContentUris;
 import android.content.ContentValues;
 import android.content.Context;
 import android.content.Intent;
 import android.database.Cursor;
 import android.net.Uri;
 import android.text.TextUtils;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.Button;
 import android.widget.CursorAdapter;
 import android.widget.TextView;

 import static android.R.attr.id;
 import static android.R.attr.order;
 import static android.R.attr.value;
 import static android.os.Build.VERSION_CODES.M;



public class ItemCursorAdapter extends CursorAdapter {
public ItemCursorAdapter(Context context, Cursor c) {
    super(context, c, 0 /* flags */);
}

public View newView(Context context, Cursor cursor, ViewGroup parent) {
    // Inflate a list item view using the layout specified in list_item.xml
    return LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
}

@Override
public void bindView(View view, final Context context, final Cursor cursor) {
    // Find individual views that we want to modify in the list item layout
    TextView nameTextView = (TextView) view.findViewById(R.id.name);
    TextView priceTextView = (TextView) view.findViewById(R.id.price);
    final TextView quantityTextView = (TextView) view.findViewById(R.id.quantity);
    Button sell=(Button) view.findViewById(R.id.sell);
    Button order=(Button) view.findViewById(R.id.order);
    Button detail =(Button)  view.findViewById(R.id.detail);





    // Find the columns of pet attributes that we're interested in
    int nameColumnIndex = cursor.getColumnIndex(ItemContract.ItemEntry.NAME);
    int priceColumnIndex = cursor.getColumnIndex(ItemContract.ItemEntry.PRICE);
    int quantityColumnIndex = cursor.getColumnIndex(ItemContract.ItemEntry.QUANTITY);



    // Read the  attributes from the Cursor for the current pet
    String itemName = cursor.getString(nameColumnIndex);
    String itemPrice = cursor.getString(priceColumnIndex);
     final String itemQuantity=cursor.getString(quantityColumnIndex);


    if (TextUtils.isEmpty(itemPrice)) {
        itemPrice = "Not known yet";
    }
    if (TextUtils.isEmpty(itemQuantity)) {
        itemPrice = "Not known yet";
    }

    final int qua=Integer.parseInt(itemQuantity);
    nameTextView.setText(itemName);
    priceTextView.setText(itemPrice);
    quantityTextView.setText(itemQuantity);

    sell.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(Integer.parseInt(itemQuantity)>0)
            {
                int x=Integer.parseInt(itemQuantity)-1;

                quantityTextView.setText("" + x);
                int idColumnIndex = cursor.getColumnIndex(ItemContract.ItemEntry._ID);
                int id = cursor.getInt(idColumnIndex);
                Uri itemUri = ContentUris.withAppendedId(ItemContract.ItemEntry.CONTENT_URI, id);
                ContentValues values = new ContentValues();
                values.put(ItemContract.ItemEntry.QUANTITY, x);
                context.getContentResolver().update(itemUri, values, null, null);
            }

        }
    });





    order.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
                    "mailto","abc@gmail.com", null));
            emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
            emailIntent.putExtra(Intent.EXTRA_TEXT, "Body");
            context.startActivity(Intent.createChooser(emailIntent, "Send email..."));
        }
    });

   /* view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(context,editor.class);

            Uri itemUri = ContentUris.withAppendedId(ItemContract.ItemEntry.CONTENT_URI, id);
            intent.setData(itemUri);
            context.startActivity(intent);
        }
    });*/
}


}

如果我添加我在适配器类中注释的意图代码,itnet会成功发生,但没有数据从列表视图传递到意图。

提前致谢。

0 个答案:

没有答案