every one,
I am a beginning learner on android system ,
I create a small app that could get specific data , like foreign exchange rate from bank website ,
this app uses Jsoup , ListView , BaseAdapter etc...
finally , the only problem is the listView has no any display ,
could any one tell me , how to fix it.
Thanks.
Category.java
public class Category
{
String bank_name;
String page_url;
Category(String bank_name,String url)
{
this.bank_name=bank_name;
this.page_url=url;
}
#Override
public String toString()
{
return bank_name;
}
}
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.content.*;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends Activity
{
ArrayList<Category> items;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
items=new ArrayList<Category>();
items.add(new Category("台灣銀行","http://rate.bot.com.tw/Pages/Static/UIP003.zh-TW.htm"));
ArrayAdapter<Category> adapter = new ArrayAdapter<Category>(this , android.R.layout.simple_list_item_1 ,items);
ListView lv = (ListView)findViewById(R.id.lv);
lv.setAdapter(adapter);
lv.setOnItemClickListener(itemClick);
}
OnItemClickListener itemClick = new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> av , View v , int position , long id)
{
Category category = items.get(position);
Intent intent = new Intent();
intent.setClass(MainActivity.this , RateActivity.class );
intent.putExtra("bank_name", category.bank_name);
intent.putExtra("page_url" , category.page_url);
startActivity(intent);
}
};
RateBean.java
public class RateBean
{
private String currency;
private String cashBuy;
private String cashSold;
private String spotBuy;
private String spotSold;
public RateBean()
{
currency = "";
cashBuy = "";
cashSold = "";
spotBuy = "";
spotSold = "";
}
public RateBean(String [] strArr)
{
this.currency=strArr[0];
this.cashBuy=strArr[1];
this.cashSold=strArr[2];
this.spotBuy=strArr[3];
this.spotSold=strArr[4];
}
public RateBean(String currency , String cashBuy , String cashSold , String spotBuy ,String spotSold)
{
setCurrency(currency);
setCashBuy(cashBuy);
setCashSold(cashSold);
setSpotBuy(spotBuy);
setSpotSold(spotSold);
}
//Currency setter and getter
public void setCurrency(String currency)
{
this.currency = currency ;
}
public String getCurrency()
{
return currency;
}
//Cash buy setter and getter
public void setCashBuy(String cashBuy)
{
this.cashBuy = cashBuy;
}
public String getCashBuy()
{
return cashBuy;
}
//Cash sold setter and getter
public void setCashSold(String cashSold)
{
this.cashSold = cashSold;
}
public String getCashSold()
{
return cashSold;
}
//Spot buy setter and getter
public void setSpotBuy(String spotBuy)
{
this.spotBuy = spotBuy;
}
public String getSpotBuy()
{
return spotBuy;
}
//Spot sold setter and getter
public void setSpotSold(String spotSold)
{
this.spotSold = spotSold;
}
public String getSpotSold()
{
return spotSold;
}
}
RateActivity.java
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.util.ArrayList;
public class RateActivity extends ListActivity
{
Context context ;
ArrayList<RateBean> rateBean_item;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rate_show);
TextView updateTime = (TextView)findViewById(R.id.updateTime_textView);
Intent intent = getIntent();
String category = intent.getStringExtra("bank_name");
String page_url = intent.getStringExtra("page_url");
setTitle(category);
context=this;
ConnectThread thread = new ConnectThread(context , rateBean_item , page_url , updateTime);
thread.start();
}
public class ConnectThread extends Thread
{
Context context;
ArrayList<RateBean> rateBean_item;
String page_url ;
TextView updateTime;
RateAdapter adapter;
RateBean rateBean;
int i =0;
public ConnectThread(Context context , ArrayList<RateBean> rateBean_item , String page_url , TextView updateTime )
{
this.context=context;
this.rateBean_item=rateBean_item;
this.page_url=page_url;
this.updateTime=updateTime;
}
#Override
public void run()
{
try
{
String currency="";
String cashBuy="";
String cashSold="";
String spotBuy="";
String spotSold="";
Document doc = Jsoup.connect(page_url).get();
rateBean_item = new ArrayList<>();
String temp=doc.select("td[style=width:326px;text-align:left;vertical-align:top;color:#0000FF;font-size:11pt;font-weight:bold;]").text();
String time=temp.substring(12);
updateTime.setText("匯率更新時間:\n" + time);
for( Element title:doc.select("td.titleLeft"))
{
currency=title.text();
if( i < doc.select("td.decimal").size())
{
cashBuy=doc.select("td.decimal").eq(i++).text();
cashSold=doc.select("td.decimal").eq(i++).text();
spotBuy=doc.select("td.decimal").eq(i++).text();
spotSold=doc.select("td.decimal").eq(i++).text();
rateBean = new RateBean( a , b , c ,d , e );
rateBean_item.add(rateBean);
}
}
adapter = new RateAdapter(context , rateBean_item);
setListAdapter(adapter);
adapter.notifyDataSetChanged();
}
catch( Exception exception )
{
exception.printStackTrace();
}
}
}
}
RateAdapter.java
import java.util.ArrayList;
import android.util.Log;
import android.widget.BaseAdapter;
import android.view.*;
import android.widget.*;
import android.content.*;
public class RateAdapter extends BaseAdapter
{
private LayoutInflater inflater;
private ArrayList<RateBean> list ;
public RateAdapter()
{
}
public RateAdapter(Context context , ArrayList<RateBean> list )
{
this.inflater = LayoutInflater.from(context);
this.list = list;
}
#Override
public int getCount()
{
return list.size();
}
#Override
public Object getItem(int position)
{
return list.get(position);
}
#Override
public long getItemId(int position)
{
return position;
}
public static class ViewHolder
{
TextView currency_name;
TextView cash_buy;
TextView cash_sold;
TextView spot_buy;
TextView spot_sold;
}
#Override
public View getView( int position , View convertView , ViewGroup parent)
{
ViewHolder holder;
if(convertView == null)
{
convertView = inflater.inflate(R.layout.rate_item , null);
holder = new ViewHolder();
holder.currency_name = (TextView)convertView.findViewById(R.id.xml_currency);
holder.cash_buy = (TextView)convertView.findViewById(R.id.xml_cashBuy);
holder.cash_sold = (TextView)convertView.findViewById(R.id.xml_cashSold);
holder.spot_buy = (TextView)convertView.findViewById(R.id.xml_spotBuy);
holder.spot_sold = (TextView)convertView.findViewById(R.id.xml_spotSold);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
RateBean rateItem = list.get(position);
holder.currency_name.setText(rateItem.getCurrency());
holder.cash_buy.setText(rateItem.getCashBuy());
holder.cash_sold.setText(rateItem.getCashSold());
holder.spot_buy.setText(rateItem.getSpotBuy());
holder.spot_sold.setText(rateItem.getSpotSold());
return convertView;
}
}
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/title_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="銀行查詢選擇"/>
<ListView
android:id="#+id/lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
activity_rate_show.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/updateTime_textView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:hint="updateTime"/>
<Button
android:id="#+id/fresh_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onClick"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/button_fresh"/>
</LinearLayout>>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/currency"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/cash_buy"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/cash_sold"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/spot_buy"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/spot_sold"/>
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
rate_item.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/xml_currency"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.25"
android:textAppearance="?android:attr/textAppearanceLarge"
android:hint="text"/>
<TextView
android:id="#+id/xml_cashBuy"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:hint="text"/>
<TextView
android:id="#+id/xml_cashSold"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:hint="text"/>
<TextView
android:id="#+id/xml_spotBuy"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:hint="text"/>
<TextView
android:id="#+id/xml_spotSold"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:hint="text"/>
</LinearLayout>
<ListView
android:id="#+id/lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Your listview needs a height, it cannot be wrap_content. Give it a height like 500dp or match_parent.
Related
My intention is to get data from the run-time growing listview (as shown in below figures) which has Edittext and Spinners and send this data to remote server using json.
Am I on the correct path or there is another kind of method to be used for this.
I want to access Edittext and Spinner from the listview which is expanded at runtime.
My Problem is, whenever a new view is added, it overlaps on the previous view.
Instead, it should get added below the 1st view. (Solved this. See the edited code).
Another thing is how to access edittext & spinner from each view? I want to accept this data from user & save it in the device. How to do that? (in private void SaveUserData() {} method).
Below is my full code.
Edited on 20 Mar 2018
MainActivity.java
package tandel.amit.expandablelistview;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
ListView listView;
Button buttonSave;
FloatingActionButton fab;
Adapter adapter;
int count = 0;
String[] Heading = {"User 1","User 2","User 3","User 4"};
String Name = "Name";
String ID = "ID";
String Gender = "Gender";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.list_view);
buttonSave = findViewById(R.id.btnSave);
fab = findViewById(R.id.fabAdd);
buttonSave.setOnClickListener(onClickListener);
fab.setOnClickListener(onClickListener);
adapter = new Adapter(getApplicationContext(), R.layout.row_layout);
listView.setAdapter(adapter);
AddView(count++);
}
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnSave:
SaveUserData(); // Save the data to Local Database
break;
case R.id.fabAdd:
AddView(count++); // Add new View in listview
break;
default:
break;
}
}
};
private void AddView(int i) {
DataProvider dataProvider = new DataProvider(Heading[i],Name,Gender,ID);
adapter.add(dataProvider);
adapter.notifyDataSetChanged();
}
private void SaveUserData() {
}
}
Adapter.java
package tandel.amit.expandablelistview;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/*
* Created by Amit Tandel on 2/2/2018.
*/
public class Adapter extends ArrayAdapter{
List list = new ArrayList();
public Adapter(#NonNull Context context, int resource) {
super(context, resource);
}
#Override
public void add(#Nullable Object object) {
super.add(object);
list.add(object);
}
#Override
public int getCount() {
return this.list.size();
}
#Nullable
#Override
public Object getItem(int position) {
return this.list.get(position);
}
static class DataHandler{
TextView Heading,Name,Gender,ID;
EditText UserName,UserID;
Spinner UserGender;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
DataHandler dataHandler = new DataHandler();
if (convertView == null){ // If row is empty, we need to create row
LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.row_layout,parent,false);
dataHandler.Heading = convertView.findViewById(R.id.tvHeading);
dataHandler.Name = convertView.findViewById(R.id.tvName);
dataHandler.Gender = convertView.findViewById(R.id.tvGender);
dataHandler.ID = convertView.findViewById(R.id.tvID);
dataHandler.UserName = convertView.findViewById(R.id.etName);
dataHandler.UserID = convertView.findViewById(R.id.etID);
dataHandler.UserGender = convertView.findViewById(R.id.spGender);
convertView.setTag(dataHandler);
}else {
dataHandler = (DataHandler) convertView.getTag(); // If row already present then get that row
}
DataProvider dataProvider;
dataProvider = (DataProvider) this.getItem(position);
if (dataProvider != null) {
dataHandler.Heading.setText(dataProvider.getHeading());
dataHandler.Name.setText(dataProvider.getName());
dataHandler.Gender.setText(dataProvider.getGender());
dataHandler.ID.setText(dataProvider.getID());
}
return convertView;
}
}
DataProvider.java
package tandel.amit.expandablelistview;
/*
* Created by Amit Tandel on 2/2/2018.
*/
public class DataProvider {
private String Heading;
private String Name;
private String Gender;
private String ID;
public DataProvider(String Heading, String Name, String Gender, String ID) {
this.setHeading(Heading);
this.setName(Name);
this.setGender(Gender);
this.setID(ID);
}
public String getHeading() {
return Heading;
}
public void setHeading(String heading) {
Heading = heading;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getGender() {
return Gender;
}
public void setGender(String gender) {
Gender = gender;
}
public String getID() {
return ID;
}
public void setID(String ID) {
this.ID = ID;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context="tandel.amit.expandablelistview.MainActivity">
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
n
<Button
android:id="#+id/btnSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Save" />
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:id="#+id/fabAdd"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:src="#drawable/plus"
android:layout_marginBottom="80dp" />
</RelativeLayout>
row_layout.xml
<?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="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:id="#+id/tvHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="User 1"
android:textSize="18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="#+id/tvName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Name"
android:textSize="16sp" />
<EditText
android:id="#+id/etName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="#+id/tvGender"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Gender"
android:textSize="16sp" />
<Spinner
android:id="#+id/spGender"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="#+id/tvID"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ID"
android:textSize="16sp" />
<EditText
android:id="#+id/etID"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="8dp"
android:background="#000000" />
</LinearLayout>
I am beginner Android Developer. I want to create an Android Page which will dynamically display Three things 1. Question No. 2. Your Answer 3. Right Answer. How can I do this? It should look like this and can grow dynamically
What about something like this (Uses a listview):
Mainactivity:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ArrayList<Question> questionArrayList;
private void fillList(){
questionArrayList = new ArrayList<>();
for (int i = 0; i < 12; i++) {
questionArrayList.add(new Question(i,"myans","yourans"));
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fillList();
ListView mlIstView = (ListView)findViewById(R.id.mListview);
QuestionAdapter adapter = new QuestionAdapter(this,R.layout.listviewlayout,questionArrayList);
mlIstView.setAdapter(adapter);
}
}
Question.java
public class Question {
private int numberOfQuestion;
private String yourAnswer;
private String correctAnswer;
public Question(int numberOfQuestion, String yourAnswer, String correctAnswer) {
this.numberOfQuestion = numberOfQuestion;
this.yourAnswer = yourAnswer;
this.correctAnswer = correctAnswer;
}
public int getNumberOfQuestion() {
return numberOfQuestion;
}
public String getYourAnswer() {
return yourAnswer;
}
public String getCorrectAnswer() {
return correctAnswer;
}
}
QuestionAdapter
import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by waro on 6/4/17.
*/
public class QuestionAdapter extends ArrayAdapter<Question> {
private ArrayList<Question> arrayList;
public QuestionAdapter(#NonNull Context context, #LayoutRes int resource, #NonNull ArrayList<Question> objects) {
super(context, resource, objects);
arrayList = objects;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
if(convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.listviewlayout,parent,false);
}
Question question = arrayList.get(position);
TextView questiontv = (TextView)convertView.findViewById(R.id.question);
TextView yourans = (TextView)convertView.findViewById(R.id.yourans);
TextView correctans = (TextView)convertView.findViewById(R.id.correctans);
questiontv.setText(Integer.toString(question.getNumberOfQuestion()));
yourans.setText(question.getYourAnswer());
correctans.setText(question.getCorrectAnswer());
return convertView;
}
}
activity_mail.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="com.suprentive.waro.myapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:id="#+id/mLinearLayout">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Question"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Your answer"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Right answer"
android:layout_weight="1"/>
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mListview"
android:layout_below="#id/mLinearLayout">
</ListView>
</RelativeLayout>
listviewlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/question"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/yourans"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/correctans"
android:layout_weight="1"/>
</LinearLayout>
Result
Hopefully this helps you out.
i want to use a checkbox on items in recyclerview i load data from the server 4 by 4 (after seeing the last item he add another 4 new item)but the problem when add new items the checked item change to another item idon't know why i want to save every item checked on SharedPreferences to use them on commande activity
some pictures :
enter image description here
after going dow i found another item checked and the firs one uchecked :
enter image description here
card.xml
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_width="wrap_content"
android:layout_height="119dp"
android:layout_gravity="center"
android:elevation="3dp"
card_view:cardCornerRadius="1dp"
card_view:cardElevation="4dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txt_idArticle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="ID Article : "
android:textColor="#android:color/black"
android:textStyle="normal|bold" />
<TextView
android:id="#+id/txt_reference1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/txt_des"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="5dp"
android:text="Reference : "
android:textColor="#android:color/background_dark"
android:textStyle="normal|bold" />
<TextView
android:id="#+id/txt_des1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/txt_prix"
android:layout_alignBottom="#+id/txt_prix"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="Prix : "
android:textColor="#android:color/background_dark"
android:textStyle="normal|bold" />
<TextView
android:id="#+id/txt_des"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/txt_prix1"
android:layout_toRightOf="#+id/txt_prix1"
android:text="TextView" />
<TextView
android:id="#+id/txt_prix"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txt_prix1"
android:layout_toEndOf="#+id/txt_des1"
android:layout_toRightOf="#+id/txt_des1"
android:text="TextView" />
<TextView
android:id="#+id/txt_idArticle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/txt_idArticle1"
android:layout_toRightOf="#+id/txt_idArticle1"
android:text="TextView" />
<TextView
android:id="#+id/txt_reference"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/txt_reference1"
android:layout_alignBottom="#+id/txt_reference1"
android:layout_toEndOf="#+id/txt_reference1"
android:layout_toRightOf="#+id/txt_reference1"
android:text="TextView" />
<TextView
android:id="#+id/txt_prix1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/txt_reference1"
android:layout_marginBottom="5dp"
android:text="Designation : "
android:textColor="#android:color/black"
android:textStyle="normal|bold" />
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="panier"
android:textSize="10sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
recherche_art.java
package com.example.bacha.pfe.activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import com.example.bacha.pfe.R;
import com.example.bacha.pfe.adapter.ArticleAdapter;
import com.example.bacha.pfe.classes.Article;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class recherche_art extends AppCompatActivity {
private RecyclerView recyclerView ;
private GridLayoutManager gridLayoutManager;
private ArticleAdapter adapter ;
private List<Article> data_list ;
private String recherche_article;
private ImageButton btnrechArt ;
private EditText arech;
int d;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recherche_art);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
btnrechArt = (ImageButton) findViewById(R.id.btnrechArt);
recherche_art.this.onRestart();
btnrechArt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
d=0;
Log.d("PFE", String.valueOf(d));
arech= (EditText) findViewById(R.id.eTextRechArt);
data_list = new ArrayList<>();
recherche_article=arech.getText().toString();
load_article_from_server(d);
gridLayoutManager = new GridLayoutManager(recherche_art.this,1);
recyclerView.setLayoutManager(gridLayoutManager);
adapter = new ArticleAdapter(recherche_art.this,data_list);
recyclerView.setAdapter(adapter);
}
});
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if(gridLayoutManager.findLastCompletelyVisibleItemPosition() == data_list.size()-1){
d=data_list.size();
load_article_from_server(d);
Log.d("PFE", String.valueOf(d));
}
}
});
}
private void load_article_from_server(final int id) {
AsyncTask<Integer, Void, Void> task = new AsyncTask<Integer,Void, Void>() {
#Override
protected Void doInBackground(Integer... Params) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://10.0.2.2/slim/article/"+recherche_article+"/"+id).build();
try {
Response response = client.newCall(request).execute();
JSONArray array = new JSONArray(response.body().string());
for(int i=0;i<array.length();i++){
JSONObject object = array.getJSONObject(i);
Article data = new Article(/*dd,*/object.getString("id_Article"),object.getString("Reference"),object.getString("Designation"),object.getString("PVTTC"));
data_list.add(data);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
System.out.print("End of content");
}
return null ;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
adapter.notifyDataSetChanged();
}
};
task.execute(id);
}
}
activity_recherche_art.xml
<TextView
android:text="Recherche Article :"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:textSize="24sp"
android:textStyle="normal|bold"
android:textAlignment="center"
android:textColor="?attr/colorPrimary"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/recherche"
android:id="#+id/btnrechArt"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignTop="#+id/eTextRechArt"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/eTextRechArt"
android:layout_below="#+id/textView"
android:layout_toLeftOf="#+id/btnrechArt"
android:layout_toStartOf="#+id/btnrechArt" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
article_view:cardUseCompatPadding="true"
article_view:cardElevation="5dp"
article_view:cardCornerRadius="5dp"
android:scrollbars="vertical"
android:layout_marginTop="18dp"
android:layout_below="#+id/btnrechArt"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
Article.java
package com.example.bacha.pfe.classes;
import java.io.Serializable;
/**
* Created by BACHA on 07/03/2017.
*/
public class Article implements Serializable {
private int id;
private String id_Article,Reference,Designation,PVTTC;
public Article(/*int id,*/ String id_Article, String reference, String designation, String PVTTC) {
// this.id = id;
this.id_Article = id_Article;
Reference = reference;
Designation = designation;
this.PVTTC = PVTTC;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getId_Article() {
return id_Article;
}
public void setId_Article(String id_Article) {
this.id_Article = id_Article;
}
public String getReference() {
return Reference;
}
public void setReference(String reference) {
Reference = reference;
}
public String getDesignation() {
return Designation;
}
public void setDesignation(String designation) {
Designation = designation;
}
public String getPVTTC() {
return PVTTC;
}
public void setPVTTC(String PVTTC) {
this.PVTTC = PVTTC;
}
}
ArticleAdapter.java
package com.example.bacha.pfe.adapter;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import com.example.bacha.pfe.R;
import com.example.bacha.pfe.activity.ArticleDetail;
import com.example.bacha.pfe.classes.Article;
import java.util.List;
import static com.example.bacha.pfe.R.layout.card;
/**
* Created by BACHA on 08/03/2017.
*/
public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.ViewHolder> {
private static Context context ;
private List<Article> my_data ;
public ArticleAdapter(Context context, List<Article> my_data) {
this.context = context;
this.my_data = my_data;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(card,parent,false);
return new ArticleAdapter.ViewHolder(itemView);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.id_Article.setText(my_data.get(position).getId_Article());
holder.Reference.setText(my_data.get(position).getReference());
holder.Designation.setText(my_data.get(position).getDesignation());
holder.PVTTC.setText(my_data.get(position).getPVTTC());
holder.root.setTag(my_data.get(position));
}
#Override
public int getItemCount() {
return my_data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView id_Article,Reference,Designation,PVTTC;
View root;
public ViewHolder(View itemView) {
super(itemView);
root = itemView;
root.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Article art = (Article)v.getTag();
Intent intent = new Intent(context, ArticleDetail.class);
intent.putExtra("REFERENCE", art.getReference());
context.startActivity(intent);
}
});
id_Article=(TextView) itemView.findViewById(R.id.txt_idArticle);
Reference=(TextView) itemView.findViewById(R.id.txt_reference);
Designation=(TextView) itemView.findViewById(R.id.txt_des);
PVTTC=(TextView) itemView.findViewById(R.id.txt_prix);
}
#Override
public void onClick(View v) {
}
}
}
I have displayed the list of student from the database, field that are displayed Student_Roll_no, Student_Name and Student_ID.
Student_Roll_no and Student_Name are display using TextView and Student_ID id is display with checkbox.
Now I want to set the code by which, when I click at the list the checkbox of that particular row should be checked but the listview is not responding to OnItemClickListener function, as there i have make a toast that doesn't appear when i click on the list.
Sry for the bad english
activity_attendence.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_attendence"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.tejas.paras.bpibs.Attendence"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/roll"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:text="Roll"
android:layout_weight=".1"
android:textColor="#000000" />
<TextView
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:text="Name"
android:layout_weight=".6"
android:textColor="#000000" />
<TextView
android:text="Present"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/textView26"
android:layout_weight=".2"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#000000"
android:layout_height="1dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"></LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="375dp"
android:id="#+id/listview"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_weight="1.10" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:text="Save Attendence"
android:layout_width="match_parent"
android:background="#drawable/buttonlayout2"
android:textColor="#FFFFFF"
android:layout_height="wrap_content"
android:id="#+id/button5" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Attendence.java
package com.tejas.paras.bpibs;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
public class Attendence extends AppCompatActivity{
String date, course, year,ID;
String myJSON;
private static final String TAG_RESULTS = "result";
String link;
JSONArray peoples = null;
BufferedReader bufferedReader;
String result,data;
ArrayList<HashMap<String, String>> personList;
Button b5;
ListView list,listView;
private DataModel dataModel;
private ArrayList<DataModel> dataModels;
private static CustomAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attendence);
list=(ListView)findViewById(R.id.listview);
b5=(Button)findViewById(R.id.button5);
getData();
b5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(Attendence.this,"1."+view, Toast.LENGTH_SHORT).show();
}
});
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
dataModel = dataModels.get(position);
Toast.makeText(Attendence.this,"2.1",Toast.LENGTH_LONG).show();
ID=dataModel.getID();
Toast.makeText(Attendence.this,dataModel.getRoll(),Toast.LENGTH_LONG).show();
}
});
}
protected void showList() {
dataModels= new ArrayList<>();
try {
JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray(TAG_RESULTS);
for (int i = 0; i < peoples.length(); i++) {
JSONObject c = peoples.getJSONObject(i);
dataModels.add(new DataModel(c.getString("name"),c.getString("id"), c.getString("roll")));
}
adapter = new CustomAdapter(dataModels, getApplicationContext());
list.setAdapter(adapter);
}
catch (JSONException e) {
e.printStackTrace();
}
}
public void getData() {
class GetDataJSON extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
try {
link = "http://painnation.esy.es/attendence.php?date=18022017&course=MCA&year=3";
URL url = new URL(link);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = bufferedReader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
return result;
}
#Override
protected void onPostExecute(String result) {
myJSON = result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
}
item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".1">
<TextView
android:id="#+id/roll"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textStyle="bold"
android:text="Roll"
android:layout_weight=".1"
android:textColor="#000000" />
<TextView
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:textStyle="bold"
android:text="Name"
android:layout_weight=".6"
android:textColor="#000000" />
<CheckBox
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/id"
android:layout_weight=".15"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
CustomAdapter.java
package com.tejas.paras.bpibs;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import java.util.ArrayList;
public class CustomAdapter extends ArrayAdapter<DataModel> implements View.OnClickListener{
private ArrayList<DataModel> dataSet;
Context mContext;
private static class ViewHolder {
TextView name;
TextView roll;
CheckBox id;
}
public CustomAdapter(ArrayList<DataModel> data, Context context) {
super(context, R.layout.item_layout, data);
this.dataSet = data;
this.mContext=context;
}
#Override
public void onClick(View v) {
int position=(Integer) v.getTag();
Object object= getItem(position);
DataModel dataModel=(DataModel)object;
}
private int lastPosition = -1;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
DataModel dataModel = getItem(position);
ViewHolder viewHolder;
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.item_layout, parent, false);
viewHolder.name = (TextView) convertView.findViewById(R.id.name);
viewHolder.roll = (TextView) convertView.findViewById(R.id.roll);
viewHolder.id=(CheckBox) convertView.findViewById(R.id.id);
result=convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
}
lastPosition = position;
viewHolder.name.setText(dataModel.getName());
viewHolder.roll.setText(dataModel.getRoll());
viewHolder.id.setText(dataModel.getID());
return convertView;
}
}
DataModel.java
package com.tejas.paras.bpibs;
public class DataModel {
String roll;
String name;
String id;
public DataModel(String name1, String id1, String roll1) {
this.name=name1;
this.id=id1;
this.roll=roll1;
}
public String getName() {
return name;
}
public String getID() {
return id;
}
public String getRoll() {
return roll;
}
}
Attendence.php file and the database is hosted on a free hosting web server(Hostinger). you can check the DB output using the
http://painnation.esy.es/attendence.php?date=18022017&course=MCA&year=3
Output Screen
Data from server is fetch in json format.plz comment the code to do check the checkbox by clicking listview.
Thank You
i think you should put android:focusable="false" in item_layout.xml for checkbox, then onItemClickListener respond. for checkbox you need to put variable in DataModel, android make it true or false and change in listview put adapter.notifyDataSetChanged
<CheckBox
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/id"
android:layout_weight=".15"
android:focusable="false"
android:textColor="#000000" />
i am a beginner in android programming and i made a custom list view which should present the user details by searching function. while i search the the user i get only the name and the mobile of the user but the default images is not presenting.
this is what i get:
and this is what i would like to get:
this is the layout for the each row in the list:
<?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" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffffff"
>
<ImageView
android:id="#+id/profileImage"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#drawable/camera"
android:background="#drawable/border"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:textColor="#000000"
android:textSize="16dp"
android:text="Full name"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/profileImage"
android:layout_toEndOf="#+id/profileImage" />
<TextView
android:id="#+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textColor="#000000"
android:textSize="14dp"
android:text="User details"
android:layout_toRightOf="#+id/profileImage"
android:layout_alignBottom="#+id/profileImage"
android:layout_toLeftOf="#+id/imageButtonInviteUser"
android:layout_toStartOf="#+id/imageButtonInviteUser"
android:layout_below="#+id/title" />
<ImageButton
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#drawable/add_user_50"
android:id="#+id/imageButtonInviteUser"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="#drawable/border"/>
<View
android:id="#+id/divider2"
android:layout_below="#+id/profileImage"
android:layout_width="fill_parent"
android:layout_marginTop="1dp"
android:layout_height="6dp"
android:background="#android:color/darker_gray"/>
</RelativeLayout>
</RelativeLayout>
main layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/editTextSearch"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="Search..." />
<View
android:id="#+id/divider1"
android:layout_below="#+id/editTextSearch"
android:layout_width="fill_parent"
android:layout_height="6dp"
android:background="#android:color/darker_gray"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/divider1"
android:id="#+id/scrollViewListView"
android:fillViewport="true"
android:layout_above="#+id/linearLayoutBtn">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/listViewusers"
android:layout_below="#+id/divider1"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</ScrollView>
<View
android:id="#+id/divider2"
android:layout_below="#+id/scrollViewListView"
android:layout_width="fill_parent"
android:layout_height="6dp"
android:background="#android:color/darker_gray"/>
<LinearLayout
android:id="#+id/linearLayoutBtn"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<Button android:text="Save"
android:id="#+id/ButtonSave"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</Button>
<Button android:text="Discard"
android:id="#+id/ButtonDiscard"
android:layout_toRightOf="#+id/ButtonSave"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</Button>
</LinearLayout>
</RelativeLayout>
custom adapter:
/**
* Created by matant on 9/24/2015.
*/
import java.util.List;
import com.example.matant.gpsportclient.R;
import com.example.matant.gpsportclient.Utilities.InviteUsersListRow;
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.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class InviteUsersArrayAdapter extends ArrayAdapter<InviteUsersListRow> {
Context context;
List<InviteUsersListRow> rowUsers;
public InviteUsersArrayAdapter(Context context,int resourceId, List<InviteUsersListRow> items) {
super(context, resourceId, items);
this.context = context;
this.rowUsers = items;
}
/*private view holder class*/
private class ViewHolder {
ImageView imageView;
TextView txtTitle;
TextView txtDesc;
ImageButton imgStatus;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
InviteUsersListRow rowItem = (InviteUsersListRow) getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.invite_users_listview_row, null);
holder = new ViewHolder();
holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
holder.imageView = (ImageView) convertView.findViewById(R.id.profileImage);
holder.imgStatus = (ImageButton) convertView.findViewById(R.id.imageButtonInviteUser);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtDesc.setText(rowItem.getDesc());
holder.txtTitle.setText(rowItem.getTitle());
holder.imageView.setImageResource(rowItem.getImageId());
holder.imgStatus.setImageResource(rowItem.getImageStatus());
return convertView;
}
#Override
public int getCount() {
return rowUsers.size();
}
#Override
public InviteUsersListRow getItem(int position) {
return rowUsers.get(position);
}
#Override
public long getItemId(int position) {
return rowUsers.indexOf(getItem(position));
}
}
row in list:
package com.example.matant.gpsportclient.Utilities;
/**
* Created by matant on 9/22/2015.
*/
public class InviteUsersListRow {
private int imageId,imagestatus;
private String title;
private String desc;
public InviteUsersListRow(int imageId,int status, String title,String desc){
this.imageId = imageId;
this.title = title;
this.desc = desc;
this.imagestatus = status;
}
public int getImageId() {
return this.imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getImageStatus(){
return this.imagestatus;
}
public void setImagestatus(int imgStatus)
{
this.imagestatus = imgStatus;
}
}
main activity:
package com.example.matant.gpsportclient.Controllers;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import com.example.matant.gpsportclient.AsyncResponse;
import com.example.matant.gpsportclient.R;
import com.example.matant.gpsportclient.Utilities.InviteUsersArrayAdapter;
import com.example.matant.gpsportclient.Utilities.InviteUsersListRow;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class InviteUsersActivity extends AppCompatActivity implements AsyncResponse, View.OnClickListener,AdapterView.OnItemClickListener {
private EditText editTextSearch;
private Button btnSave,btnDiscard;
private ListView usersListView;
private List<InviteUsersListRow> rowUser;
private DBcontroller dbController;
public static final String EXTRA_USERS = "";
ListView listViewUsers;
List<InviteUsersListRow> rowUsers;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_invite_users);
editTextSearch = (EditText) findViewById(R.id.editTextSearch);
btnSave = (Button) findViewById(R.id.ButtonSave);
btnDiscard = (Button)findViewById(R.id.ButtonDiscard);
editTextSearch.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
sendDataToDBController();
}
#Override
public void afterTextChanged(Editable s) {
}
});
btnDiscard.setOnClickListener(this);
}
#Override
public void handleResponse(String resStr) {
Log.d("invite_Response", resStr);
if (resStr != null) {
try {
JSONObject json = new JSONObject(resStr);
String flg = json.getString("flag");
Log.d("flag",flg);
switch (flg){
case "user found":{
JSONArray jsonarr = json.getJSONArray("users");
Log.d("array",jsonarr.toString());
rowUsers = new ArrayList<InviteUsersListRow>();
for(int i = 0; i < jsonarr.length();i++){
{
Log.d("user is", jsonarr.getJSONObject(i).toString());
String name = jsonarr.getJSONObject(i).getString("name");
String mobile = jsonarr.getJSONObject(i).getString("mobile");
InviteUsersListRow rowUser = new InviteUsersListRow(R.id.profileImage, R.id.imageButtonInviteUser, name, mobile);
rowUsers.add(rowUser);
}
listViewUsers = (ListView) findViewById(R.id.listViewusers);
InviteUsersArrayAdapter Useradapter = new InviteUsersArrayAdapter(this,R.layout.invite_users_listview_row,rowUsers);
listViewUsers.setAdapter(Useradapter);
listViewUsers.setOnItemClickListener(this);
}
break;
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}else
Log.d("ServiceHandler", "Couldn't get any data from the url");
}
#Override
public void sendDataToDBController() {
String username = editTextSearch.getText().toString();
BasicNameValuePair tagreq = new BasicNameValuePair("tag", "search_user");
BasicNameValuePair name = new BasicNameValuePair("name", username);
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
nameValuePairList.add(tagreq);
nameValuePairList.add(name);
dbController = new DBcontroller(this,this);
dbController.execute(nameValuePairList);
}
#Override
public void preProcess() {
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.ButtonDiscard:
{
Intent i = new Intent();
setResult(RESULT_CANCELED,i);;
finish();
}
}
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
}
Replace the below
InviteUsersListRow rowUser = new InviteUsersListRow(R.id.profileImage, R.id.imageButtonInviteUser, name, mobile);
with
InviteUsersListRow rowUser = new InviteUsersListRow(R.drawable.camera, R.drawable.add_user_50, name, mobile);
The pic should be from "R.drawable" not from "R.id"
You should actually set an image resource below and not an id
holder.imageView.setImageResource(rowItem.getImageId());
holder.imgStatus.setImageResource(rowItem.getImageStatus());
In the above code you are not setting a drawable item.