2017-06-16 10 views
0

私は最近、Javaの学習を開始し、Androidアプリケーションを構築する方法を開始しました。私は現在、CustomListAdapterを使って、jsonによって解析されたMySQLデータをListViewに表示するアプリケーションに取り組んでいます。私が実行している問題は、TextViewがデータベースからDATETIMEを表示する方法を変更する方法です。今のように、完全なYYYY-MM-DD HH:MM:SS形式を示しています。実際にはHH:MM Aと表示しようとしています。私は他の人が同様の質問をしてSimpleDateFormatクラスを使用するように回答しているのを見ましたが、自分のコードにどのように適合するか理解できません。 due_timeは私のデータベースから解析されているカラムであり、レイアウト内にはTextViewという名前が付けられています。それがうまくいくならば、私は私がどこかでdue_timeを挿入すると仮定しています、そして、私のCustomListAdaptergetView方法にスニペットプラグMySQL DATETIMEをAndroid TextViewの特定のフォーマットに変換する

SimpleDateFormat df1 = new SimpleDateFormat("YYYY-MM-DD"); 
SimpleDateFormat df2 = new SimpleDateFormat("DD-MM-YYYY"); 
try{ 
    Date d = df1.parse(DateString); 
    System.out.println("new date format " + df2.format(d)); 
}catch(Exception e){ 
    e.printStackTrace(); 
} 

これは、使用する他の誰かによって提案されたスニペットました、 下に示された。正しい軌道にいるのですか?

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    if (inflater == null) 
     inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    if (convertView == null) 
     convertView = inflater.inflate(R.layout.list_row, null); 

    TextView due_time = (TextView) convertView.findViewById(R.id.due_time); 

    // getting order data for the row 
    Order o = orderItems.get(position); 

    // Due Time 
    due_time.setText(o.getDueTime()); 

    return convertView; 
} 

答えて

2

こんにちは、あなたはこのようなあなたのGetViewメソッドからこのメソッドを呼び出すと良い作業のTextViewに

due_time.setText(convertFormat(o.getDueTime())); 
+0

を設定することができます

public static String convertFormat(String inputDate) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = null; try { date = simpleDateFormat.parse(inputDate); } catch (ParseException e) { e.printStackTrace(); } if (date == null) { return ""; } SimpleDateFormat convetDateFormat = new SimpleDateFormat("hh:mm a"); return convetDateFormat.format(date); } 

、これを試すことができます。ありがとう – OWADVL

関連する問題