탭호스트에
인기순많은순(내림차순)|인기없는순(오름차순) 으로 하려고합니다.
현재 인기만은순 (내림차순으로)탭을 누르면 정렬이 됩니당.
반대로 인기없는순(오름차순)으로 코딩을 하려면 어떠케 해야될지 모르겠습니당..(php는 작업완료)
아래소스는 인기만은순(내림차순으로)정렬 되는 소스입니다.
getData(http://웹서버주소/list_data.php); <-- 내림차순php
getData2(http://웹서버주소/list2_data.php); <--오름차순 php
public void onTabChanged(String tabId) {
if(tabId.equalsIgnoreCase("Tab1")) {
Toast.makeText(mContext, "인기많은순", Toast.LENGTH_SHORT).show();
}else if(tabId.equalsIgnoreCase("Tab2")){
Toast.makeText(mContext, "인기없는순", Toast.LENGTH_SHORT).show();
}
}
탭리스너를 달고
protected void showList(){
try {
JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray(TAG_RESULTS);
//strList = new ArrayList<String>() ;
strList = new ArrayList<>() ;
for(int i=0;i<peoples.length();i++){
JSONObject c = peoples.getJSONObject(i);
String no = c.getString(TAG_NO);
String likes= c.getString(TAG_LIKES);
Log.d("sort",strList+"");
HashMap<String,String> persons = new HashMap<String,String>();
persons.put(TAG_NO,no);
persons.put(TAG_NAME,name);
persons.put(TAG_LIKES,likes);
personList.add(persons);
strList.add(personList);
Log.d("str",strList+"");
}
adapter = new SimpleAdapter(
Main_Page.this, personList,
R.layout.list_item,
new String[]{TAG_NAME,TAG_LIKES},
new int[]{ R.id.name,R.id.textlike}
)
{
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Context context = parent.getContext();
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, parent, false);
}
TextView txtName = (TextView) convertView.findViewById(R.id.name);
final TextView txtlikes = (TextView) convertView.findViewById(R.id.textlike);
Button button = (Button) convertView.findViewById(R.id.like);
//final TextView editlike = (TextView) convertView.findViewById(R.id.textlike);
txtName.setText(personList.get(position).get("name"));
txtlikes.setText(personList.get(position).get("likes"));
test=personList.get(position).get("addCode");
button.setOnClickListener(new View.OnClickListener() {
String mCount = personList.get(position).get(TAG_LIKES);
@Override
public void onClick(View view) {
//txtlikes.setText(String.valueOf(mCount+1));
txtlikes.setText(""+(Integer.parseInt(mCount)+1));
String likes = txtlikes.getText().toString();
String no = null;
no = personList.get(position).get(TAG_NO);
//insertToDatabase(no,likes);
ModifyActivityState(no,likes);
}
});
return convertView;
}
};
listview.setAdapter(adapter);
listview.setTextFilterEnabled(true);
listview.setClickable(true);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Toast.makeText(getApplicationContext(),position, Toast.LENGTH_LONG).show();
Log.d("포지션",position+"");
intent = new Intent(mContext,Detail_Activity.class);
intent.putExtra("name",personList.get(position).get(TAG_NAME));
intent.putExtra("likes",personList.get(position).get(TAG_LIKES));
startActivity(intent);
Log.d("인텐트",intent+"");
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
public void getData(String url){
class GetDataJSON extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String uri = params[0];
Log.d("uri",uri);
BufferedReader bufferedReader = null;
try {
URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String json;
while((json = bufferedReader.readLine())!= null){
sb.append(json+"\n");
}
return sb.toString().trim();
}catch(Exception e){
return null;
}
}
@Override
protected void onPostExecute(String result){
myJSON=result;
// Log.d("result",result);
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute(url);
}