vendredi 30 janvier 2015

ExpendableListView send Bundle OnChildClickListener

I try to send bundle on My ExpendableListView using OnChildClickListener. I try to use it like I use setOnItemClickListener. But it always crashed. Can any one lend me a help? Because I am new on Android Developer.


this is may main class



package com.skripsi.olfi.kamusbahasasumbawa;

import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ExpandableListView;
import android.widget.SearchView;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends ActionBarActivity implements
SearchView.OnQueryTextListener, SearchView.OnCloseListener{

private SearchView search;
private MyListAdapter listAdapter;
private ExpandableListView myList;
private ArrayList<Kelompok> kelompokList = new ArrayList<Kelompok>();
private ArrayList<Kata> kataSumList, kataIndList;
private ArrayAdapter<Kata> adapter;
private DatabaseManager dbManager;

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

dbManager = DatabaseManager.getInstance(this);

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
search = (SearchView) findViewById(R.id.search);
search.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
search.setIconifiedByDefault(false);
search.setOnQueryTextListener(this);
search.setOnCloseListener(this);
// get reference to the ExpandableListView
myList = (ExpandableListView) findViewById(R.id.expandableList);



// display the list
displayList();
// expand all Groups
expandAll();


}




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

// method to expand all groups
private void expandAll() {
int count = listAdapter.getGroupCount();
for (int i = 0; i < count; i++) {
myList.expandGroup(i);
}
}

// method to expand all groups
private void displayList() {

// display the list
loadSomeData();


// create the adapter by passing your ArrayList data
listAdapter = new MyListAdapter(MainActivity.this, kelompokList);
// attach the adapter to the list
myList.setAdapter(listAdapter);



myList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Bundle b = new Bundle();
b.putString("id", String.valueOf(adapter.getItem(groupPosition).getId()));
b.putString("kata", adapter.getItem(groupPosition).getKata());
b.putString("arti", adapter.getItem(groupPosition).getArti());
b.putString("kalimat", adapter.getItem(groupPosition).getKalimat());
Intent intent = new Intent(MainActivity.this, LihatTerjemahan.class);
intent.putExtras(b);
startActivity(intent);
return false;
}
});


}

private void loadSomeData() {

kataSumList = dbManager.getAllSum();
kataIndList = dbManager.getAllInd();

Kelompok kelompok = new Kelompok("Bahasa Sumbawa", kataSumList);
kelompokList.add(kelompok);
kelompok = new Kelompok("Bahasa Indonesia", kataIndList);
kelompokList.add(kelompok);

}

@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 boolean onClose() {
listAdapter.filterData("");
expandAll();
return false;
}

@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
listAdapter.filterData(query);
expandAll();
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
listAdapter.filterData(newText);
expandAll();
return false;
}


}


this is my database class



package com.skripsi.olfi.kamusbahasasumbawa;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

import java.util.ArrayList;

/**
* Created by Olfi on 30/01/2015.
*/
public class DatabaseManager extends SQLiteAssetHelper {

private static final String DB_NAME = "db_kamus_android";
private static final int DB_VER = 1;



private static DatabaseManager dbInstance;
private static SQLiteDatabase db;

DatabaseManager(Context context){
super(context, DB_NAME, null, DB_VER);
}

public static DatabaseManager getInstance(Context context){
if (dbInstance ==null){
dbInstance = new DatabaseManager(context);
db = dbInstance.getWritableDatabase();
}
return dbInstance;
}

@Override
public synchronized void close(){
super.close();
if (dbInstance!=null){
dbInstance.close();
}
}

public ArrayList<Kata> getAllSum(){
ArrayList<Kata> listSumbawa = new ArrayList<Kata>();
Cursor cursor = db.rawQuery("SELECT id, sumbawa as kata , arti_indonesia as arti, kalimat, favorit FROM kamus", null);
if (cursor!=null) {
if (cursor.moveToFirst()) do {
Kata kamus = new Kata();
kamus.setId(Integer.parseInt(cursor.getString(0)));
kamus.setKata(cursor.getString(1));
kamus.setArti(cursor.getString(2));
kamus.setKalimat(cursor.getString(3));
kamus.setFav(Integer.parseInt(cursor.getString(4)));
listSumbawa.add(kamus);
} while (cursor.moveToNext());
}
return listSumbawa;
}

public ArrayList<Kata> getAllInd(){
ArrayList<Kata> listIndonesia = new ArrayList<Kata>();
Cursor cursor = db.rawQuery("SELECT id, indonesia as kata , arti_sumbawa as arti, kalimat, favorit_indonesia FROM kamus", null);
if (cursor!=null) {
if (cursor.moveToFirst()) do {
Kata kamus = new Kata();
kamus.setId(Integer.parseInt(cursor.getString(0)));
kamus.setKata(cursor.getString(1));
kamus.setArti(cursor.getString(2));
kamus.setKalimat(cursor.getString(3));
kamus.setFav(Integer.parseInt(cursor.getString(4)));
listIndonesia.add(kamus);
} while (cursor.moveToNext());
}
return listIndonesia;
}


}


