I am currently trying to implement a custom listView for my Fragment, but when i try to add the item to the ListView it doesn't show up, and i get no exception or anything.
What i'm trying to do is to create an object with a name and a number by typing, both of these things work, but it seems like i did a mistake with the listView
Here is the Fragment with the ListView
package com.example.myapplicationteeeeeeeeeest.ui.BShoppingRecipe;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import com.example.myapplicationteeeeeeeeeest.R;
import java.util.ArrayList;
import java.util.List;
public class ShoppingListTab extends ShoppingRecipe {
private EditText inputIngredient;
private EditText inputQuantity;
private Button addButton;
private ListView listView;
private ShoppingListAdapter adapterListView;
private ArrayList<ShoppingList> ShoppingList;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate( R.layout.tab_fragment_one, container,false );
listView = (ListView) view.findViewById( R.id.shoppingList_listView );
addButton = (Button) view.findViewById( R.id.shoppingTab_button);
inputQuantity = (EditText) view.findViewById( R.id.quantity_editText );
inputIngredient = (EditText) view.findViewById( R.id.ingredient_editText );
ShoppingList = new ArrayList<>();
adapterListView = new ShoppingListAdapter( getActivity(),ShoppingList );
listView.setAdapter( adapterListView );
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
ShoppingList.remove(position);
adapterListView.notifyDataSetChanged();
return true;
}
});
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String ingredient = inputIngredient.getText().toString().trim();
String quantitiy = inputQuantity.getText().toString().trim();
if (!ingredient.isEmpty() && !quantitiy.isEmpty()) {
ShoppingList ShoppingListItem = new ShoppingList(ingredient, quantitiy);
ShoppingList.add(ShoppingListItem);
adapterListView.notifyDataSetChanged();
inputIngredient.setText("");
inputQuantity.setText("");
}
}
});
return view;
}
}
The Fragment Layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/ingredient_editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="80dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:ems="10"
android:hint="#string/shopping_list_hint_artikel"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/quantity_editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:ems="10"
android:hint="#string/shopping_list_hint_stückzahl"
android:inputType="number"
app:layout_constraintEnd_toStartOf="#+id/shoppingTab_button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/ingredient_editText"
app:layout_constraintHorizontal_bias="0.0"/>
<Button
android:id="#+id/shoppingTab_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="#string/shopping_list_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/ingredient_editText" />
<ListView
android:id="#+id/shoppingList_listView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/quantity_editText"/>
</androidx.constraintlayout.widget.ConstraintLayout>
The Costume ListAdapter
package com.example.myapplicationteeeeeeeeeest.ui.BShoppingRecipe;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.example.myapplicationteeeeeeeeeest.R;
import java.util.ArrayList;
public class ShoppingListAdapter extends ArrayAdapter<ShoppingList> {
private ArrayList<ShoppingList> shoppingList;
private Context context;
public ShoppingListAdapter(Context context, ArrayList<ShoppingList> taskList) {
super(context, R.layout.shopping_list_item);
this.shoppingList = taskList;
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null){
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = layoutInflater.inflate(R.layout.shopping_list_item, null);
}
ShoppingList taskItem = shoppingList.get(position);
if(taskItem != null){
TextView taskItemText = v.findViewById(R.id.shoppingList_Ingredient);
TextView taskItemDate = v.findViewById(R.id.shoppingList_Quantity);
taskItemText.setText(taskItem.getTask());
taskItemDate.setText(taskItem.getNumber());
}
return v;
}
}
And the Layout for the Item
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<TextView
android:text="TextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/shoppingList_Ingredient"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="#+id/shoppingList_Quantity"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"/>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/shoppingList_Quantity"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
Try adding this method in your adapter class:
#Override
public int getCount() {
return shoppingList.size();
}
And also try not to use same name for your list variable and model class.
Hope this helps!
Related
I fetch data from database and put it to RecyclerView. My database arrives 4 items I see it with log debug then I send it to my adapter. However I see only first item on my screen.
my adapter ListArticleAdapter
package uz.sherdevs.alishernavoiy.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
import uz.sherdevs.alishernavoiy.R;
import uz.sherdevs.alishernavoiy.model.Article;
public class ListArticleAdapter extends RecyclerView.Adapter<ListArticleAdapter.MyViewHolder> {
private Context context;
private List<Article> articleList;
public ListArticleAdapter(Context context, List<Article> articleList) {
this.context = context;
this.articleList = articleList;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.item_list, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
holder.tv_title.setText(articleList.get(position).getTitle());
}
#Override
public int getItemCount() {
// Log.d("list", "" + articleList.size());
return articleList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv_title;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
tv_title = itemView.findViewById(R.id.article_title);
}
}
}
MainActivity
package uz.sherdevs.alishernavoiy;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.util.Log;
import java.util.List;
import uz.sherdevs.alishernavoiy.adapter.ListArticleAdapter;
import uz.sherdevs.alishernavoiy.database.SQLiteDBHelper;
import uz.sherdevs.alishernavoiy.model.Article;
public class MainActivity extends AppCompatActivity {
private RecyclerView articleRecyclerView;
private ListArticleAdapter adapter;
private List<Article> articleList;
private SQLiteDBHelper dbHelper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
articleRecyclerView = (RecyclerView)findViewById(R.id.articles_recyclerview);
dbHelper = new SQLiteDBHelper(this);
articleList = dbHelper.getArticles();
// for (Article article : articleList) {
// Log.d("list", article.getTitle());
// }
articleRecyclerView.setHasFixedSize(true);
adapter = new ListArticleAdapter(this, articleList);
// Log.d("list", articleList.toString());
articleRecyclerView.setAdapter(adapter);
articleRecyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#drawable/background_icon"
>
<ImageView
android:src="#drawable/background_regtangle_green"
android:scaleType="centerCrop"
android:id="#+id/circle_background"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHeight_percent="0.3"
app:layout_constraintWidth_percent="1"
app:layout_constraintVertical_bias="0"
/>
<TextView
android:text="Dostonlar"
android:textColor="#FFDF6A"
android:textSize="60sp"
android:textStyle="bold"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="#id/circle_background"
app:layout_constraintLeft_toLeftOf="#id/circle_background"
app:layout_constraintRight_toRightOf="#id/circle_background"
app:layout_constraintBottom_toBottomOf="#id/circle_background"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintVertical_bias="0.3"
android:fontFamily="#font/myfont"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/articles_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="?attr/actionBarSize">
</androidx.recyclerview.widget.RecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>
item_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="12dp">
<androidx.cardview.widget.CardView
android:id="#+id/cardview1"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHeight_percent="0.15"
app:layout_constraintWidth_percent="0.8"
app:cardCornerRadius="20dp"
app:layout_constraintVertical_bias="0.23"
app:cardElevation="20dp"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/article_title"
android:textSize="45sp"
android:textStyle="bold"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHeight_percent="0.6"
android:gravity="center"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
I got 4 Objects from my database with command
articleList = dbHelper.getArticles();
However I see only first one on the screen.
Moreover as you see I loged in my adapter
#Override
public int getItemCount() {
// Log.d("list", "" + articleList.size());
return articleList.size();
}
That size also shows 4.
In item_list.xml, change android:layout_height="match_parent" with android:layout_height="wrap_content"
I have created a listview that has many info in one list. I tired to implement the search view in the listview adapter but the list do not get filtered.
Below is my source code for that Activity.
DictionaryAdapter.java
import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.CursorAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.personaldictionary.R;
import com.example.personaldictionary.database.DictionaryContract;
public class DictionaryAdapter extends CursorAdapter {
private static final String LOG_TAG = "DictionaryAdapter";
public DictionaryAdapter(Context context, Cursor c) {
super(context, c);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Log.d(LOG_TAG,"newView called cursor: "+cursor+" context: "+context+" parent: "+parent);
return LayoutInflater.from(context).inflate(R.layout.word_list, parent, false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
Log.d(LOG_TAG,"bindView called cursor: "+cursor+" context: "+context+" view: "+view);
view.findViewById(R.id.container).setAnimation(AnimationUtils.loadAnimation(context,R.anim.fade_scale_animation));
TextView wordTextView = (TextView) view.findViewById(R.id.word_text_list);
TextView wordDefTextView = (TextView) view.findViewById(R.id.word_definition_list);
TextView wordEtyloTextView = (TextView) view.findViewById(R.id.word_etymologies_list);
int wordColumnIndex = cursor.getColumnIndex(DictionaryContract.DictionaryEntry.COLUMN_WORD);
int defColumnIndex = cursor.getColumnIndex(DictionaryContract.DictionaryEntry.COLUMN_WORD_DEFINITION);
int etyloColumnIndex = cursor.getColumnIndex(DictionaryContract.DictionaryEntry.COLUMN_WORD_ETYMOLOGIES);
String word = cursor.getString(wordColumnIndex);
String wordDef = cursor.getString(defColumnIndex);
String wordEtylo = cursor.getString(etyloColumnIndex);
wordTextView.setText(word);
wordDefTextView.setText(wordDef);
wordEtyloTextView.setText(wordEtylo);
}
}
WordListActivity.java
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.MenuItemCompat;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.CursorLoader;
import androidx.loader.content.Loader;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import androidx.appcompat.widget.SearchView;
import android.widget.Toast;
import com.example.personaldictionary.adapter.DictionaryAdapter;
import com.example.personaldictionary.database.DictionaryContract;
import com.example.personaldictionary.database.DictionaryDbHelper;
public class WordListActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {
private static final String LOG_TAG = "WordListActivity";
DictionaryAdapter mAdapter;
DictionaryDbHelper mDbHelper;
private static final int WORD_LOADER = 0;
private String wordString;
private String wordDefString;
private String wordEtyloString;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_word_list);
ListView wordListView = (ListView) findViewById(R.id.word_list);
View emptyView = findViewById(R.id.empty_view);
wordListView.setEmptyView(emptyView);
Log.d(LOG_TAG,"productListView.setEmptyView(emptyView)");
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if(extras!=null){
wordString = intent.getStringExtra("word");
wordDefString = intent.getStringExtra("wordDefinition");
wordEtyloString = intent.getStringExtra("wordEtylo");
Log.d("WordListActivity","wordString: "+wordString+" wordDefString: "+wordDefString+" wordEtyloString: "+wordEtyloString);
insertWord(wordString,wordDefString,wordEtyloString);
}
mAdapter = new DictionaryAdapter(this,null);
wordListView.setAdapter(mAdapter);
getSupportLoaderManager().initLoader(WORD_LOADER, null,this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.search,menu);
MenuItem search_item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) search_item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
WordListActivity.this.mAdapter.getFilter().filter(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
public void insertWord(String word, String word_def, String word_etylo){
Uri newUri = null;
Log.d(LOG_TAG,"insertWord() word: "+word+" word_def: "+word_def+" word_et: "+word_etylo);
ContentValues values = new ContentValues();
values.put(DictionaryContract.DictionaryEntry.COLUMN_WORD,word);
values.put(DictionaryContract.DictionaryEntry.COLUMN_WORD_DEFINITION,word_def);
values.put(DictionaryContract.DictionaryEntry.COLUMN_WORD_ETYMOLOGIES,word_etylo);
Log.d(LOG_TAG,"DictionaryContract.DictionaryEntry.CONTENT_URI: "+DictionaryContract.DictionaryEntry.CONTENT_URI);
Log.d(LOG_TAG,"values: "+values);
newUri = getContentResolver().insert(DictionaryContract.DictionaryEntry.CONTENT_URI, values);
//mAdapter.swapCursor(getAllItems());
//mAdapter.swapCursor(getAllItems());
if (newUri == null) {
Toast.makeText(WordListActivity.this, "Word insert error ", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(WordListActivity.this, "Word insert Success", Toast.LENGTH_SHORT).show();
}
}
private Cursor getAllItems(){
Log.d(LOG_TAG,"getAllItems inside");
String[] projection = {DictionaryContract.DictionaryEntry._ID,
DictionaryContract.DictionaryEntry.COLUMN_WORD,
DictionaryContract.DictionaryEntry.COLUMN_WORD_DEFINITION,
DictionaryContract.DictionaryEntry.COLUMN_WORD_ETYMOLOGIES};
Cursor cursor = getContentResolver().query(DictionaryContract.DictionaryEntry.CONTENT_URI,projection,null,null,null);
Log.d(LOG_TAG,"getAllItems: "+cursor);
return cursor;
}
#NonNull
#Override
public Loader<Cursor> onCreateLoader(int id, #Nullable Bundle args) {
Log.d(LOG_TAG,"onCreateLoader called");
String[] projection = {DictionaryContract.DictionaryEntry._ID,
DictionaryContract.DictionaryEntry.COLUMN_WORD,
DictionaryContract.DictionaryEntry.COLUMN_WORD_DEFINITION,
DictionaryContract.DictionaryEntry.COLUMN_WORD_ETYMOLOGIES,};
return new CursorLoader(this, DictionaryContract.DictionaryEntry.CONTENT_URI,projection,null,null,null);
}
#Override
public void onLoadFinished(#NonNull Loader<Cursor> loader, Cursor data) {
Log.d(LOG_TAG,"onLoadFinished called data: "+data);
mAdapter.swapCursor(data);
}
#Override
public void onLoaderReset(#NonNull Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
}
activity_word_list.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".WordListActivity"
android:background="#color/white"
android:id="#+id/root_layout">
<ListView
android:id="#+id/word_list"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/search_input" />
<RelativeLayout
android:id="#+id/empty_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/empty_title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:fontFamily="sans-serif-medium"
android:paddingTop="16dp"
android:text="#string/empty_view_title_text"
android:textAppearance="?android:textAppearanceMedium"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="#+id/empty_subtitle_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/empty_title_text"
android:layout_centerHorizontal="true"
android:fontFamily="sans-serif"
android:paddingTop="8dp"
android:text="#string/empty_view_subtitle_text"
android:textAppearance="?android:textAppearanceSmall"
android:textColor="#A2AAB0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.528"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.548" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
word_list.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="#fff">
<RelativeLayout
android:id="#+id/container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:background="#drawable/card_bg"
android:paddingBottom="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<TextView
android:id="#+id/word_text_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:textColor="#color/title_text_color"
android:textStyle="bold" />
<TextView
android:id="#+id/word_definition_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/word_text_list"
android:layout_alignStart="#+id/word_text_list"
android:layout_alignLeft="#id/word_text_list"
android:layout_marginTop="8dp"
android:layout_marginRight="16dp"
android:lineSpacingExtra="8dp"
android:textColor="#color/content_text_color" />
<TextView
android:id="#+id/word_etymologies_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/word_definition_list"
android:layout_alignStart="#+id/word_definition_list"
android:layout_alignLeft="#id/word_definition_list"
android:layout_marginTop="8dp"
android:layout_marginRight="16dp"
android:lineSpacingExtra="8dp"
android:textColor="#color/content_text_color" />
</RelativeLayout>
<ImageView
android:id="#+id/word_audio"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:src="#drawable/ic_audio"
app:layout_constraintEnd_toStartOf="#+id/container"
app:layout_constraintStart_toStartOf="#+id/container"
app:layout_constraintTop_toTopOf="#+id/container"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
search.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_search_gray_24dp"
android:title="Search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always"/>
</menu>
This is how the view is. I want to filter the text in blue through searchview. Could anyone suggest how to do it?
You should filter list and call notifyDataSetChanged in adapter.
Example you have list ListA which contains ListA = {A,B,C}
You filter listA ( you search for item B ) FilteredList = {B}
AdapterSource = FilteredList
adapter.notifyDataSetChanged()
If you clear search view
AdapterSource = ListA
This is java class MainActivity.java
package com.example.mhn.intercoapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import com.example.mhn.intercoapp.Adapters.EmployeeDirAdapter;
import com.example.mhn.intercoapp.static_class.EmployeeDir;
import java.util.ArrayList;
public class EmployeeDirectoryActivity extends AppCompatActivity {
ListView listView;
ImageView back_button;
EmployeeDir obj;
ArrayList <EmployeeDir> empDir ;
EmployeeDirAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_employee_directory);
listView = (ListView) findViewById(R.id.listView_emp_directory_xml);
back_button = (ImageView) findViewById(R.id.back_button_header_emp_directory_activity_xml);
obj = new EmployeeDir();
empDir = new ArrayList<>();
adapter = new EmployeeDirAdapter(getApplicationContext(),empDir);
back_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
finish();
}
});
obj.setEmp_name("Hafiz Sadique Umar");
obj.setEmp_email("hsu#gmail.com");
obj.setEmp_contact_num("+923045607057");
empDir.add(obj);
empDir.add(obj);
empDir.add(obj);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
String value = (String)adapter.getItemAtPosition(position);
Intent intent= new Intent(getApplicationContext(),DoneActivity.class);
startActivity(intent);
// assuming string and if you want to get the value on click of list item
// do what you intend to do on click of listview row
}
});
}
}
This is activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".EmployeeDirectoryActivity">
<RelativeLayout
android:id="#+id/relatice_layout_header_emp_directory_activity_xml"
android:background="#color/colorAccent"
android:layout_width="match_parent"
android:layout_alignParentTop="true"
android:layout_height="56dp">
<ImageView
android:id="#+id/back_button_header_emp_directory_activity_xml"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:src="#drawable/back_arrow_header"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Employee Directory"
android:textColor="#fff"
android:textSize="18dp"
android:textStyle="bold"
android:layout_centerInParent="true"
/>
</RelativeLayout>
<ListView
android:clickable="true"
android:id="#+id/listView_emp_directory_xml"
android:layout_below="#id/relatice_layout_header_emp_directory_activity_xml"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
This is Adapter
package com.example.mhn.intercoapp.Adapters;
import android.content.Context;
import android.content.Intent;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.mhn.intercoapp.MainActivity;
import com.example.mhn.intercoapp.R;
import com.example.mhn.intercoapp.static_class.EmployeeDir;
import java.util.ArrayList;
public class EmployeeDirAdapter extends BaseAdapter {
Context context;
private LayoutInflater inflater;
private ArrayList<EmployeeDir> menuData=new ArrayList<>();
public EmployeeDirAdapter(Context context, ArrayList<EmployeeDir>EmpDir)
{
this.context = context;
this.menuData=EmpDir;
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return menuData.size();
}
#Override
public Object getItem(int i) {
return menuData.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderEmpDir holder;
if (convertView == null) {
holder = new ViewHolderEmpDir();
convertView = inflater.inflate(R.layout.emp_directory_list_row_layout, parent, false);
holder.name = (TextView) convertView.findViewById(R.id.name_textView_emp_directory_list_row_layout);
holder.email = (TextView) convertView.findViewById(R.id.email_textView_emp_directory_list_row_layout);
holder.phone = (TextView) convertView.findViewById(R.id.phone);
holder.pic = (ImageView) convertView.findViewById(R.id.emp_pic_emp_directory_list_row_layout);
holder.viewButton = (ImageView) convertView.findViewById(R.id.view_button_emp_dir_list_row_layout);
convertView.setTag(holder);
}
else
{
holder = (ViewHolderEmpDir) convertView.getTag();
}
holder.name.setText(menuData.get(position).getEmp_name());
holder.email.setText(menuData.get(position).getEmp_email());
holder.phone.setText(menuData.get(position).getEmp_contact_num());
//Linkify.addLinks(holder.email,Linkify.EMAIL_ADDRESSES);
//Linkify.addLinks(holder.phone,Linkify.PHONE_NUMBERS);
holder.pic.setImageResource(R.drawable.user_sign_up);
holder.viewButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context,MainActivity.class);
}
});
//downloadImage(position,holder);
return convertView;
}
// private void downloadImage(int position,ViewHolderShowAllBooking holder)
// {
// Picasso.with(context)
// .load("http://www.efefoundation.net/inklink/uploads/artist/"+menuData.get(position).getArtistId()+".jpg")
// .fit() // will explain later
// .into(holder.profile);
// }
static class ViewHolderEmpDir
{
TextView name;
TextView email;
TextView phone;
ImageView pic;
ImageView viewButton;
}
}
This is list_row_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:weightSum="4"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_marginTop="5dp"
android:layout_centerHorizontal="true"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="70dp"
android:background="#color/colorAccent">
<ImageView
android:id="#+id/emp_pic_emp_directory_list_row_layout"
android:layout_marginTop="5dp"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_marginBottom="5dp"
android:src="#drawable/user_sign_up"/>
</RelativeLayout>
<RelativeLayout
android:background="#color/colorAccent"
android:layout_marginTop="5dp"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="3">
<TextView
android:id="#+id/name_textView_emp_directory_list_row_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textColor="#fff"
android:textSize="18dp"
android:layout_alignParentLeft="true"
android:text="Husnain"/>
<TextView
android:id="#+id/phone"
android:layout_toRightOf="#id/name_textView_emp_directory_list_row_layout"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:autoLink="phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="***********"
android:textColor="#fff"/>
<TextView
android:id="#+id/email_textView_emp_directory_list_row_layout"
android:layout_alignParentLeft="true"
android:layout_below="#id/name_textView_emp_directory_list_row_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="mhn786#gmail.com"
android:autoLink="email"
android:layout_marginLeft="10dp"
android:textSize="18dp"
android:textColor="#fff"
/>
<ImageView
android:id="#+id/view_button_emp_dir_list_row_layout"
android:src="#drawable/view"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</LinearLayout>
When I apply OnItemClickListener it does not do any action like intent inside the func not called. I tried toast too but nothing shown on clicking the listView Items.
I also tried OnItemSlectedListener , it also not worked for me.
What is the problem here with this code. ?
Thanks Every One. Issue was with autolink on email and phone, I make their focusable=false but nothing happened.Then I remove it and guess what? Clicklistener also starts working.
The attribute android:clickable="true" in your ListView could be overriding all of click events of its child views, you should probably remove it:
<ListView
android:clickable="true" <!-- Remove this attribute -->
android:id="#+id/listView_emp_directory_xml"
android:layout_below="#id/relatice_layout_header_emp_directory_activity_xml"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Friends, I am a new developer in the field of android and I want to develop an application like that it will sort the Gridview by using a spinner, I had applied the XML array for Spinner and Custom adapter also I tried various solution but it didn't work so please help me
Moodel Class for adapter of GridAdapter
package braniacs.com.gridupdaterapp;
/**
* Created by grimReaper on 12/4/2017.
*/
public class GridModel {
String thingName;
public String getThingName() {
return thingName;
}
public void setThingName(String thingName) {
this.thingName = thingName;
}
}
MainActvityCode along adapter in it!
package braniacs.com.gridupdaterapp;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
GridView mGridViewA;
Spinner mspinnerColony;
ArrayList<GridModel> ColonyArray;
String[] ColonyArraySpinnerWali = {"hie", "wasteage", "short", "Lovely", "RUSSIA", "ASIA", "KOREA"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGridViewA = findViewById(R.id.mgridViewApp);
mspinnerColony = findViewById(R.id.spnr_colony);
ColonyArray = new ArrayList<>();
ArrayAdapter mArrayAdapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, ColonyArraySpinnerWali);
mspinnerColony.setAdapter(mArrayAdapter);
mspinnerColony.setSelection(0);
mspinnerColony.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (i != 0) {
GridModel mgRIDmODEL = new GridModel();
TextView mTitle = (TextView) view;
mgRIDmODEL.setThingName(mTitle.getText().toString());
ColonyArray.add(mgRIDmODEL);
GridVuCustionAdapter adapter = new GridVuCustionAdapter(MainActivity.this,
R.layout.custom_gridvu_design, ColonyArray);
mGridViewA.setAdapter(adapter);
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
mGridViewA.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// CLICK LISTENER AREA
}
});
}
public static class GridVuCustionAdapter extends ArrayAdapter<GridModel> {
LayoutInflater mLayoutInflater;
ArrayList<GridModel> objects;
Context context;
int resource;
public GridVuCustionAdapter(#NonNull Context context, int resource, #NonNull ArrayList<GridModel> objects) {
super(context, resource, objects);
mLayoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
this.objects = objects;
this.context = context;
this.resource = resource;
}
#Override
public int getCount() {
return objects.size();
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
convertView = mLayoutInflater.inflate(resource, parent, false);
TextView txtVuTitle = convertView.findViewById(R.id.txt_vu_name);
ImageView imgVuCancel = convertView.findViewById(R.id.img_vu_cancel);
txtVuTitle.setText(objects.get(position).getThingName());
return convertView;
}
}
}
the xml file for custom design for the design is simple here
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="#+id/txt_vu_name"
android:layout_weight="1.7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
android:text="TextView" />
<ImageView
android:layout_weight="0.2"
android:id="#+id/img_vu_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/btn_dialog" />
</LinearLayout>
the xml file is here for the activity
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="braniacs.com.gridupdaterapp.MainActivity">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mgridViewApp"
android:layout_width="0dp"
android:layout_height="200dp"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498" />
<Spinner
android:id="#+id/spnr_colony"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="45dp" />
</android.support.constraint.ConstraintLayout>
I am having some trouble with my custom adapter, the adapter is meant to print out |TextView|Button|EditText|Button|, It was working before I added in the button listeners, but when I added in the listeners the textView showed up blank.
I am also wondering if it is possible to keep the editText separate from row to row? As each row has buttons to increment or decrement their own running total, but when I press a button the total is carried to every row
Can anyone help me with these problems?
This is the adapter class I am using
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.pinchtapzoom.R;
public class CustomCursorAdapter extends CursorAdapter{
public int counter = 0;
public CustomCursorAdapter(Context context, Cursor c) {
super(context, c);
this.context = context;
}
Context context;
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// when the view will be created for first time,
// we need to tell the adapters, how each item will look
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.row, parent, false);
return retView;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
// here we are setting our data
// that means, take the data from the cursor and put it in views
TextView textViewPersonName = (TextView) view.findViewById(R.id.item1);
textViewPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));
}
public View getView(final int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
convertView = (RelativeLayout)inflater.inflate(R.layout.row, null);
}
final EditText edit1 = (EditText)convertView.findViewById(R.id.runningTotal);
Button plusButton = (Button) convertView.findViewById(R.id.plusButton);
plusButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
counter++;
edit1.setText(Integer.toString(counter));
}
});
//final EditText edit1 = (EditText)convertView.findViewById(R.id.runningTotal);
Button minusButton = (Button) convertView.findViewById(R.id.minusButton);
minusButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
counter--;
edit1.setText(Integer.toString(counter));
}
});
return convertView;
}
}
This is my XML code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:id="#+id/item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:paddingBottom="5dp"/>
<Button
android:id="#+id/plusButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/plusButton"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:id="#+id/runningTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/minusButton"
android:layout_toStartOf="#+id/minusButton"
android:layout_marginRight="30dp" />
<Button
android:id="#+id/minusButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/minusButton"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
There are some issues with layout xml, TextView position was specified,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:id="#+id/item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="item"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="30sp"
android:paddingBottom="5dp"/>
<Button
android:id="#+id/plusButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="plus"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/item1"
android:layout_toEndOf="#+id/item1"
android:layout_centerHorizontal="true" />
<EditText
android:id="#+id/runningTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/plusButton"
android:layout_toEndOf="#+id/plusButton"
android:layout_toLeftOf="#+id/minusButton"
android:layout_toStartOf="#+id/minusButton"
android:layout_marginRight="30dp" />
<Button
android:id="#+id/minusButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="minus"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
It is better though to use LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="#+id/item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="item"
android:textSize="30sp" />
<Button
android:id="#+id/plusButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:text="plus" />
<EditText
android:id="#+id/runningTotal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_weight="1" />
<Button
android:id="#+id/minusButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:text="minus" />
</LinearLayout>
Update:
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.pinchtapzoom.R;
public class CustomCursorAdapter extends CursorAdapter {
Context context;
public int counter = 0;
public CustomCursorAdapter(Context context, Cursor c) {
super(context, c);
this.context = context;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// when the view will be created for first time,
// we need to tell the adapters, how each item will look
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.row, parent, false);
return retView;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
// here we are setting our data
// that means, take the data from the cursor and put it in views
TextView textViewPersonName = (TextView) view.findViewById(R.id.item1);
textViewPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));
final EditText edit1 = (EditText) view.findViewById(R.id.runningTotal);
Button plusButton = (Button) view.findViewById(R.id.plusButton);
plusButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
counter++;
edit1.setText(Integer.toString(counter));
}
});
Button minusButton = (Button) view.findViewById(R.id.minusButton);
minusButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
counter--;
edit1.setText(Integer.toString(counter));
}
});
}
}