android,无法解析片段内的getListAdapter和setOnItemLongClickListener

时间:2015-11-29 09:18:37

标签: android fragment syntax-error

在这里,我正在尝试制作OnItemLongClick,我之前尝试在ListActivity进行此操作。 现在我想在Fragment中创建一个,但是我在这里和那里得到一个错误,我需要一些建议/建议/答案来解决错误,或者正确的方法来实现该方法。

在ListActivity(工作)中:

public class ViewData extends ListActivity implements OnItemLongClickListener {

//init controller
private DBDataSource dataSource;

//init arraylist
private ArrayList<Barang> values;
private Button delButton;

@Override
public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.viewdata);
      dataSource = new DBDataSource(this);
      // open controller
      dataSource.open();

      // get values
      values = dataSource.getAllBarang();

      // insert data to array adapter
      ArrayAdapter<Barang> adapter = new ArrayAdapter<Barang>(this,
      android.R.layout.simple_list_item_1, values);

      // set adapter in list
      setListAdapter(adapter);

      // listview for set onItemLongClickListener
      ListView lv = (ListView) findViewById(android.R.id.list);
      lv.setOnItemLongClickListener(this);

}

//if user longclick
@Override
public boolean onItemLongClick(final AdapterView<?> adapter, View v, int pos,
        final long id) {

    //tampilkan alert dialog
    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.dialog_view);
    dialog.setTitle("Pilih Aksi");
    dialog.show();
    final Barang b = (Barang) getListAdapter().getItem(pos);
    delButton = (Button) dialog.findViewById(R.id.button_delete_data);


    //apabila tombol delete di klik
    delButton.setOnClickListener(
            new OnClickListener()
            {
                @Override
                public void onClick(View v) {
                    // Delete barang
                    dataSource.deleteBarang(b.getId());
                    dialog.dismiss();
                    finish();
                    startActivity(getIntent());
                }
            }
            );

    return true;

}
}

片段版本(仍然出错):

public class Menu_Riwayat extends Fragment {
//init controller
private DBDataSource dataSource;

//init arraylist
private ArrayList<Investasi_DB> values;

static ListView lv;
private Button button_hapus;
static LinearLayout mLinear;

@Nullable
@Override

public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {

    mLinear = (LinearLayout) inflater.inflate(R.layout.viewdata, container, false);
    lv = (ListView) mLinear.findViewById(R.id.list);
    dataSource = new DBDataSource(getActivity());
    // open controller
    dataSource.open();

    // get values
    values = dataSource.getAllInvestasi_DB();

    // insert data to array adapter
    ArrayAdapter<Investasi_DB> adapter = new ArrayAdapter<Investasi_DB>(getActivity(),
            android.R.layout.simple_list_item_1, values);

    // set adapter in list
    lv.setAdapter(adapter);
    return mLinear;
    // ERROR IN THIS PART BELOW, not sure what to change "this" with
    lv.setOnItemClickListener(this);
}

public boolean onItemLongClick(final AdapterView<?> adapter, View v, int pos,
                               final long id) {
    //tampilkan alert dialog
    final Dialog dialog = new Dialog(getActivity());
    dialog.setContentView(R.layout.dialog_view);
    dialog.setTitle("Pilihan");
    dialog.show();

    // ERROR IN THIS PART BELOW, not sure how to implement "getListAdapter()" inside fragment
    final Investasi_DB b = (Investasi_DB) getListAdapter().getItem(pos);
    button_hapus = (Button) dialog.findViewById(R.id.button_hapus);
    button_hapus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dataSource.deleteInvestasi_DB(b.getId());
            dialog.dismiss();
            getActivity().finish();
             // ERROR IN THIS PART BELOW, the "getIntent()" part
            startActivity(getIntent());

        }
    });

    return true;
}
}

我需要帮助来解决错误(正确的实施方式):
getListAdapter()

中的getIntent()thislv.setOnItemClickListener(this);

一些建议后的编辑部分: 所以现在我就这样做了

public class Menu_Riwayat extends Fragment {
//inisialisasi kontroller
private DBDataSource dataSource;

//inisialisasi arraylist
private ArrayList<Investasi_DB> values;

static ListView lv;
private Button button_hapus;
static LinearLayout mLinear;

@Nullable
@Override

public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {

    mLinear = (LinearLayout) inflater.inflate(R.layout.viewdata, container, false);
    lv = (ListView) mLinear.findViewById(R.id.list);
    dataSource = new DBDataSource(getActivity());
    // buka kontroller
    dataSource.open();

    // ambil semua data barang
    values = dataSource.getAllInvestasi_DB();

    // masukkan data barang ke array adapter
    ArrayAdapter<Investasi_DB> adapter = new ArrayAdapter<Investasi_DB>(getActivity(),
            android.R.layout.simple_list_item_1, values);

    // set adapter pada list
    lv.setAdapter(adapter);
    //lv.setOnItemLongClickListener(this);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            final Dialog dialog = new Dialog(getActivity());
            dialog.setContentView(R.layout.dialog_view);
            dialog.setTitle("Pilihan");
            dialog.show();

            final Investasi_DB b = (Investasi_DB) lv.getItemAtPosition(position);
            button_hapus = (Button) dialog.findViewById(R.id.button_hapus);
            button_hapus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dataSource.deleteInvestasi_DB(b.getId());
                    dialog.dismiss();
                    getActivity().finish();
                }
            });
        }
    });
    return mLinear;
}

它的工作,但点击按钮后我的应用程序自动关闭(我认为是因为getActivity()。finish();),对此有何想法?

1 个答案:

答案 0 :(得分:2)

  

我需要帮助来解决错误(正确的实现方式):   getListAdapter(),getIntent(),以及这个   lv.setOnItemClickListener(本);

如果this想要访问Context,则可以使用getActivity()Fragment不是Context

setOnItemClickListener(this);中,this指的是当前实例。使用该行,您意味着this也在实现OnItemClickListener接口。哪个不是真的。您需要将implement OnItemClickListener添加到Fragment课程。

getListAdapter():

而不是getListAdapter()您可以使用ListView来检索位置上的项目,因为您已经将引用保留为成员。变化

final Investasi_DB b = (Investasi_DB) getListAdapter().getItem(pos);

 final Investasi_DB b = (Investasi_DB) lv.getItemAtPosition(pos);

为什么需要这一行startActivity(getIntent());