this is my ListAdapterClass:



package com.skripsi.olfi.kamusbahasasumbawa;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Locale;

public class MyListAdapter extends BaseExpandableListAdapter {

private Context context;
private ArrayList<Kelompok> kelompokList;
private ArrayList<Kelompok> originalList;

public MyListAdapter(Context context, ArrayList<Kelompok> continentList) {
this.context = context;
this.kelompokList = new ArrayList<Kelompok>();
this.kelompokList.addAll(continentList);
this.originalList = new ArrayList<Kelompok>();
this.originalList.addAll(continentList);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
ArrayList<Kata> kataList = kelompokList.get(groupPosition).getListKata();
return kataList.get(childPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}

@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

Kata kata = (Kata) getChild(groupPosition, childPosition);
if(convertView == null)
{
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.child_row, null);
}

TextView txtkata = (TextView) convertView.findViewById(R.id.kata);
TextView arti = (TextView) convertView.findViewById(R.id.arti);
TextView kalimat = (TextView) convertView.findViewById(R.id.kalimat);
txtkata.setText(kata.getKata().trim());
arti.setText(kata.getArti().trim());
kalimat.setText((kata.getKalimat().trim()));

return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
ArrayList<Kata> kataList = kelompokList.get(groupPosition).getListKata();
return kataList.size();
}

@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return kelompokList.get(groupPosition);
}

@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return kelompokList.size();
}

@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Kelompok kelompok = (Kelompok) getGroup(groupPosition);
if(convertView == null)
{
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.group_row, null);
}

TextView heading = (TextView) convertView.findViewById(R.id.heading);
heading.setText(kelompok.getKlp().trim());

return convertView;
}

@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}

public void filterData(String query)
{
query = query.toLowerCase();
Log.v("MyListAdapter", String.valueOf(kelompokList.size()));
kelompokList.clear();

if(query.isEmpty())
{
kelompokList.addAll(originalList);
} else {
for(Kelompok kelompok: originalList)
{
ArrayList<Kata> kataList = kelompok.getListKata();
ArrayList<Kata> newList = new ArrayList<Kata>();
for(Kata kata: kataList)
{
if(kata.getKata().toLowerCase().contains(query) || kata.getArti().toLowerCase().contains(query) || kata.getKalimat().toLowerCase().contains(query))
{
newList.add(kata);
}
}
if(newList.size() > 0)
{
Kelompok nKelompok = new Kelompok(kelompok.getKlp(), newList);
kelompokList.add(nKelompok);
}
}
}

Log.v("MyListAdapter", String.valueOf(kelompokList.size()));
notifyDataSetChanged();
}
}


this is my destination class:



package com.skripsi.olfi.kamusbahasasumbawa;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class LihatTerjemahan extends ActionBarActivity {
private TextView teksKata, teksArti, teksKalimat;
private DatabaseManager dbManager;
int id_kata,is_favorit;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lihat_terjemahan);
teksKata = (TextView) findViewById(R.id.teksKata);
teksArti = (TextView) findViewById(R.id.teksArti);
teksKalimat = (TextView) findViewById(R.id.teksKalimat);
dbManager = DatabaseManager.getInstance(this);

Bundle b = getIntent().getExtras();
if (b != null){
id_kata = Integer.valueOf(b.getString("id"));
teksKata.setText(b.getString("kata"));
teksArti.setText(b.getString("arti"));
teksKalimat.setText(b.getString("kalimat"));
/*
is_favorit=dbManager.getFavorit(String.valueOf(id_kata));
if(st_favorit==1){
bfavor.setText("Hapus Favorite");
}else{
bfavor.setText("Tambah Favorite");

}
*/

}

}


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

Aucun commentaire:

Enregistrer un commentaire