I have a dynamic list from server database in listview. In each row of list, I've added two images where one of them is clickable which is in-built android Star Off image. Now when user clicks on Star Off image, that image should change to Star On. For this, I've set onClickListener on image but I am getting error on onClickListener. I've also tried onTouchListener but got same error. Here is my full code below :
Category.java
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class Category extends Activity implements OnClickListener
{
String url;
String CompName,CompID;
Strings st1=new Strings();
TextView text;
ImageView star;
ListView list;
//List<RowItem> rowItems;
TextView txt;
int k=0;
int po;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
ActionBar action=getActionBar();
action.setDisplayHomeAsUpEnabled(true);
action.setIcon(R.drawable.ic_launcher);
Intent intent=getIntent();
CompID = intent.getStringExtra("CompID");
url=st1.getUrl();
connect(CompID);
star=(ImageView)findViewById(R.id.favorite);
star.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(arg0==star)
{
star.setImageResource(android.R.drawable.star_big_on);
}
}
});
}
private void connect(String compID2)
{
// TODO Auto-generated method stub
String data;
List<String> r = new ArrayList<String>();
final List<String> r1=new ArrayList<String>();
ArrayAdapter<String>adapter=new ArrayAdapter<String>(getApplicationContext(),R.layout.list_text_black,R.id.list_item,r);
final ListView list=(ListView)findViewById(R.id.listView1);
//CustomBaseAdapter adapter = new CustomBaseAdapter(this, rowItems);
try
{
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url+"Retrive.php?q=comp&p="+URLEncoder.encode(compID2,"UTF-8"));
//HttpGet request = new HttpGet("http://ift.tt/1ogd0Kw"+URLEncoder.encode(abc,"UTF-8"));
HttpResponse response = client.execute(request);
HttpEntity entity=response.getEntity();
data=EntityUtils.toString(entity);
Log.e("STRING", data);
try
{
JSONArray json=new JSONArray(data);
for(int i=0;i<json.length(); i++)
{
JSONObject obj=json.getJSONObject(i);
CompName=obj.getString("fldName");//Name of the vendor.
CompID=obj.getString("fldVid");//ID of the vendor.
Log.e("STRING", CompName);
Log.e("STRING",CompID);
r.add(CompName);
r1.add(CompID);
//star=(ImageView)findViewById(R.id.favorite+k);
list.setAdapter(adapter);
ArrayList<HashMap<String, String>>list1=new ArrayList<HashMap<String,String>>();
HashMap<String,String>item=new HashMap<String, String>();
item.put("CompName",CompName);
item.put("CompID",CompID);
list1.add(item);
//Toast.makeText(getApplicationContext(),list1+"",5000).show();
list.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent,View view, int position, long id)
{
String a=Integer.toString(position);
po=position;
Log.e("selected company's id is : ",a);
String selected=r1.get(position);
//String selected=(String) list.getItemAtPosition(position);
Log.e("selected company's id is : ",selected);
Intent i1=new Intent(Category.this,Chatting.class);
//i1.putExtra("CompID", selected);
startActivity(i1);
}
});
String df=r1.get(po);
Log.e("dfsf", df);
}
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClientProtocolException e) {
Log.d("HTTPCLIENT", e.getLocalizedMessage());
} catch (IOException e) {
Log.d("HTTPCLIENT", e.getLocalizedMessage());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.category, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId())
{
case android.R.id.home:
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Category.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:ignore="UseCompoundDrawables,MangledCRLF,ContentDescription,DisableBaselineAlignment,InefficientWeight" >
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
</RelativeLayout>
list_text_black.xml
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp" >
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="50dp"
android:adjustViewBounds="true"
android:maxHeight="30dp"
android:maxWidth="30dp"
android:src="@drawable/ic_action_camera"
/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10dp"
android:adjustViewBounds="true"
android:maxHeight="30dp"
android:maxWidth="30dp"
android:src="@drawable/arrow1" />
<TextView
android:id="@+id/list_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="40dp"
android:text="Just Test"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/favorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/imageView1"
android:src="@android:drawable/btn_star_big_on"
android:clickable="true" />
</RelativeLayout>
No comments:
Post a Comment