我正在开发android应用程序及其相关的邮箱,我想互相做separete读取和未读消息。我正在使用它。当CustomListView中的Clicked项目保存项目位置或名称并更改单击的textView的颜色CustomListView和当Refresh活动变为点击的颜色
package com.androidbegin.jsonparsetutorial;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
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.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity implements OnRefreshListener,
OnClickListener {
// Declare Variables
JSONObject jsonobject;
JSONArray jsonarray;
JSONArray jsonarray2;
ListView listview;
ListViewAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String RANK = "rank";
static String COUNTRY = "country";
static String POPULATION = "population";
static String FLAG = "flag";
Button btngeri;
static Integer[] imageId = { R.drawable.enveloppe, R.drawable.enveloppe,
R.drawable.enveloppe, R.drawable.enveloppe, R.drawable.enveloppe,
R.drawable.enveloppe, R.drawable.enveloppe };
SwipeRefreshLayout swipeLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from listview_main.xml
setContentView(R.layout.listview_main);
btngeri = (Button) findViewById(R.id.btn_geri);
btngeri.setOnClickListener(this);
swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(this);
swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
// Execute DownloadJSON AsyncTask
new DownloadJSON().execute();
}
// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(MainActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("Mail Kutusu");
// Set progressdialog message
mProgressDialog.setMessage("Güncelleme Yapılıyor...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
JSONObject returndata = null;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://78.186.62.169:8210/AnketServis.asmx/Message");
httppost.setHeader("Content-type", "application/json");
JSONObject jsonparameter = new JSONObject();
try {
// jsonparameter.put("AnketID", "3");
httppost.setEntity(new StringEntity(jsonparameter.toString(),
"UTF-8"));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity);
Log.i("@banner_responseString", "" + responseString);
try {
returndata = new JSONObject(responseString);
JSONArray jsonMainNode = returndata.optJSONArray("d");
int lengthJsonArr = jsonMainNode.length();
Log.i("@lengthJson", "" + lengthJsonArr);
int sorusayisi = lengthJsonArr;
for (int i = 0; i < sorusayisi; i++) {
HashMap<String, String> map = new HashMap<String, String>();
HashMap<String, Integer> map2 = new HashMap<String, Integer>();
if (i == sorusayisi)
break;
JSONObject jsonChildNode = jsonMainNode
.getJSONObject(i);
map.put("message", jsonChildNode.getString("ID"));
map.put("country", jsonChildNode.getString("Konu"));
map.put("rank", jsonChildNode.getString("Tarih"));
map.put("population", jsonChildNode.getString("Mesaj"));
// map.put("population", datetime);
// map.put("population",
// jsonobject.getString("population"));
// map.put("flag",
// jsonChildNode.getString("Tarih"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
}
} catch (Exception e) {
}
return null;
}
@Override
protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listview);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(MainActivity.this, arraylist, imageId);
// Set the adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
}
}
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = getIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
startActivity(intent);
swipeLayout.setRefreshing(false);
}
}, 5000);
}
}
ListView适配器
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
private final Integer[] imageId;
public ListViewAdapter(Context context,
ArrayList<HashMap<String, String>> arraylist, Integer[] imageId) {
this.imageId = imageId;
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView rank;
TextView country;
TextView population;
ImageView flag;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.listview_item, parent, false);
// Get the position
resultp = data.get(position);
// Locate the TextViews in listview_item.xml
rank = (TextView) itemView.findViewById(R.id.rank);
country = (TextView) itemView.findViewById(R.id.country);
population = (TextView) itemView.findViewById(R.id.population);
// Locate the ImageView in listview_item.xml
flag = (ImageView) itemView.findViewById(R.id.flag);
// Capture position and set results to the TextViews
// rank.setText(resultp.get(MainActivity.RANK));
country.setText(resultp.get(MainActivity.COUNTRY));
// population.setText(resultp.get(MainActivity.POPULATION));
// Capture position and set results to the ImageView
// Passes flag images URL into ImageLoader.class
imageLoader.DisplayImage(resultp.get(MainActivity.FLAG), flag);
// Capture ListView item click
itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// Get the position
resultp = data.get(position);
Intent intent = new Intent(context, SingleItemView.class);
// Pass all data rank
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
// intent.putExtra("rank", resultp.get(MainActivity.RANK));
// Pass all data country
intent.putExtra("country", resultp.get(MainActivity.COUNTRY));
// Pass all data population
// intent.putExtra("population",
// resultp.get(MainActivity.POPULATION));
// Pass all data flag
intent.putExtra("flag", resultp.get(MainActivity.FLAG));
// Start SingleItemView Class
context.startActivity(intent);
}
});
return itemView;
}
答案 0 :(得分:0)
您需要使用选择器为您的布局提供响应式触控。关于选择者的研究。我肯定你会找到一些东西。
答案 1 :(得分:0)
bg.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/holo_red_dark"/>
</shape>
itesm_select.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/bg"/>
</selector>
列表视图
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:saveEnabled="true"
android:listSelector="@drawable/itesm_select"
/>